Author: thomas.heute(a)jboss.com
Date: 2010-01-19 07:42:54 -0500 (Tue, 19 Jan 2010)
New Revision: 1367
Modified:
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/ResourceBundleDescription.java
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/BaseResourceBundleService.java
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/LocaleConfigImpl.java
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/LocaleConfigServiceImpl.java
portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/TestLocaleConfigService.java
portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/test/TestLocaleConfig.java
portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/test/TestResourceBundleService.java
portal/trunk/webui/eXo/src/main/java/org/exoplatform/webui/organization/UIUserProfileInputSet.java
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/webui/portal/UILanguageSelector.java
Log:
GTNPORTAL-307: Support for derivated languages (French from France, French from
Canada...)
Modified:
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/ResourceBundleDescription.java
===================================================================
---
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/ResourceBundleDescription.java 2010-01-19
09:35:25 UTC (rev 1366)
+++
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/ResourceBundleDescription.java 2010-01-19
12:42:54 UTC (rev 1367)
@@ -56,8 +56,8 @@
b.append(name_);
if (language_ != null)
b.append('_').append(language_);
- // if(country_ != null) b.append('_').append(country_);
- // if(variant_ != null) b.append('_').append(variant_);
+ if(country_ != null) b.append('_').append(country_);
+ if(variant_ != null) b.append('_').append(variant_);
id_ = b.toString();
}
return id_;
Modified:
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/BaseResourceBundleService.java
===================================================================
---
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/BaseResourceBundleService.java 2010-01-19
09:35:25 UTC (rev 1366)
+++
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/BaseResourceBundleService.java 2010-01-19
12:42:54 UTC (rev 1367)
@@ -121,7 +121,8 @@
}
/**
- * Loads all the "init" resource bundles
+ * Loads all the "init" resource bundles
+ *
* @see org.picocontainer.Startable#start()
*/
public void start()
@@ -194,17 +195,33 @@
try
{
Collection<LocaleConfig> localeConfigs =
localeService_.getLocalConfigs();
- String defaultLang = localeService_.getDefaultLocaleConfig().getLanguage();
+ // String defaultLang =
+ // localeService_.getDefaultLocaleConfig().getLanguage();
+ Locale defaultLocale = localeService_.getDefaultLocaleConfig().getLocale();
+
for (Iterator<LocaleConfig> iter = localeConfigs.iterator();
iter.hasNext();)
{
LocaleConfig localeConfig = iter.next();
- String language = localeConfig.getLanguage();
- String content = getResourceBundleContent(name, language, defaultLang, cl);
+ // String language = localeConfig.getLanguage();
+ // String content = getResourceBundleContent(name, language,
+ // defaultLang, cl);
+ Locale locale = localeConfig.getLocale();
+ String language = locale.getLanguage();
+ String country = locale.getCountry();
+
+ String content = getResourceBundleContent(name, locale, defaultLocale, cl);
if (content != null)
{
// save the content
ResourceBundleData data = new ResourceBundleData();
- data.setId(baseName + "_" + language);
+ if (country != null && country.length() > 0)
+ {
+ data.setId(baseName + "_" + language + "_" +
country);
+ }
+ else
+ {
+ data.setId(baseName + "_" + language);
+ }
data.setName(baseName);
data.setLanguage(language);
data.setData(content);
@@ -260,10 +277,100 @@
}
/**
- * Invalidate an entry in the cache at this level. Normally this is called by the
subclass.
- *
- * @param name the bundle name
+ * This method is used for country support
+ *
+ * @param baseName
+ * @param locale
+ * @param defaultLocale
+ * @param cl
+ * @return
*/
+ protected String getResourceBundleContent(String baseName, Locale locale, Locale
defaultLocale, ClassLoader cl)
+ throws Exception
+ {
+ List<String> candidateFiles = new ArrayList<String>();
+
+ String language = locale.getLanguage();
+ String country = locale.getCountry().toUpperCase();
+
+ String defaultLanguage = defaultLocale.getLanguage();
+ String defaultCountry = defaultLocale.getCountry().toUpperCase();
+
+ if (country != null && country.length() > 0)
+ {
+ candidateFiles.add(baseName + "_" + language + "_" + country
+ ".properties");
+ }
+
+ if (language != null && language.length() > 0 )
+ {
+ candidateFiles.add(baseName + "_" + language +
".properties");
+ }
+
+ if (defaultCountry != null && defaultCountry.length() > 0)
+ {
+ candidateFiles.add(baseName + "_" + defaultLanguage + "_" +
defaultCountry + ".properties");
+ }
+
+ if (defaultLanguage != null && defaultLanguage.length() > 0)
+ {
+ candidateFiles.add(baseName + "_" + defaultLanguage +
".properties");
+ }
+
+ candidateFiles.add(baseName + ".properties");
+
+ cl = new PropertiesClassLoader(cl, true);
+ String fileName = null;
+
+ try
+ {
+ URL url = null;
+ for (String candidateFile : candidateFiles)
+ {
+ url = cl.getResource(candidateFile);
+ if (url != null)
+ {
+ fileName = candidateFile;
+ break;
+ }
+ }
+
+ if (url != null)
+ {
+ InputStream is = url.openStream();
+ try
+ {
+ byte[] buf = IOUtil.getStreamContentAsBytes(is);
+ return new String(buf, "UTF-8");
+ }
+ finally
+ {
+ try
+ {
+ is.close();
+ }
+ catch (IOException e)
+ {
+ // Do nothing
+ }
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ throw new Exception("Error while reading the file: " + fileName, ex);
+ }
+
+ return null;
+
+ }
+
+ /**
+ * Invalidate an entry in the cache at this level. Normally this is called by
+ * the subclass.
+ *
+ * @param name
+ * the bundle name
+ */
protected final void invalidate(String name)
{
cache_.remove(name);
@@ -275,14 +382,30 @@
{
return IdentityResourceBundle.getInstance();
}
+
+ // Case 1: ResourceBundle of portlets, standard java API is used
if (isClasspathResource(name))
return ResourceBundleLoader.load(name, locale, cl);
- String id = name + "_" + locale.getLanguage();
+
+ // Case 2: ResourceBundle of portal
+ String country = locale.getCountry();
+ String id;
+ if (country != null && country.length() > 0)
+ {
+ id = name + "_" + locale.getLanguage() + "_" +
locale.getCountry();
+ }
+ else
+ {
+ id = name + "_" + locale.getLanguage();
+ }
+
try
{
ResourceBundle rb = cache_.get(id);
if (rb != null)
+ {
return rb;
+ }
}
catch (Exception ex)
{
Modified:
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/LocaleConfigImpl.java
===================================================================
---
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/LocaleConfigImpl.java 2010-01-19
09:35:25 UTC (rev 1366)
+++
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/LocaleConfigImpl.java 2010-01-19
12:42:54 UTC (rev 1367)
@@ -45,7 +45,7 @@
predefinedLocaleMap_ = new HashMap<String, Locale>(10);
predefinedLocaleMap_.put("us", Locale.US);
predefinedLocaleMap_.put("en", Locale.ENGLISH);
- predefinedLocaleMap_.put("fr", Locale.FRANCE);
+ predefinedLocaleMap_.put("fr", Locale.FRENCH);
predefinedLocaleMap_.put("zh", Locale.SIMPLIFIED_CHINESE);
}
@@ -112,7 +112,15 @@
localeName_ = localeName;
locale_ = predefinedLocaleMap_.get(localeName);
if (locale_ == null)
- locale_ = new Locale(localeName);
+ {
+ String[] localeParams = localeName.split("_");
+ if (localeParams.length > 1)
+ {
+ locale_ = new Locale(localeParams[0], localeParams[1]);
+ }else{
+ locale_ = new Locale(localeName);
+ }
+ }
}
public final String getLanguage()
Modified:
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/LocaleConfigServiceImpl.java
===================================================================
---
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/LocaleConfigServiceImpl.java 2010-01-19
09:35:25 UTC (rev 1366)
+++
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/LocaleConfigServiceImpl.java 2010-01-19
12:42:54 UTC (rev 1367)
@@ -80,7 +80,8 @@
}
/**
- * @param lang a locale language
+ * @param lang
+ * a locale language
* @return The LocalConfig
*/
public LocaleConfig getLocaleConfig(String lang)
@@ -159,7 +160,15 @@
log.debug("Added locale config " + config + " to the set of
locale configs");
//
- configs_.put(config.getLanguage(), config);
+ String country = config.getLocale().getCountry();
+ if (country != null && country.length() > 0)
+ {
+ configs_.put(config.getLanguage() + "_" + country, config);
+ }
+ else
+ {
+ configs_.put(config.getLanguage(), config);
+ }
if (i == 0)
defaultConfig_ = config;
}
Modified:
portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/TestLocaleConfigService.java
===================================================================
---
portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/TestLocaleConfigService.java 2010-01-19
09:35:25 UTC (rev 1366)
+++
portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/TestLocaleConfigService.java 2010-01-19
12:42:54 UTC (rev 1367)
@@ -113,7 +113,7 @@
assertLocaleConfig(en, "en", "Default configuration for english
locale", "UTF-8", "UTF-8", Orientation.LT,
Locale.ENGLISH);
assertLocaleConfig(fr, "fr", "Default configuration for the french
locale", "UTF-8", "UTF-8", Orientation.LT,
- Locale.FRANCE);
+ Locale.FRENCH);
assertLocaleConfig(ar, "ar", "Default configuration for the arabic
locale", "UTF-8", "UTF-8", Orientation.RT,
new Locale("ar"));
assertLocaleConfig(vi, "vi", "Default configuration for the vietnam
locale", "UTF-8", "UTF-8", Orientation.LT,
Modified:
portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/test/TestLocaleConfig.java
===================================================================
---
portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/test/TestLocaleConfig.java 2010-01-19
09:35:25 UTC (rev 1366)
+++
portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/test/TestLocaleConfig.java 2010-01-19
12:42:54 UTC (rev 1367)
@@ -64,7 +64,7 @@
// --------------get a locale------------------
locale = service_.getLocaleConfig("fr");
assertTrue("expect locale config is found", locale != null);
- assertTrue("expect France locale is found",
locale.getLocale().equals(Locale.FRANCE));
+ assertTrue("expect France locale is found",
locale.getLocale().equals(Locale.FRENCH));
locale = service_.getLocaleConfig("vi");
assertTrue("expect locale config is found", locale != null);
@@ -78,7 +78,7 @@
Locale vnlocale = service_.getLocaleConfig("vi").getLocale();
hasObjectInCollection(vnlocale, locales, new LocaleComparator());
hasObjectInCollection(Locale.ENGLISH, locales, new LocaleComparator());
- hasObjectInCollection(Locale.FRANCE, locales, new LocaleComparator());
+ hasObjectInCollection(Locale.FRENCH, locales, new LocaleComparator());
}
public static class LocaleComparator implements Comparator
Modified:
portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/test/TestResourceBundleService.java
===================================================================
---
portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/test/TestResourceBundleService.java 2010-01-19
09:35:25 UTC (rev 1366)
+++
portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/test/TestResourceBundleService.java 2010-01-19
12:42:54 UTC (rev 1367)
@@ -74,8 +74,8 @@
ResourceBundle res = service_.getResourceBundle(fileRes, Locale.ENGLISH);
// //------------create ressource bundle in database------------------
- createResourceBundle(databaseRes, PROPERTIES, Locale.ENGLISH.getLanguage());
- createResourceBundle(databaseRes, PROPERTIES_FR, Locale.FRANCE.getLanguage());
+ createResourceBundle(databaseRes, PROPERTIES, Locale.ENGLISH);
+ createResourceBundle(databaseRes, PROPERTIES_FR, Locale.FRANCE);
res = service_.getResourceBundle(databaseRes, Locale.ENGLISH);
assertTrue("Expect to find the ResourceBundle", res != null);
@@ -85,13 +85,13 @@
assertEquals("Expect French locale bundle", "fr",
res.getString("language"));
assertEquals("Expect French locale bundle", "property",
res.getString("property"));
//--------- Update a databseRes resource bundle in database ----------------
- createResourceBundle(databaseRes, PROPERTIES_FR_UPDATE,
Locale.FRANCE.getLanguage());
+ createResourceBundle(databaseRes, PROPERTIES_FR_UPDATE, Locale.FRANCE);
res = service_.getResourceBundle(databaseRes, Locale.FRANCE);
assertEquals("Expect French locale bundle", "fr-property",
res.getString("property"));
//--------Update fileRes resource bundle in databse--------------
String datas = "key1=fileSystem\nlanguage=french";
- createResourceBundle(fileRes, datas, Locale.FRANCE.getLanguage());
+ createResourceBundle(fileRes, datas, Locale.FRANCE);
res = service_.getResourceBundle(fileRes, Locale.FRANCE);
assertTrue("Expect to find the ResourceBundle", res != null);
assertTrue("Expect 'fileRes' is updated",
res.getString("key1").equals("fileSystem"));
@@ -99,7 +99,7 @@
//--------Update fileRes resource bundle in databse--------------
datas = "key1=fileSystemUpdate\nlanguage=french";
- createResourceBundle(fileRes, datas, Locale.FRANCE.getLanguage());
+ createResourceBundle(fileRes, datas, Locale.FRANCE);
res = service_.getResourceBundle(fileRes, Locale.FRANCE);
assertTrue("Expect to find the ResourceBundle", res != null);
assertTrue("Expect 'fileRes' is updated",
res.getString("key1").equals("fileSystemUpdate"));
@@ -112,8 +112,8 @@
ResourceBundle res = service_.getResourceBundle(fileRes, Locale.ENGLISH);
//------------create ressource bundle in database------------------
- createResourceBundle(databaseRes, PROPERTIES, Locale.ENGLISH.getLanguage());
- createResourceBundle(databaseRes, PROPERTIES_FR, Locale.FRANCE.getLanguage());
+ createResourceBundle(databaseRes, PROPERTIES, Locale.ENGLISH);
+ createResourceBundle(databaseRes, PROPERTIES_FR, Locale.FRANCE);
res = service_.getResourceBundle(databaseRes, Locale.ENGLISH);
assertTrue("Expect to find the ResourceBundle", res != null);
@@ -147,8 +147,8 @@
ResourceBundle res = service_.getResourceBundle(fileRes, Locale.ENGLISH);
//------------create ressource bundle in database------------------
- createResourceBundle(databaseRes, PROPERTIES, Locale.ENGLISH.getLanguage());
- createResourceBundle(databaseRes, PROPERTIES_FR, Locale.FRANCE.getLanguage());
+ createResourceBundle(databaseRes, PROPERTIES, Locale.ENGLISH);
+ createResourceBundle(databaseRes, PROPERTIES_FR, Locale.FRANCE);
res = service_.getResourceBundle(databaseRes, Locale.ENGLISH);
assertTrue("Expect to find the ResourceBundle", res != null);
@@ -165,12 +165,16 @@
assertTrue("Expect at least 2 locale properties resources", l.size()
>= 2);
}
- private void createResourceBundle(String name, String datas, String language) throws
Exception
+ private void createResourceBundle(String name, String datas, Locale locale) throws
Exception
{
ResourceBundleData data = service_.createResourceBundleDataInstance();
data.setName(name);
data.setData(datas);
- data.setLanguage(language);
+ data.setLanguage(locale.getLanguage());
+ if (locale.getCountry().trim().length() != 0)
+ {
+ data.setCountry(locale.getCountry());
+ }
service_.saveResourceBundle(data);
}
Modified:
portal/trunk/webui/eXo/src/main/java/org/exoplatform/webui/organization/UIUserProfileInputSet.java
===================================================================
---
portal/trunk/webui/eXo/src/main/java/org/exoplatform/webui/organization/UIUserProfileInputSet.java 2010-01-19
09:35:25 UTC (rev 1366)
+++
portal/trunk/webui/eXo/src/main/java/org/exoplatform/webui/organization/UIUserProfileInputSet.java 2010-01-19
12:42:54 UTC (rev 1367)
@@ -42,9 +42,7 @@
import java.util.Locale;
/**
- * Created by The eXo Platform SARL
- * Author : Dang Van Minh
- * minhdv81(a)yahoo.com
+ * Created by The eXo Platform SARL Author : Dang Van Minh minhdv81(a)yahoo.com
* Jun 28, 2006
*/
@ComponentConfig(template = "system:/groovy/webui/form/UIVTabInputSet.gtmpl")
@@ -125,7 +123,7 @@
}
private void initLanguageCombo(UIFormSelectBox langSelectBox)
- {
+ {
if (langSelectBox == null)
return;
String selectedLang = langSelectBox.getSelectedValues()[0];
@@ -139,13 +137,23 @@
String displayLanguage = null;
String displayName = null;
String language = null;
+ String country = null;
SelectItemOption<String> option;
while (i.hasNext())
{
LocaleConfig config = i.next();
- displayLanguage = config.getLocale().getDisplayLanguage(currentLocale);
- displayName = config.getLocale().getDisplayName(currentLocale);
- language = config.getLanguage();
+ Locale locale = config.getLocale();
+ displayName = locale.getDisplayName(currentLocale);
+ language = locale.getDisplayLanguage(currentLocale);
+ country = locale.getCountry();
+ if (country != null && country.length() > 0)
+ {
+ displayLanguage = language + " (" +
locale.getDisplayCountry(currentLocale) + ")";
+ }
+ else
+ {
+ displayLanguage = language;
+ }
option = new SelectItemOption<String>(displayLanguage, language,
displayName);
if (lang.equals(selectedLang))
{
@@ -208,7 +216,8 @@
for (UIComponent uiComp : inputSet.getChildren())
{
UIFormStringInput uiInput = (UIFormStringInput)uiComp;
- //if(uiInput.getValue() == null || uiInput.getValue().length() < 1)
continue;
+ // if(uiInput.getValue() == null || uiInput.getValue().length() < 1)
+ // continue;
userProfile.getUserInfoMap().put(uiInput.getName(), uiInput.getValue());
}
}
Modified:
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/webui/portal/UILanguageSelector.java
===================================================================
---
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/webui/portal/UILanguageSelector.java 2010-01-19
09:35:25 UTC (rev 1366)
+++
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/webui/portal/UILanguageSelector.java 2010-01-19
12:42:54 UTC (rev 1367)
@@ -64,7 +64,20 @@
Locale locale = localeConfig.getLocale();
String displayName = locale.getDisplayLanguage(currentLocale);
String lang = locale.getLanguage();
- String localedName = capitalizeFirstLetter(locale.getDisplayLanguage(locale));
+ String country = locale.getCountry();
+ String localedName;
+
+ if (country != null && country.length() > 0)
+ {
+ localedName =
+ capitalizeFirstLetter(locale.getDisplayLanguage(currentLocale) + "
(" + locale.getDisplayCountry(currentLocale) + ")");
+ lang = lang + "_" + country;
+ }
+ else
+ {
+ localedName =
capitalizeFirstLetter(locale.getDisplayLanguage(currentLocale));
+ }
+
if (localedName == null || localedName.length() == 0)
localedName = "???";
if (locale.getDisplayName().equalsIgnoreCase(currentLocale.getDisplayName()))
@@ -77,7 +90,7 @@
}
optionsList.add(localeItem);
}
- //TODO need use other UIComponent here
+ // TODO need use other UIComponent here
Collections.sort(optionsList, new LanguagesComparator());
List<SelectItemCategory> contientsCategories = new
ArrayList<SelectItemCategory>();
SelectItemCategory category = new SelectItemCategory("Languages");
@@ -111,12 +124,11 @@
UIPortalApplication uiApp = Util.getUIPortalApplication();
UIMaskWorkspace uiMaskWS =
uiApp.getChildById(UIPortalApplication.UI_MASK_WS_ID);
uiMaskWS.setUIComponent(null);
- //event.getRequestContext().addUIComponentToUpdateByAjax(uiMaskWS) ;
+ // event.getRequestContext().addUIComponentToUpdateByAjax(uiMaskWS) ;
Util.getPortalRequestContext().setFullRender(false);
if (language == null || language.trim().length() < 1)
return;
- //if(!uiPortal.isModifiable()) return;
-
+ // if(!uiPortal.isModifiable()) return;
LocaleConfigService localeConfigService =
event.getSource().getApplicationComponent(LocaleConfigService.class);
LocaleConfig localeConfig = localeConfigService.getLocaleConfig(language);
if (localeConfig == null)
@@ -136,7 +148,7 @@
}
}
}
-
+
private String capitalizeFirstLetter(String word)
{
if (word == null)