Index: lams_common/src/java/org/lamsfoundation/lams/util/LanguageUtil.java =================================================================== RCS file: /usr/local/cvsroot/lams_common/src/java/org/lamsfoundation/lams/util/LanguageUtil.java,v diff -u -r1.9 -r1.10 --- lams_common/src/java/org/lamsfoundation/lams/util/LanguageUtil.java 21 Apr 2008 06:08:04 -0000 1.9 +++ lams_common/src/java/org/lamsfoundation/lams/util/LanguageUtil.java 18 Dec 2008 01:33:15 -0000 1.10 @@ -109,11 +109,24 @@ } /** - * Returns server default locale. + * Returns server default locale; if invalid, uses en_AU. */ public static SupportedLocale getDefaultLocale() { String localeName = Configuration.get(ConfigurationKeys.SERVER_LANGUAGE); - return getSupportedLocale(localeName.substring(0,2),localeName.substring(3)); + String langIsoCode = DEFAULT_LANGUAGE; + String countryIsoCode = DEFAULT_COUNTRY; + if (StringUtils.isNotBlank(localeName) && localeName.length() > 2) { + langIsoCode = localeName.substring(0,2); + countryIsoCode = localeName.substring(3); + } + + SupportedLocale locale = null; + locale = getSupportedLocaleOrNull(langIsoCode, countryIsoCode); + if (locale == null) { + locale = getSupportedLocaleOrNull(DEFAULT_LANGUAGE, DEFAULT_COUNTRY); + } + + return locale; } /** @@ -134,31 +147,43 @@ } /** - * Finds a locale based on language and/or country. + * Finds a locale based on language and/or country, use server locale if invalid. */ public static SupportedLocale getSupportedLocale(String langIsoCode, String countryIsoCode) { SupportedLocale locale = null; - Map properties = new HashMap(); + locale = getSupportedLocaleOrNull(langIsoCode, countryIsoCode); + if (locale == null) { + locale = getDefaultLocale(); + } + + return locale; + } + + // Given langIsoCode and countryIsoCode, returns SupportedLocale (null if doesn't exist) + private static SupportedLocale getSupportedLocaleOrNull(String langIsoCode, String countryIsoCode) { + SupportedLocale locale = null; + + Map properties = new HashMap(); + if (StringUtils.isNotBlank(countryIsoCode)) { properties.put("countryIsoCode", countryIsoCode.trim()); } if (StringUtils.isNotBlank(langIsoCode)) { properties.put("languageIsoCode", langIsoCode.trim()); } - + if (properties.isEmpty()) { - return getDefaultLocale(); + return null; } List list = getService().findByProperties(SupportedLocale.class, properties); if (list != null && list.size() > 0){ Collections.sort(list); locale = (SupportedLocale)list.get(0); } else { - locale = getDefaultLocale(); + locale = null; } - return locale; }