/** * Returns the translated value for the translation-key filled with the * parameters. * * @param key * key for the translation in the language file. the key should * <b>always</b> have the following structure * <i>PACKAGE_NAME_FROM_CALLER.CLASS_NAME_FROM_CALLER.key</i> * @param def * default value which will be returned if there is no mapping * for the key * @param args * parameters which should be inserted in the translated string * @return translated value or the def parameter filled with the parameters * @see Loc#L(String, String) */ public static String LF(final String key, final String def, final Object... args) { try { return String.format(Loc.L(key, def), args); } catch (final Exception e) { return "Error: " + key; } }
/** * Returns the translated value for the translation-key. If the current * language file doesn't contain the translated value, the default value * will be returned. * * @param key * key for the translation in the language file. the key should * <b>always</b> have the following structure * <i>PACKAGE_NAME_FROM_CALLER.CLASS_NAME_FROM_CALLER.key</i> * @param def * default value which will be returned if there is no mapping * for the key * @return translated value or the def parameter * @see Loc#LF(String, String, Object...) * @throws IllegalArgumentException * if the key is null or is empty */ public static String L(String key, final String def) { if (key == null || (key = key.trim()).length() == 0) { throw new IllegalArgumentException(); } if (Loc.DATA == null) { Log.L.warning("No parsed localization found! Loading now from saved localization file!"); try { Loc.setLocale(Loc.CFG.get(Loc.PROPERTY_LOCALE, Loc.FALLBACK_LOCALE)); } catch (final Exception e) { Log.L.severe("Error while loading the stored localization name!"); Loc.setLocale(Loc.FALLBACK_LOCALE); } if (Loc.DATA == null) { return def == null ? "Error in Loc! No loaded data!" : def; } } final String loc = Loc.DATA.get(key.toLowerCase().hashCode()); if (loc == null) { Loc.DATA.put(key.toLowerCase().hashCode(), def); return def; } return loc; }
/** * Set-up this class by creating the HashMap for the key-string-pairs. * * @param loc * name of the localization file * @see Loc#parseLocalization(RFSFile) */ public static void setLocale(String loc) { try { if (loc == null) { loc = Loc.CFG.get(Loc.PROPERTY_LOCALE, Loc.getDefaultLocale()); } // first check filesystem final URL file = Loc.getResourceURL(loc); Loc.locale = loc; if (file != null) { // TODO Loc.CFG.put(Loc.PROPERTY_LOCALE, loc); Loc.parseLocalization(file); } else { Log.L.info("The language " + loc + " isn't available! Parsing default (" + Loc.FALLBACK_LOCALE + ".loc) one!"); Loc.locale = Loc.getDefaultLocale(); final String[] locs = Loc.locale.split("_"); Locale.setDefault(new Locale(locs[0], locs[1])); Loc.parseLocalization(Loc.getResourceURL(Loc.FALLBACK_LOCALE)); } } catch (final Exception e) { org.appwork.utils.logging.Log.exception(e); } }
/** * @return */ private static String getDefaultLocale() { if (Loc.DEFAULT_LOCALE_CACHE != null) { return Loc.DEFAULT_LOCALE_CACHE; } final String sys = System.getProperty("user.language").toLowerCase(); final String cou = System.getProperty("user.country").toUpperCase(); final String[] locs = Loc.getLocales(); if (locs.length == 0) { Loc.DEFAULT_LOCALE_CACHE = Loc.FALLBACK_LOCALE; } if (Loc.DEFAULT_LOCALE_CACHE == null) { for (final String l : locs) { if (l.equals(sys + "_" + cou)) { Loc.DEFAULT_LOCALE_CACHE = l; break; } } } if (Loc.DEFAULT_LOCALE_CACHE == null) { for (final String l : locs) { if (l.equals(sys)) { Loc.DEFAULT_LOCALE_CACHE = l; break; } } } if (Loc.DEFAULT_LOCALE_CACHE == null) { for (final String l : locs) { if (l.startsWith(sys + "_")) { Loc.DEFAULT_LOCALE_CACHE = l; break; } } } if (Loc.DEFAULT_LOCALE_CACHE == null) { for (final String l : locs) { if (l.equals(Loc.FALLBACK_LOCALE)) { Loc.DEFAULT_LOCALE_CACHE = l; break; } } } if (Loc.DEFAULT_LOCALE_CACHE == null) { Loc.DEFAULT_LOCALE_CACHE = locs[0]; } return Loc.DEFAULT_LOCALE_CACHE; }
/** * Returns a localized regular expression for words that usualy ar present * in an error message * * @return */ public static String getErrorRegex() { return Loc.L("system.error", ".*(error|failed).*"); }