001/* 002 * Copyright 2012 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.ametys.cms.languages; 018 019import java.util.ArrayList; 020import java.util.HashMap; 021import java.util.LinkedHashMap; 022import java.util.List; 023import java.util.Map; 024 025import org.apache.avalon.framework.component.Component; 026import org.apache.avalon.framework.configuration.Configurable; 027import org.apache.avalon.framework.configuration.Configuration; 028import org.apache.avalon.framework.configuration.ConfigurationException; 029 030import org.ametys.core.ui.Callable; 031import org.ametys.runtime.i18n.I18nizableText; 032 033/** 034 * Handle possible languages 035 */ 036public class LanguagesManager implements Configurable, Component 037{ 038 /** The component role */ 039 public static final String ROLE = LanguagesManager.class.getName(); 040 041 private Map<String, Language> _languages; 042 043 @Override 044 public void configure(Configuration configuration) throws ConfigurationException 045 { 046 _languages = new LinkedHashMap<>(); 047 048 for (Configuration lConf : configuration.getChildren("language")) 049 { 050 String code = lConf.getAttribute("code"); 051 Language language; 052 053 if (lConf.getAttributeAsBoolean("i18n", true)) 054 { 055 language = new Language(code, new I18nizableText("application", lConf.getValue(code))); 056 } 057 else 058 { 059 language = new Language(code, new I18nizableText(lConf.getValue(code))); 060 } 061 _languages.put(code, language); 062 } 063 } 064 065 /** 066 * Get the language by its code 067 * @param code The language code 068 * @return The language or <code>null</code> if not found 069 */ 070 public Language getLanguage (String code) 071 { 072 return _languages.get(code); 073 } 074 075 /** 076 * Get the list of possible languages 077 * @return the association language-code Language object 078 */ 079 public Map<String, Language> getAvailableLanguages() 080 { 081 return _languages; 082 } 083 084 /** 085 * Get the list of available languages as a JSON object 086 * @return The available languages 087 */ 088 @Callable 089 public List<Map<String, Object>> getAvailableLanguagesAsJsonObject() 090 { 091 List<Map<String, Object>> availableLanguages = new ArrayList<>(); 092 093 for (Language language : getAvailableLanguages().values()) 094 { 095 availableLanguages.add(toJson(language)); 096 } 097 098 return availableLanguages; 099 } 100 101 /** 102 * Return the JSON representation of a language 103 * @param language The language 104 * @return The json object representing the language 105 */ 106 protected Map<String, Object> toJson(Language language) 107 { 108 Map<String, Object> info = new HashMap<>(); 109 110 info.put("name", language.getCode()); 111 info.put("label", language.getLabel()); 112 113 return info; 114 } 115}