@SuppressWarnings("unchecked")
public UILanguageSelector() throws Exception {
name_ = "UIChangeLanguage";
LocaleConfigService configService = getApplicationComponent(LocaleConfigService.class);
Locale currentLocale = Util.getPortalRequestContext().getLocale();
SelectItemOption localeItem = null;
List<SelectItemOption> optionsList = new ArrayList<SelectItemOption>();
for (Object object : configService.getLocalConfigs()) {
LocaleConfig localeConfig = (LocaleConfig) object;
Locale locale = localeConfig.getLocale();
String lang = locale.getLanguage();
String country = locale.getCountry();
ResourceBundle currentLocaleResourceBundle = getResourceBundle(currentLocale);
ResourceBundle localeResourceBundle = getResourceBundle(locale);
String key = "Locale." + lang;
String displayName = null;
try {
String translation = currentLocaleResourceBundle.getString(key);
displayName = translation;
} catch (MissingResourceException e) {
displayName = capitalizeFirstLetter(locale.getDisplayLanguage(currentLocale));
}
String localedName = null;
try {
String translation = localeResourceBundle.getString(key);
localedName = translation;
} catch (MissingResourceException e) {
localedName = capitalizeFirstLetter(locale.getDisplayLanguage(locale));
}
if (country != null && country.length() > 0) {
lang = lang + "_" + country;
key = "Locale." + lang;
try {
String translation = currentLocaleResourceBundle.getString(key);
displayName = translation;
} catch (MissingResourceException e) {
displayName = capitalizeFirstLetter(locale.getDisplayLanguage(currentLocale)) + " - "
+ capitalizeFirstLetter(locale.getDisplayCountry(currentLocale));
}
try {
String translation = localeResourceBundle.getString(key);
localedName = translation;
} catch (MissingResourceException e) {
localedName = capitalizeFirstLetter(locale.getDisplayLanguage(locale)) + " - "
+ capitalizeFirstLetter(locale.getDisplayCountry(locale));
}
}
if (localedName == null || localedName.length() == 0)
localedName = "???";
if (locale.getDisplayName().equalsIgnoreCase(currentLocale.getDisplayName())) {
localeItem = new SelectItemOption(displayName, lang, localedName, "", true);
} else {
localeItem = new SelectItemOption(displayName, lang, localedName, "");
}
optionsList.add(localeItem);
}
// TODO need use other UIComponent here
Collections.sort(optionsList, new LanguagesComparator());