Index: lams_common/src/java/org/lamsfoundation/lams/util/LanguageUtil.java =================================================================== diff -u -r43612de482ce842c250b97e38f2964d719c5338f -r516eab2b292fd5234c4c2fc83057f72552947790 --- lams_common/src/java/org/lamsfoundation/lams/util/LanguageUtil.java (.../LanguageUtil.java) (revision 43612de482ce842c250b97e38f2964d719c5338f) +++ lams_common/src/java/org/lamsfoundation/lams/util/LanguageUtil.java (.../LanguageUtil.java) (revision 516eab2b292fd5234c4c2fc83057f72552947790) @@ -36,155 +36,162 @@ import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; - /** * Various internationalisation (internationalization) utilities. - * + * * @author Fiona Malikoff - * + * */ public class LanguageUtil { - public static final String DEFAULT_LANGUAGE = "en"; - public static final String DEFAULT_COUNTRY = "AU"; - public static final String DEFAULT_DIRECTION = "LTR"; - private static IUserManagementService service; - - private static IUserManagementService getService() { - if (service == null) { - WebApplicationContext ctx = WebApplicationContextUtils - .getWebApplicationContext(HttpSessionManager.getInstance().getServletContext()); - service = (IUserManagementService) ctx.getBean("userManagementService"); - } - return service; + public static final String DEFAULT_LANGUAGE = "en"; + + public static final String DEFAULT_COUNTRY = "AU"; + + public static final String DEFAULT_DIRECTION = "LTR"; + + private static IUserManagementService service; + + private static IUserManagementService getService() { + if (service == null) { + WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(HttpSessionManager + .getInstance().getServletContext()); + service = (IUserManagementService) ctx.getBean("userManagementService"); } - - /** - * Get the default language, country, based on entries in the - * server configuration file. - * - * @return String[language, country] - */ - public static String[] getDefaultLangCountry() { + return service; + } - String languageIsoCode = null; - String countryIsoCode = null; + /** + * Get the default language, country, based on entries in the server + * configuration file. + * + * @return String[language, country] + */ + public static String[] getDefaultLangCountry() { - String serverLang = Configuration.get(ConfigurationKeys.SERVER_LANGUAGE); - if ( serverLang != null) { - // assume either "en" or "en_AU" formats - if ( serverLang.length() >= 2) - languageIsoCode = serverLang.substring(0,2); - if ( serverLang.length() >= 5) - countryIsoCode = serverLang.substring(3,5); - } + String languageIsoCode = null; + String countryIsoCode = null; - // fallback to en_AU - if ( languageIsoCode == null ) - languageIsoCode = DEFAULT_LANGUAGE; - if ( countryIsoCode == null ) - languageIsoCode = DEFAULT_COUNTRY; + String serverLang = Configuration.get(ConfigurationKeys.SERVER_LANGUAGE); + if (serverLang != null) { + // assume either "en" or "en_AU" formats + if (serverLang.length() >= 2) + languageIsoCode = serverLang.substring(0, 2); + if (serverLang.length() >= 5) + countryIsoCode = serverLang.substring(3, 5); + } - return new String[]{languageIsoCode, countryIsoCode}; + // fallback to en_AU + if (languageIsoCode == null) + languageIsoCode = DEFAULT_LANGUAGE; + if (countryIsoCode == null) + languageIsoCode = DEFAULT_COUNTRY; + return new String[] { languageIsoCode, countryIsoCode }; + + } + + /** + * Get the default direction, based on the values in the server + * configuration file. + * + * @return direction + */ + public static String getDefaultDirection() { + String direction = Configuration.get(ConfigurationKeys.SERVER_PAGE_DIRECTION); + if (direction == null) + direction = DEFAULT_DIRECTION; + return direction; + } + + /** + * Get the default timezone + * + * @return timezone + */ + public static TimeZone getDefaultTimeZone() { + return TimeZone.getDefault(); + } + + /** + * Returns server default locale; if invalid, uses en_AU. + */ + public static SupportedLocale getDefaultLocale() { + String localeName = Configuration.get(ConfigurationKeys.SERVER_LANGUAGE); + String langIsoCode = DEFAULT_LANGUAGE; + String countryIsoCode = DEFAULT_COUNTRY; + if (StringUtils.isNotBlank(localeName) && localeName.length() > 2) { + langIsoCode = localeName.substring(0, 2); + countryIsoCode = localeName.substring(3); } - - /** - * Get the default direction, based on the values in the server configuration file. - * @return direction - */ - public static String getDefaultDirection() { - String direction = Configuration.get(ConfigurationKeys.SERVER_PAGE_DIRECTION); - if ( direction == null ) - direction = DEFAULT_DIRECTION; - return direction; + + SupportedLocale locale = null; + locale = getSupportedLocaleOrNull(langIsoCode, countryIsoCode); + if (locale == null) { + locale = getSupportedLocaleOrNull(DEFAULT_LANGUAGE, DEFAULT_COUNTRY); } - /** - * Get the default timezone - * @return timezone - */ - public static TimeZone getDefaultTimeZone() { - return TimeZone.getDefault(); + return locale; + } + + /** + * Searches for a locale based on language, then country, matching the + * single input string. Otherwise returns server default locale. + */ + public static SupportedLocale getSupportedLocale(String input) { + List list = getService().findByProperty(SupportedLocale.class, "languageIsoCode", input); + if (list != null && list.size() > 0) { + return (SupportedLocale) list.get(0); + } else { + list = getService().findByProperty(SupportedLocale.class, "countryIsoCode", input); + if (list != null && list.size() > 0) { + return (SupportedLocale) list.get(0); + } } - - /** - * Returns server default locale; if invalid, uses en_AU. - */ - public static SupportedLocale getDefaultLocale() { - String localeName = Configuration.get(ConfigurationKeys.SERVER_LANGUAGE); - 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; + return getDefaultLocale(); + } + + /** + * 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; + + locale = getSupportedLocaleOrNull(langIsoCode, countryIsoCode); + if (locale == null) { + locale = getDefaultLocale(); } - - /** - * Searches for a locale based on language, then country, matching the single input string. - * Otherwise returns server default locale. - */ - public static SupportedLocale getSupportedLocale(String input) { - List list = getService().findByProperty(SupportedLocale.class, "languageIsoCode", input); - if (list!=null && list.size()>0) { - return (SupportedLocale)list.get(0); - } else { - list = getService().findByProperty(SupportedLocale.class, "countryIsoCode", input); - if (list!=null && list.size()>0) { - return (SupportedLocale)list.get(0); - } - } - return getDefaultLocale(); + + return locale; } - - /** - * 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; - - 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()); } - - // 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 null; - } - - List list = getService().findByProperties(SupportedLocale.class, properties); - if (list != null && list.size() > 0){ - Collections.sort(list); - locale = (SupportedLocale)list.get(0); - } else { - locale = null; - } - return locale; + if (StringUtils.isNotBlank(langIsoCode)) { + properties.put("languageIsoCode", langIsoCode.trim()); } + if (properties.isEmpty()) { + 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 = null; + } + return locale; + } + }