コード例 #1
0
ファイル: Framework.java プロジェクト: tripu/Unicorn
 private static void loadConfigFile(InputStream stream, String fileName, String[]... parameters)
     throws IOException {
   UCNProperties properties = new UCNProperties();
   logger.debug("Loading config file: " + fileName);
   for (String[] param : parameters) {
     if (param.length == 2 && param[1] != null) properties.put(param[0], param[1]);
   }
   properties.load(stream);
   properties.parse();
   unicornPropertiesFiles.put(fileName, properties);
   logger.debug("> " + fileName + ":" + properties);
 }
コード例 #2
0
ファイル: Framework.java プロジェクト: tripu/Unicorn
  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.");
    }
  }