Author: mstruk
Date: 2010-08-31 17:28:42 -0400 (Tue, 31 Aug 2010)
New Revision: 3988
Modified:
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/LocaleContextInfo.java
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/localization/DefaultLocalePolicyService.java
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/localization/LocalizationFilter.java
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/localization/LocalizationLifecycle.java
Log:
GTNPORTAL-1306, GTNPORTAL-1355: Fallback from more specific locale (lang_COUNTRY) - when
not available - to more general one (lang)
Modified:
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/LocaleContextInfo.java
===================================================================
---
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/LocaleContextInfo.java 2010-08-31
20:20:03 UTC (rev 3987)
+++
portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/LocaleContextInfo.java 2010-08-31
21:28:42 UTC (rev 3988)
@@ -171,16 +171,44 @@
* Otherwise it returns null.
*
* @param locale locale to check
- * @return locale if supported, null otherwise
+ * @return original locale if supported, null otherwise
*/
public Locale getLocaleIfSupported(Locale locale)
{
+ if (locale == null)
+ return null;
if (supportedLocales.contains(locale))
return locale;
return null;
}
/**
+ * Helper method that returns the locale only if it's language is supported by
portal.
+ * Otherwise it returns null.
+ *
+ * @param locale locale to check
+ * @return original locale if language is supported, null otherwise
+ */
+ public Locale getLocaleIfLangSupported(Locale locale)
+ {
+ if (locale == null)
+ return null;
+ if (supportedLocales.contains(locale))
+ return locale;
+
+ if ("".equals(locale.getCountry()) == false)
+ {
+ Locale loc = new Locale(locale.getLanguage());
+ if (supportedLocales.contains(loc))
+ {
+ // return original locale
+ return locale;
+ }
+ }
+ return null;
+ }
+
+ /**
* Helper method to convert String representation of Locale into Locale object.
* @param portalLocaleName String representation of Locale
* @return locale
Modified:
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/localization/DefaultLocalePolicyService.java
===================================================================
---
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/localization/DefaultLocalePolicyService.java 2010-08-31
20:20:03 UTC (rev 3987)
+++
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/localization/DefaultLocalePolicyService.java 2010-08-31
21:28:42 UTC (rev 3988)
@@ -32,7 +32,13 @@
* This service represents a default policy for determining LocaleConfig to be used for
user's session.
* This service is registered through portal services configuration file:
conf/portal/configuration.xml
* Custom locale determination policy can be implemented by overriding or completely
replacing this class,
- * and registering an alternative implementation. Special care needs to be taken to
assure Locale consistency
+ * and registering an alternative implementation.
+ *
+ * To gracefully fallback from more specific locales (lang_COUNTRY) to more generic
(lang) without loss
+ * of information about user's language selection use {@link
LocaleContextInfo#getLocaleIfLangSupported(java.util.Locale)}
+ * and return a more specific Locale. The Locale will be appropriately narrowed by
LocalePolicy caller.
+ *
+ * Special care needs to be taken to assure Locale consistency
* between portal requests and non-portal requests - like login redirect upon failed
authentication attempt.
* To keep consistency at least one of {@link LocaleContextInfo#cookieLocales} and {@link
LocaleContextInfo#sessionLocale}
* needs to be enabled.
@@ -71,7 +77,7 @@
*/
protected Locale getLocaleConfigForRegistered(LocaleContextInfo context)
{
- Locale locale = context.getLocaleIfSupported(context.getUserProfileLocale());
+ Locale locale = context.getLocaleIfLangSupported(context.getUserProfileLocale());
if (locale == null)
locale = getLocaleConfigFromCookie(context);
if (locale == null)
@@ -95,7 +101,7 @@
if (locales != null)
{
for (Locale loc: locales)
- return context.getLocaleIfSupported(loc);
+ return context.getLocaleIfLangSupported(loc);
}
return null;
}
@@ -145,7 +151,7 @@
if (locales != null)
{
for (Locale locale: locales)
- return context.getLocaleIfSupported(locale);
+ return context.getLocaleIfLangSupported(locale);
}
return null;
}
Modified:
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/localization/LocalizationFilter.java
===================================================================
---
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/localization/LocalizationFilter.java 2010-08-31
20:20:03 UTC (rev 3987)
+++
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/localization/LocalizationFilter.java 2010-08-31
21:28:42 UTC (rev 3988)
@@ -143,8 +143,15 @@
localeCtx.setPortalLocale(Locale.ENGLISH);
Locale locale = localePolicy.determineLocale(localeCtx);
- if (!supportedLocales.contains(locale))
+ boolean supported = supportedLocales.contains(locale);
+
+ if (!supported && !"".equals(locale.getCountry()))
{
+ locale = new Locale(locale.getLanguage());
+ supported = supportedLocales.contains(locale);
+ }
+ if (!supported)
+ {
if (log.isWarnEnabled())
log.warn("Unsupported locale returned by LocalePolicy: " +
localePolicy + ". Falling back to 'en'.");
locale = Locale.ENGLISH;
Modified:
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/localization/LocalizationLifecycle.java
===================================================================
---
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/localization/LocalizationLifecycle.java 2010-08-31
20:20:03 UTC (rev 3987)
+++
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/application/localization/LocalizationLifecycle.java 2010-08-31
21:28:42 UTC (rev 3988)
@@ -149,8 +149,15 @@
localeCtx.setPortalLocale(portalLocale);
Locale locale = localePolicy.determineLocale(localeCtx);
- if (!supportedLocales.contains(locale))
+ boolean supported = supportedLocales.contains(locale);
+
+ if (!supported && !"".equals(locale.getCountry()))
{
+ locale = new Locale(locale.getLanguage());
+ supported = supportedLocales.contains(locale);
+ }
+ if (!supported)
+ {
if (log.isWarnEnabled())
log.warn("Unsupported locale returned by LocalePolicy: " +
localePolicy + ". Falling back to 'en'.");
locale = Locale.ENGLISH;
Show replies by date