Ejemplo n.º 1
0
  public static boolean integratePluginMessages(ResourceBundle bundle) {
    synchronized (pluginResourceBundles) {
      pluginResourceBundles.add(bundle);
    }

    RESOURCE_BUNDLE.addResourceMessages(bundle);
    setResourceBundle(RESOURCE_BUNDLE);

    return true;
  }
Ejemplo n.º 2
0
  // TODO: This is slow. For every call, IntegratedResourceBundle creates
  //       a hashtables and fills it with the old resourceBundle, then adds
  //       the new one, and then puts it all back into a ListResourceBundle.
  //       As we get more plugins, the time to add a new plugin's language file
  //       increases dramatically (even if the language file only has 1 entry!)
  //       Fix this by:
  //         - Create only one IntegratedResourceBundle
  //         - extending ResourceBundle
  //         - override handleGetObject, store in hashtable
  //         - function to add another ResourceBundle, adds to hashtable
  public static boolean integratePluginMessages(String localizationPath, ClassLoader classLoader) {
    boolean integratedSuccessfully = false;
    if (null != localizationPath
        && localizationPath.length() != 0
        && !pluginLocalizationPaths.containsKey(localizationPath)) {
      synchronized (pluginLocalizationPaths) {
        pluginLocalizationPaths.put(localizationPath, classLoader);
      }

      RESOURCE_BUNDLE.addPluginBundle(localizationPath, classLoader);
      setResourceBundle(RESOURCE_BUNDLE);

      integratedSuccessfully = true;
    }
    return integratedSuccessfully;
  }
Ejemplo n.º 3
0
 // preload default language w/o plugins
 static {
   setResourceBundle(
       new IntegratedResourceBundle(
           getResourceBundle(BUNDLE_NAME, LOCALE_DEFAULT, MessageText.class.getClassLoader()),
           pluginLocalizationPaths));
 }
Ejemplo n.º 4
0
  private static boolean changeLocale(Locale newLocale, boolean force) {
    // set locale for startup (will override immediately it on locale change anyway)
    Locale.setDefault(newLocale);

    if (!isCurrentLocale(newLocale) || force) {
      Locale.setDefault(LOCALE_DEFAULT);
      ResourceBundle newResourceBundle = null;
      String bundleFolder = BUNDLE_NAME.replace('.', '/');
      final String prefix = BUNDLE_NAME.substring(BUNDLE_NAME.lastIndexOf('.') + 1);
      final String extension = ".properties";

      if (newLocale.equals(LOCALE_ENGLISH)) newLocale = LOCALE_DEFAULT;

      try {
        File userBundleFile = new File(SystemProperties.getUserPath());
        File appBundleFile = new File(SystemProperties.getApplicationPath());

        // Get the jarURL
        // XXX Is there a better way to get the JAR name?
        ClassLoader cl = MessageText.class.getClassLoader();
        String sJar = cl.getResource(bundleFolder + extension).toString();
        sJar = sJar.substring(0, sJar.length() - prefix.length() - extension.length());
        URL jarURL = new URL(sJar);

        // User dir overrides app dir which overrides jar file bundles
        URL[] urls = {userBundleFile.toURL(), appBundleFile.toURL(), jarURL};

        /* This is debugging code, use it when things go wrong :) The line number
         * is approximate as the input stream is buffered by the reader...

        {
        	LineNumberInputStream lnis	= null;

            try{
                ClassLoader fff = new URLClassLoader(urls);

                java.io.InputStream stream = fff.getResourceAsStream("MessagesBundle_th_TH.properties");

                lnis = new LineNumberInputStream( stream );

                new java.util.PropertyResourceBundle(lnis);
            }catch( Throwable e ){

            	System.out.println( lnis.getLineNumber());

            	e.printStackTrace();
            }
        }
        */

        newResourceBundle =
            getResourceBundle("MessagesBundle", newLocale, new URLClassLoader(urls));
        // do more searches if getBundle failed, or if the language is not the
        // same and the user wanted a specific country
        if ((!newResourceBundle.getLocale().getLanguage().equals(newLocale.getLanguage())
            && !newLocale.getCountry().equals(""))) {

          Locale foundLocale = newResourceBundle.getLocale();
          System.out.println(
              "changeLocale: "
                  + (foundLocale.toString().equals("")
                      ? "*Default Language*"
                      : foundLocale.getDisplayLanguage())
                  + " != "
                  + newLocale.getDisplayName()
                  + ". Searching without country..");
          // try it without the country
          Locale localeJustLang = new Locale(newLocale.getLanguage());
          newResourceBundle =
              getResourceBundle("MessagesBundle", localeJustLang, new URLClassLoader(urls));

          if (newResourceBundle == null
              || !newResourceBundle
                  .getLocale()
                  .getLanguage()
                  .equals(localeJustLang.getLanguage())) {
            // find first language we have in our list
            System.out.println(
                "changeLocale: Searching for language "
                    + newLocale.getDisplayLanguage()
                    + " in *any* country..");
            Locale[] locales = getLocales();
            for (int i = 0; i < locales.length; i++) {
              if (locales[i].getLanguage() == newLocale.getLanguage()) {
                newResourceBundle =
                    getResourceBundle("MessagesBundle", locales[i], new URLClassLoader(urls));
                break;
              }
            }
          }
        }
      } catch (MissingResourceException e) {
        System.out.println("changeLocale: no resource bundle for " + newLocale);
        Debug.printStackTrace(e);
        return false;
      } catch (Exception e) {
        Debug.printStackTrace(e);
      }

      if (newResourceBundle != null) {
        if (!newLocale.equals(LOCALE_DEFAULT) && !newResourceBundle.getLocale().equals(newLocale)) {
          String sNewLanguage = newResourceBundle.getLocale().getDisplayName();
          if (sNewLanguage == null || sNewLanguage.trim().equals(""))
            sNewLanguage = "English (default)";
          System.out.println(
              "changeLocale: no message properties for Locale '"
                  + newLocale.getDisplayName()
                  + "' ("
                  + newLocale
                  + "), using '"
                  + sNewLanguage
                  + "'");
        }
        newLocale = newResourceBundle.getLocale();
        Locale.setDefault(newLocale.equals(LOCALE_DEFAULT) ? LOCALE_ENGLISH : newLocale);
        LOCALE_CURRENT = newLocale;
        setResourceBundle(new IntegratedResourceBundle(newResourceBundle, pluginLocalizationPaths));
        if (newLocale.equals(LOCALE_DEFAULT)) DEFAULT_BUNDLE = RESOURCE_BUNDLE;
        return true;
      } else return false;
    }
    return false;
  }