Exemplo n.º 1
0
 public static void reset() {
   unicornPropertiesFiles = new Hashtable<String, UCNProperties>();
   languageContexts = new Hashtable<ULocale, VelocityContext>();
   languageProperties = new Hashtable<ULocale, UCNProperties>();
   metadataProperties = new Hashtable<ULocale, UCNProperties>();
   mapOfObserver = new LinkedHashMap<String, Observer>();
   responseImpl = new LinkedHashMap<String, Class<Response>>();
   Language.reset();
 }
Exemplo n.º 2
0
  public static void initLanguages() throws InitializationFailedException {

    // Loading language files
    logger.debug("-------------------------------------------------------");
    logger.debug(
        "Loading language files from language directory: "
            + Property.get("PATH_TO_LANGUAGE_FILES"));
    ULocale defaultLocale = null;
    for (ULocale locale : ULocale.getAvailableLocales()) {
      if (locale.getName().equals(Property.get("DEFAULT_LANGUAGE"))) {
        defaultLocale = locale;
        break;
      }
    }

    if (defaultLocale == null)
      throw new InitializationFailedException(
          "Locale not found for default language in unicorn.properties: "
              + Property.get("DEFAULT_LANGUAGE"));

    Language.reset();
    Language.initLocaleMatcher(defaultLocale);

    File defaultLanguageFile =
        new File(Property.get("PATH_TO_LANGUAGE_FILES", "DEFAULT_LANGUAGE") + ".properties");
    UCNProperties defaultProps = new UCNProperties();
    try {
      defaultProps = Language.load(defaultLanguageFile);
      logger.debug(
          "> Found language (default): "
              + defaultLocale.getName()
              + " - "
              + defaultLocale.getDisplayName(defaultLocale));
      Language.addUiLocale(defaultLocale);
      LanguageAction.addLanguageProperties(defaultLocale, defaultProps);
      defaultProps.parse();
      defaultProps.put("complete", "true");
      languageProperties.put(defaultLocale, defaultProps);
      UCNProperties metaProps = new UCNProperties();
      LanguageAction.addMetadatasProperties(defaultLocale, metaProps);
      metadataProperties.put(defaultLocale, metaProps);
    } catch (IllegalArgumentException e) {
      logger.warn(e.getMessage());
    } catch (FileNotFoundException e) {
      throw new InitializationFailedException(
          "Default language file does not exist: " + defaultLanguageFile.getPath());
    } catch (IOException e) {
      throw new InitializationFailedException(
          "Unable to read default language file " + defaultLanguageFile.getPath());
    }

    File[] languageFiles =
        ListFiles.listFiles(Property.get("PATH_TO_LANGUAGE_FILES"), "\\.properties$");

    for (File langFile : languageFiles) {
      if (langFile.equals(defaultLanguageFile)) continue;
      try {
        ULocale fileLocale = Language.getLocaleFromFileName(langFile.getName());
        if (fileLocale == Language.getDefaultLocale()) continue;
        UCNProperties props = Language.load(langFile);
        logger.debug(
            "> Found language: "
                + fileLocale.getName()
                + " - "
                + fileLocale.getDisplayName(Language.getDefaultLocale()));
        Language.addUiLocale(fileLocale);
        Language.clean(props, defaultProps, langFile.getName());
        LanguageAction.addLanguageProperties(fileLocale, props);
        Language.complete(props, defaultProps, langFile.getName());
        props.parse();
        languageProperties.put(fileLocale, props);
        UCNProperties metaProps = new UCNProperties();
        LanguageAction.addMetadatasProperties(fileLocale, metaProps);
        metadataProperties.put(fileLocale, metaProps);
      } catch (IllegalArgumentException e) {
        logger.warn(e.getMessage());
      } catch (FileNotFoundException e) {
        // Should not happen
        logger.error(e.getMessage(), e);
      } catch (IOException e) {
        logger.error("Unable to read language file " + langFile + ". This file will be skiped.");
      }
    }

    Language.initUILocaleMatcher();

    if (languageProperties.size() == 0) {
      throw new InitializationFailedException(
          "No language have been loaded. Check language files in: "
              + Property.get("PATH_TO_LANGUAGE_FILES"));
    } else {
      String s = "Language properties:";
      for (ULocale localeKey : languageProperties.keySet()) {
        Properties props = languageProperties.get(localeKey);
        s += "\n\n\t" + localeKey.getDisplayName(Language.getDefaultLocale()) + ":";
        for (Object langKey : props.keySet()) {
          s += "\n\t\t" + langKey + " => " + props.getProperty((String) langKey);
        }
      }
      logger.debug(s);
      logger.info("OK - " + languageProperties.size() + " language(s) successfully loaded.");
    }
  }