Exemplo n.º 1
0
 /**
  * ** Returns an I18N.Text instance used for lazy localization.<br>
  * ** (use in XML loaders to avoid expression matches when auto-updating
  * 'LocalStrings_XX.properties') ** @param pkg The package name ** @param key The localization key
  * ** @param dft The default localized text ** @param showError If true, a stacktrace will be
  * display if the key is invalid. ** @return An I18N.Text instance used for lazy localization
  */
 public static I18N.Text parseText(String pkg, String key, String dft, boolean showError) {
   if (dft == null) {
     Print.logStackTrace("Default value is null!");
     return new I18N.Text(pkg, key, "");
   } else if (!StringTools.isBlank(key)) {
     return new I18N.Text(pkg, key, dft);
   } else if (!StringTools.startsWithIgnoreCase(dft, I18N_KEY_STARTE)
       && !StringTools.startsWithIgnoreCase(dft, I18N_KEY_STARTC)) {
     if (showError) {
       Print.logStackTrace(
           "Invalid/missing key definition!\n"
               + "Package: "
               + pkg
               + "\n"
               + "Key    : "
               + key
               + "\n"
               + "Default: "
               + dft);
     }
     return new I18N.Text(pkg, null, dft);
   } else {
     int ks = I18N_KEY_STARTE.length();
     int ke = dft.indexOf(I18N_KEY_END, ks);
     if (ke < ks) {
       return new I18N.Text(pkg, null, dft); // ']' is missing, return string as-is
     }
     String k = dft.substring(ks, ke).trim();
     String v = dft.substring(ke + I18N_KEY_END.length()).trim();
     return new I18N.Text(pkg, k, v);
   }
 }
Exemplo n.º 2
0
 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);
   }
 }
Exemplo n.º 3
0
 /**
  * ** Gets the Java Locale instance based on the specified locale name ** @param loc The name of
  * the Locale ** @param dft The default Locale returned ** @return The Java Locale instance
  */
 public static Locale getLocale(String loc, Locale dft) {
   String locale = !StringTools.isBlank(loc) ? loc : RTConfig.getString(RTKey.LOCALE, "");
   if (StringTools.isBlank(locale)) {
     return dft;
   } else {
     int p = locale.indexOf("_");
     try {
       if (p < 0) {
         String language = locale;
         return new Locale(language);
       } else {
         String language = locale.substring(0, p);
         String country = locale.substring(p + 1);
         return new Locale(language, country);
       }
     } catch (Throwable th) {
       return dft;
     }
   }
 }
Exemplo n.º 4
0
 /**
  * ** Gets the Localized value for the specified key. The default String text is returned ** if
  * the specified key does not exist ** @param key The LocalStrings key ** @param dft The default
  * String text to return if the LocalStrings key does not exist ** @return The Localized String
  * text
  */
 public String getString(String key, String dft) {
   if (!StringTools.isBlank(key) && (this.resBundle != null)) {
     RTProperties cfgProps = RTConfig.getConfigFileProperties();
     if (!cfgProps.hasProperty(key) || cfgProps.getBoolean(key, true)) {
       try {
         String s = this.resBundle.getString(key);
         if (s != null) {
           return I18N.decodeNewLine(s);
         }
       } catch (Throwable th) {
         // Print.logException("",th);
         // MissingResourceException - if no object for the given key can be found
         // ClassCastException - if the object found for the given key is not a string
       }
     }
   }
   return I18N.decodeNewLine(dft);
 }
Exemplo n.º 5
0
 public boolean hasKey() {
   return !StringTools.isBlank(this.getKey());
 }
Exemplo n.º 6
0
 public boolean hasPackage() {
   return !StringTools.isBlank(this.getPackage());
 }
Exemplo n.º 7
0
 /**
  * ** Converts "\\n" patterns into literal newlines (\n) ** @param s The StringBuffer to convert
  * "\\n" to "\n" ** @return The decoded StringBuffer
  */
 protected static StringBuffer decodeNewLine(StringBuffer s) {
   return StringTools.replace(s, "\\n", "\n");
 }