From 1cbfb94d8688df7c649594749e9a69d8480c5452 Mon Sep 17 00:00:00 2001 From: pahimar Date: Sat, 29 Sep 2012 21:48:44 -0400 Subject: [PATCH] Simplify the loading of localization files now that our localization PR has been merged into FML and Forge --- .../ee3/common/EquivalentExchange3.java | 6 +- .../core/handlers/LocalizationHandler.java | 60 ++----------------- .../core/helper/LocalizationHelper.java | 41 ------------- .../ee3/common/core/helper/VersionHelper.java | 9 +-- 4 files changed, 12 insertions(+), 104 deletions(-) diff --git a/ee3_common/ee3/common/EquivalentExchange3.java b/ee3_common/ee3/common/EquivalentExchange3.java index 5f1a1b16..17f377fa 100644 --- a/ee3_common/ee3/common/EquivalentExchange3.java +++ b/ee3_common/ee3/common/EquivalentExchange3.java @@ -53,11 +53,11 @@ public class EquivalentExchange3 { @PreInit public void preInit(FMLPreInitializationEvent event) { + // Load the localization files into the LanguageRegistry + LocalizationHandler.loadLanguages(); + // Initialize the configuration ConfigurationHandler.init(event.getSuggestedConfigurationFile()); - - // Load the localization files into the LanguageRegistry - LocalizationHandler.instance().loadLanguages(); // Conduct the version check and log the result VersionHelper.checkVersion(); diff --git a/ee3_common/ee3/common/core/handlers/LocalizationHandler.java b/ee3_common/ee3/common/core/handlers/LocalizationHandler.java index 13eb2438..ca8fbe8b 100644 --- a/ee3_common/ee3/common/core/handlers/LocalizationHandler.java +++ b/ee3_common/ee3/common/core/handlers/LocalizationHandler.java @@ -1,10 +1,5 @@ package ee3.common.core.handlers; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.Iterator; -import java.util.Properties; import cpw.mods.fml.common.registry.LanguageRegistry; import ee3.common.core.helper.LocalizationHelper; import ee3.common.lib.Localizations; @@ -20,61 +15,14 @@ import ee3.common.lib.Localizations; */ public class LocalizationHandler { - private static final LocalizationHandler INSTANCE = new LocalizationHandler(); - - public static LocalizationHandler instance() { - return INSTANCE; - } - /*** * Loads in all the localization files from the Localizations library class */ - public void loadLanguages() { - InputStream languageStream = null; - Properties languageMappings = new Properties(); - Iterator keyIter = null; - String currentKey, currentLang; - - try { - // For every file specified in the Localization library class, load them into the Language Registry - for (String localizationFile : Localizations.localeFiles) { - URL localizationFileURL = this.getClass().getResource(localizationFile); - - languageStream = localizationFileURL.openStream(); - - // If this file is a XML file, load it from XML - if (LocalizationHelper.isXMLLanguageFile(localizationFile)) { - languageMappings.loadFromXML(languageStream); - } - // Otherwise, load it like any other Java Properties file - else { - languageMappings.load(languageStream); - } - - // Read the locale from the file name of the localization file - currentLang = LocalizationHelper.getLocaleFromFileName(localizationFile); - - // For every key in the localization file, add its key:value pair to the Language Registry for the given locale - keyIter = (Iterator)languageMappings.keys(); - while (keyIter.hasNext()) { - currentKey = keyIter.next(); - LanguageRegistry.instance().addStringLocalization(currentKey, currentLang, languageMappings.getProperty(currentKey)); - } - } - - } catch (Exception e) { - e.printStackTrace(System.err); - } finally { - // Close the input stream when we are done with it - try { - if (languageStream != null) { - languageStream.close(); - } - } catch (IOException ex) { - ex.printStackTrace(System.err); - } + public static void loadLanguages() { + // For every file specified in the Localization library class, load them into the Language Registry + for (String localizationFile : Localizations.localeFiles) { + LanguageRegistry.instance().loadLocalization(localizationFile, LocalizationHelper.getLocaleFromFileName(localizationFile), LocalizationHelper.isXMLLanguageFile(localizationFile)); } - } } diff --git a/ee3_common/ee3/common/core/helper/LocalizationHelper.java b/ee3_common/ee3/common/core/helper/LocalizationHelper.java index 93228e1c..35e064af 100644 --- a/ee3_common/ee3/common/core/helper/LocalizationHelper.java +++ b/ee3_common/ee3/common/core/helper/LocalizationHelper.java @@ -1,12 +1,5 @@ package ee3.common.core.helper; -import java.util.HashMap; -import java.util.Properties; -import cpw.mods.fml.common.registry.LanguageRegistry; -import cpw.mods.fml.relauncher.ReflectionHelper; -import net.minecraft.src.StringTranslate; -import ee3.common.core.handlers.LocalizationHandler; - /** * LocalizationHelper * @@ -18,40 +11,6 @@ import ee3.common.core.handlers.LocalizationHandler; */ public class LocalizationHelper { - // The language data field name for localization data in the Language Registry - private static final String LANGUAGE_REGISTRY_LANGUAGE_DATA_FIELD = "modLanguageData"; - - /*** - * Returns the localized version of the text represented by key for the current language from the Language Registry - * @param key The key that represents the text we are attempting to localize - * @return The localized string for the specified key for the current language, null if no localized version of the key exists in the Language Registry - */ - public static String localize(String key) { - return localize(StringTranslate.getInstance().getCurrentLanguage(), key); - } - - /*** - * Returns the localized version of the text represented by key for the specified language from the Language Registry - * @param language The language for which to search for the localized version of the key - * @param key The key that represents the text we are attempting to localize - * @return The localized string for the specified key for the specified language, null if no localized version of the key exists in the Language Registry - */ - public static String localize(String language, String key) { - String localizedValue = ""; - HashMap modLanguageData = null; - Properties languageMapping = null; - - try { - modLanguageData = ReflectionHelper.getPrivateValue(cpw.mods.fml.common.registry.LanguageRegistry.class, LanguageRegistry.instance(), LANGUAGE_REGISTRY_LANGUAGE_DATA_FIELD); - languageMapping = modLanguageData.get(language); - localizedValue = languageMapping.getProperty(key); - } catch (Exception e) { - e.printStackTrace(System.err); - } - - return localizedValue; - } - /*** * Simple test to determine if a specified file name represents a XML file or not * @param fileName String representing the file name of the file in question diff --git a/ee3_common/ee3/common/core/helper/VersionHelper.java b/ee3_common/ee3/common/core/helper/VersionHelper.java index b8bdee29..b12dd08b 100644 --- a/ee3_common/ee3/common/core/helper/VersionHelper.java +++ b/ee3_common/ee3/common/core/helper/VersionHelper.java @@ -7,6 +7,7 @@ import java.util.logging.Level; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Loader; +import cpw.mods.fml.common.registry.LanguageRegistry; import ee3.common.lib.Reference; /** @@ -97,16 +98,16 @@ public class VersionHelper { public static String getResultMessage() { if (result == UNINITIALIZED) { - return LocalizationHelper.localize(UNINITIALIZED_MESSAGE); + return LanguageRegistry.instance().getStringLocalization(UNINITIALIZED_MESSAGE); } else if (result == CURRENT) { - return LocalizationHelper.localize(CURRENT_MESSAGE); + return LanguageRegistry.instance().getStringLocalization(CURRENT_MESSAGE); } else if (result == OUTDATED) { - return LocalizationHelper.localize(OUTDATED_MESSAGE); + return LanguageRegistry.instance().getStringLocalization(OUTDATED_MESSAGE); } else if (result == CONNECTION_ERROR) { - return LocalizationHelper.localize(CONNECTION_ERROR_MESSAGE); + return LanguageRegistry.instance().getStringLocalization(CONNECTION_ERROR_MESSAGE); } else { return null;