001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.configuration.preferences; 029 030import org.opencms.file.CmsObject; 031import org.opencms.main.OpenCms; 032import org.opencms.util.CmsStringUtil; 033import org.opencms.xml.content.CmsXmlContentProperty; 034 035import java.util.ArrayList; 036import java.util.Collections; 037import java.util.Iterator; 038import java.util.LinkedHashMap; 039import java.util.List; 040import java.util.Locale; 041import java.util.Map; 042import java.util.stream.Collectors; 043 044import org.apache.commons.lang3.StringUtils; 045 046import com.google.common.collect.ComparisonChain; 047 048/** 049 * Workplace locale preference.<p> 050 */ 051public class CmsLanguagePreference extends CmsBuiltinPreference { 052 053 /** The nice name. */ 054 private static final String NICE_NAME = "%(key." 055 + org.opencms.workplace.commons.Messages.GUI_LABEL_LANGUAGE_0 056 + ")"; 057 058 /** 059 * Creates a new instance.<p> 060 * 061 * @param name the name 062 */ 063 public CmsLanguagePreference(String name) { 064 065 super(name); 066 m_basic = true; 067 } 068 069 /** 070 * Gets the language selection options, with the locales as keys and the titles as values. 071 * 072 * @return the map of language selection options 073 */ 074 public static Map<Locale, String> getOptionMapForLanguage() { 075 076 // get available locales from the workplace manager 077 List<Locale> locales = new ArrayList<>(OpenCms.getWorkplaceManager().getLocales()); 078 List<Locale> contentLocales = OpenCms.getLocaleManager().getAvailableLocales(); 079 080 // Put locales that are configured as content locales first 081 Collections.sort(locales, (a, b) -> { 082 int indexA = contentLocales.indexOf(a); 083 int indexB = contentLocales.indexOf(b); 084 return ComparisonChain.start().compareTrueFirst(indexA != -1, indexB != -1).compare(indexA, indexB).compare( 085 a.toString(), 086 b.toString()).result(); 087 }); 088 089 Iterator<Locale> i = locales.iterator(); 090 LinkedHashMap<Locale, String> result = new LinkedHashMap<>(); 091 for (Locale currentLocale : locales) { 092 // add all locales to the select box 093 String language = currentLocale.getDisplayLanguage(currentLocale); 094 if (CmsStringUtil.isNotEmpty(currentLocale.getCountry())) { 095 language = language + " (" + currentLocale.getDisplayCountry(currentLocale) + ")"; 096 } 097 if (CmsStringUtil.isNotEmpty(currentLocale.getVariant())) { 098 language = language + " (" + currentLocale.getDisplayVariant(currentLocale) + ")"; 099 } 100 language = StringUtils.capitalize(language); 101 result.put(currentLocale, language); 102 } 103 return result; 104 105 } 106 107 /** 108 * @see org.opencms.configuration.preferences.CmsBuiltinPreference#getPropertyDefinition(org.opencms.file.CmsObject) 109 */ 110 @Override 111 public CmsXmlContentProperty getPropertyDefinition() { 112 113 CmsXmlContentProperty prop = new CmsXmlContentProperty( 114 getName(), //name 115 "string", //type 116 null, //widget 117 null, //widgetconfig 118 null, //regex 119 null, //ruletype 120 null, //default 121 NICE_NAME, //nicename 122 null, //description 123 null, //error 124 null//preferfolder 125 ); 126 return prop; 127 } 128 129 /** 130 * @see org.opencms.configuration.preferences.CmsBuiltinPreference#getPropertyDefinition(org.opencms.file.CmsObject) 131 */ 132 @Override 133 public CmsXmlContentProperty getPropertyDefinition(CmsObject cms) { 134 135 Locale locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(cms); 136 String options = getOptionsForLanguage(locale); 137 CmsXmlContentProperty prop = new CmsXmlContentProperty( 138 getName(), //name 139 "string", //type 140 "select_notnull", //widget 141 options, //widgetconfig 142 null, //regex 143 null, //ruletype 144 null, //default 145 NICE_NAME, //nicename 146 null, //description 147 null, //error 148 null//preferfolder 149 ); 150 return prop; 151 } 152 153 /** 154 * Gets the options for the language selector.<p> 155 * 156 * @param setLocale the locale for the select options 157 * 158 * @return the options for the language selector 159 */ 160 private String getOptionsForLanguage(Locale setLocale) { 161 162 Map<Locale, String> options = getOptionMapForLanguage(); 163 String result = options.entrySet().stream().map(entry -> entry.getKey() + ":" + entry.getValue()).collect( 164 Collectors.joining("|")); 165 return result; 166 } 167 168}