Index: lams_common/src/java/org/lamsfoundation/lams/util/LanguageUtil.java =================================================================== diff -u -rb02b8bc25e7d8feeed8978ebdc148500d762d148 -r43612de482ce842c250b97e38f2964d719c5338f --- lams_common/src/java/org/lamsfoundation/lams/util/LanguageUtil.java (.../LanguageUtil.java) (revision b02b8bc25e7d8feeed8978ebdc148500d762d148) +++ lams_common/src/java/org/lamsfoundation/lams/util/LanguageUtil.java (.../LanguageUtil.java) (revision 43612de482ce842c250b97e38f2964d719c5338f) @@ -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; }