Пример #1
0
 /** Should be used only in DialogBox */
 public String getDialog(String id) {
   if (currentDialogs.containsKey(id)) {
     return currentDialogs.get(id);
   }
   if (defaultDialogs.containsKey(id)) {
     return defaultDialogs.get(id);
   }
   Logger.err("Specified dialog id doesnt exist: " + id, LangManager.class.getSimpleName());
   return "";
 }
Пример #2
0
  /**
   * Load the specified language from the languages directory
   *
   * @param languageCode - The language code ("en", "fr", "es"...)
   */
  @SuppressWarnings("unchecked")
  public void loadLanguage(String languageCode) {
    if (languageCode != null && !"".equals(languageCode)) {
      currentStrings.clear();
      currentDialogs.clear();

      Yaml yaml = new Yaml();
      java.util.Map<String, Object> resultMap = null;

      // Strings
      try {
        InputStream ios =
            new FileInputStream(new File(Constants.PATH_LANGUAGES + languageCode + "/strings.dat"));
        resultMap = (java.util.Map<String, Object>) yaml.load(ios);
        ios.close();
      } catch (Exception e) {
        Logger.err(
            "Error during opening "
                + Constants.PATH_LANGUAGES
                + languageCode
                + "/strings.dat file: "
                + e,
            LangManager.class.getSimpleName());
      }

      if (resultMap == null) {
        Logger.warn(
            Constants.PATH_LANGUAGES + languageCode + "/strings.dat is empty",
            LangManager.class.getSimpleName());
      } else if (!resultMap.containsKey("strings")) {
        Logger.warn(
            "Main key 'strings' not found in "
                + Constants.PATH_LANGUAGES
                + languageCode
                + "/strings.dat",
            LangManager.class.getSimpleName());
      } else {
        for (LinkedHashMap<String, Object> string :
            (ArrayList<LinkedHashMap<String, Object>>) resultMap.get("strings")) {
          if (Constants.DEFAULT_LANGUAGE.equals(languageCode)) {
            defaultStrings.put((String) string.get("id"), (String) string.get("text"));
          } else {
            currentStrings.put((String) string.get("id"), (String) string.get("text"));
          }
        }
      }

      // Dialogs
      try {
        InputStream ios =
            new FileInputStream(new File(Constants.PATH_LANGUAGES + languageCode + "/dialogs.dat"));
        resultMap = (java.util.Map<String, Object>) yaml.load(ios);
        ios.close();
      } catch (Exception e) {
        Logger.err(
            "Error during opening "
                + Constants.PATH_LANGUAGES
                + languageCode
                + "/dialogs.dat file: "
                + e,
            ResourceManager.class.getSimpleName());
      }

      if (resultMap == null) {
        Logger.warn(
            Constants.PATH_LANGUAGES + languageCode + "/dialogs.dat is empty",
            LangManager.class.getSimpleName());
      } else if (!resultMap.containsKey("dialogs")) {
        Logger.warn(
            "Main key 'dialogs' not found in "
                + Constants.PATH_LANGUAGES
                + languageCode
                + "/dialogs.dat",
            LangManager.class.getSimpleName());
      } else {
        for (LinkedHashMap<String, Object> string :
            (ArrayList<LinkedHashMap<String, Object>>) resultMap.get("dialogs")) {
          if (Constants.DEFAULT_LANGUAGE.equals(languageCode)) {
            defaultDialogs.put((String) string.get("id"), (String) string.get("text"));
          } else {
            currentDialogs.put((String) string.get("id"), (String) string.get("text"));
          }
        }
      }

      // Images
      if (!Constants.DEFAULT_LANGUAGE.equals(languageCode)) {
        ResourceManager.getInstance().loadLanguageImages(languageCode);
      }
    } else {
      Logger.info("Trying to load an empty language code.", LangManager.class.getSimpleName());
    }
  }