Simplify the loading of localization files now that our localization PR has been merged into FML and Forge

This commit is contained in:
pahimar 2012-09-29 21:48:44 -04:00
parent 6d9ce5367b
commit 1cbfb94d86
4 changed files with 12 additions and 104 deletions

View file

@ -53,12 +53,12 @@ public class EquivalentExchange3 {
@PreInit @PreInit
public void preInit(FMLPreInitializationEvent event) { public void preInit(FMLPreInitializationEvent event) {
// Load the localization files into the LanguageRegistry
LocalizationHandler.loadLanguages();
// Initialize the configuration // Initialize the configuration
ConfigurationHandler.init(event.getSuggestedConfigurationFile()); ConfigurationHandler.init(event.getSuggestedConfigurationFile());
// Load the localization files into the LanguageRegistry
LocalizationHandler.instance().loadLanguages();
// Conduct the version check and log the result // Conduct the version check and log the result
VersionHelper.checkVersion(); VersionHelper.checkVersion();
VersionHelper.logResult(); VersionHelper.logResult();

View file

@ -1,10 +1,5 @@
package ee3.common.core.handlers; 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 cpw.mods.fml.common.registry.LanguageRegistry;
import ee3.common.core.helper.LocalizationHelper; import ee3.common.core.helper.LocalizationHelper;
import ee3.common.lib.Localizations; import ee3.common.lib.Localizations;
@ -20,61 +15,14 @@ import ee3.common.lib.Localizations;
*/ */
public class LocalizationHandler { 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 * Loads in all the localization files from the Localizations library class
*/ */
public void loadLanguages() { public static void loadLanguages() {
InputStream languageStream = null;
Properties languageMappings = new Properties();
Iterator<String> keyIter = null;
String currentKey, currentLang;
try {
// For every file specified in the Localization library class, load them into the Language Registry // For every file specified in the Localization library class, load them into the Language Registry
for (String localizationFile : Localizations.localeFiles) { for (String localizationFile : Localizations.localeFiles) {
URL localizationFileURL = this.getClass().getResource(localizationFile); LanguageRegistry.instance().loadLocalization(localizationFile, LocalizationHelper.getLocaleFromFileName(localizationFile), LocalizationHelper.isXMLLanguageFile(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<String>)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);
} }
} }
} }
}

View file

@ -1,12 +1,5 @@
package ee3.common.core.helper; 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 * LocalizationHelper
* *
@ -18,40 +11,6 @@ import ee3.common.core.handlers.LocalizationHandler;
*/ */
public class LocalizationHelper { 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<String,Properties> 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 * 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 * @param fileName String representing the file name of the file in question

View file

@ -7,6 +7,7 @@ import java.util.logging.Level;
import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.registry.LanguageRegistry;
import ee3.common.lib.Reference; import ee3.common.lib.Reference;
/** /**
@ -97,16 +98,16 @@ public class VersionHelper {
public static String getResultMessage() { public static String getResultMessage() {
if (result == UNINITIALIZED) { if (result == UNINITIALIZED) {
return LocalizationHelper.localize(UNINITIALIZED_MESSAGE); return LanguageRegistry.instance().getStringLocalization(UNINITIALIZED_MESSAGE);
} }
else if (result == CURRENT) { else if (result == CURRENT) {
return LocalizationHelper.localize(CURRENT_MESSAGE); return LanguageRegistry.instance().getStringLocalization(CURRENT_MESSAGE);
} }
else if (result == OUTDATED) { else if (result == OUTDATED) {
return LocalizationHelper.localize(OUTDATED_MESSAGE); return LanguageRegistry.instance().getStringLocalization(OUTDATED_MESSAGE);
} }
else if (result == CONNECTION_ERROR) { else if (result == CONNECTION_ERROR) {
return LocalizationHelper.localize(CONNECTION_ERROR_MESSAGE); return LanguageRegistry.instance().getStringLocalization(CONNECTION_ERROR_MESSAGE);
} }
else { else {
return null; return null;