/** * ** Returns an I18N instance based on the specified package name and Locale ** @param pkgName * The resource package name ** @param loc The Locale resource from with the localized text is * loaded */ public static I18N getI18N(String pkgName, Locale loc) { if (pkgName != null) { loc = I18N.getLocale(loc); /* get package map for specific Locale */ Map<String, I18N> packageMap = localeMap.get(loc); if (packageMap == null) { packageMap = new HashMap<String, I18N>(); localeMap.put(loc, packageMap); } /* get I18N instance for package */ I18N i18n = packageMap.get(pkgName); if (i18n == null) { i18n = new I18N(pkgName, loc); packageMap.put(pkgName, i18n); } return i18n; } else { /* no package specified */ return null; } }
public String toString() { String locStr = RTConfig.getString(RTKey.SESSION_LOCALE, null); if (StringTools.isBlank(locStr)) { return this.getDefault(); } else { Locale loc = I18N.getLocale(locStr); return this.toString(loc); } }
/** * ** Constructor ** @param pkgName The resource package name ** @param loc The Locale resource * from with the localized text is loaded */ private I18N(String pkgName, Locale loc) { String bundleName = null; try { this.locale = I18N.getLocale(loc); bundleName = ((pkgName == null) || pkgName.equals("")) ? LOCAL_STRINGS : (pkgName + _LOCAL_STRINGS); this.resBundle = ResourceBundle.getBundle(bundleName, this.locale); // Print.logInfo("Found bundle: " + bundleName); } catch (Throwable th) { // MissingResourceException if (loc != null) { // quietly ignore this exception if (loc == null) Print.logInfo("Bundle not found: " + bundleName + " [" + th); } this.resBundle = null; } }
/** ** Debug/Testing entry point ** @param argv The command-line args */ public static void main(String argv[]) { RTConfig.setCommandLineArgs(argv); if (RTConfig.hasProperty(ARG_PACKAGE)) { String pkg = RTConfig.getString(ARG_PACKAGE, "org.opengts.util"); String loc = RTConfig.getString(ARG_LOCALE, "en"); String key = RTConfig.getString(ARG_KEY, ""); Locale locale = I18N.getLocale(loc); Print.sysPrintln("Package: " + pkg); Print.sysPrintln("Locale : " + locale + " [" + loc + "]"); Print.sysPrintln("Key : " + key); I18N i18n = I18N.getI18N(pkg, locale); if (i18n != null) { Print.sysPrintln("String : " + i18n.getString(key, "Undefined")); } else { Print.sysPrintln("Package resource not found"); } System.exit(0); } if (RTConfig.hasProperty("test")) { I18N i18n = getI18N(I18N.class, null); i18n.printKeyValues(); String m3 = i18n.getString( "m.m3", "{0}", new Object() { public String toString() { return mainStr; } }); String m2 = i18n.getString("m.m2", "How Now Brown {0}", m3); String m1 = i18n.getString("m.m1", "Message: \\n{0}", m2); Print.sysPrintln(m1); mainStr = "Horse"; Print.sysPrintln(m1); } }
/** * ** Gets the Java Locale instance based on the specified locale name ** @param loc The name of * the Locale ** @return The Java Locale instance */ public static Locale getLocale(String loc) { Locale locale = I18N.getLocale(loc, null); return (locale != null) ? locale : I18N.getDefaultLocale(); }