diff --git a/ee3_common/ee3/common/EquivalentExchange3.java b/ee3_common/ee3/common/EquivalentExchange3.java index 30c14fe0..1c2b9319 100644 --- a/ee3_common/ee3/common/EquivalentExchange3.java +++ b/ee3_common/ee3/common/EquivalentExchange3.java @@ -23,6 +23,7 @@ import ee3.common.core.handlers.AddonHandler; import ee3.common.core.handlers.ConfigurationHandler; import ee3.common.core.handlers.EntityLivingHandler; import ee3.common.core.handlers.ItemPickupHandler; +import ee3.common.core.handlers.LocalizationHandler; import ee3.common.core.handlers.PacketHandler; import ee3.common.core.handlers.PlayerDestroyItemHandler; import ee3.common.core.handlers.VersionCheckTickHandler; @@ -72,6 +73,9 @@ public class EquivalentExchange3 { @Init public void load(FMLInitializationEvent event) { + // Load the localization files into the LanguageRegistry + LocalizationHandler.loadLanguages(); + // Initialize the custom item rarity types proxy.initCustomRarityTypes(); diff --git a/ee3_common/ee3/common/block/ModBlocks.java b/ee3_common/ee3/common/block/ModBlocks.java index 266eec60..57c3ba46 100644 --- a/ee3_common/ee3/common/block/ModBlocks.java +++ b/ee3_common/ee3/common/block/ModBlocks.java @@ -25,10 +25,6 @@ public class ModBlocks { redWaterStill = new BlockRedWaterStill(BlockIds.RED_WATER_STILL, Material.water); redWaterFlowing = new BlockRedWaterFlowing(BlockIds.RED_WATER_STILL - 1, Material.water); - LanguageRegistry.addName(calcinator, "Calcinator"); - LanguageRegistry.addName(redWaterStill, "Red Water (Still)"); - LanguageRegistry.addName(redWaterFlowing, "Red Water (Flowing)"); - GameRegistry.registerBlock(calcinator); GameRegistry.registerBlock(redWaterStill); GameRegistry.registerBlock(redWaterFlowing); diff --git a/ee3_common/ee3/common/core/handlers/LocalizationHandler.java b/ee3_common/ee3/common/core/handlers/LocalizationHandler.java index 0fb96e6e..cad96a32 100644 --- a/ee3_common/ee3/common/core/handlers/LocalizationHandler.java +++ b/ee3_common/ee3/common/core/handlers/LocalizationHandler.java @@ -1,5 +1,70 @@ package ee3.common.core.handlers; -public class LocalizationHandler { +import java.io.IOException; +import java.io.InputStream; +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; +/** + * LocalizationHandler + * + * Loads in all specified localizations for the mod + * + * @author pahimar + * @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html) + * + */ +public class LocalizationHandler { + + /*** + * Loads in all the localization files from the Localizations library class + */ + public static 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) { + languageStream = LocalizationHandler.class.getResourceAsStream(localizationFile); + + // 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 { + try { + if (languageStream != null) { + languageStream.close(); + } + } catch (IOException ex) { + ex.printStackTrace(System.err); + } + } + + } + } diff --git a/ee3_common/ee3/common/core/helper/LocalizationHelper.java b/ee3_common/ee3/common/core/helper/LocalizationHelper.java new file mode 100644 index 00000000..93228e1c --- /dev/null +++ b/ee3_common/ee3/common/core/helper/LocalizationHelper.java @@ -0,0 +1,73 @@ +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 + * + * Helper class for looking up localized strings in the Language Registry + * + * @author pahimar + * @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html) + * + */ +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 + * @return True if the file name represents a XML file, false otherwise + */ + public static boolean isXMLLanguageFile(String fileName) { + return fileName.endsWith(".xml"); + } + + /*** + * Returns the locale from file name + * @param fileName String representing the file name of the file in question + * @return String representation of the locale snipped from the file name + */ + public static String getLocaleFromFileName(String fileName) { + return fileName.substring(fileName.lastIndexOf('/') + 1, fileName.lastIndexOf('.')); + } + +} diff --git a/ee3_common/ee3/common/item/ModItems.java b/ee3_common/ee3/common/item/ModItems.java index 3e2a5e29..3d5feef6 100644 --- a/ee3_common/ee3/common/item/ModItems.java +++ b/ee3_common/ee3/common/item/ModItems.java @@ -34,9 +34,6 @@ public class ModItems { miniumStone.setContainerItem(miniumStone); philStone.setContainerItem(philStone); - - LanguageRegistry.addName(miniumShard, "Shard of Minium"); - LanguageRegistry.addName(miniumStone, "Minium Stone"); - LanguageRegistry.addName(philStone, "Philosophers Stone"); + } } diff --git a/ee3_common/ee3/common/lib/Localizations.java b/ee3_common/ee3/common/lib/Localizations.java new file mode 100644 index 00000000..00d6f520 --- /dev/null +++ b/ee3_common/ee3/common/lib/Localizations.java @@ -0,0 +1,20 @@ +package ee3.common.lib; + +/** + * BlockIds + * + * Library containing all mod language related files + * + * @author pahimar + * @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html) + * + */ +public class Localizations { + + private static final String LANG_RESOURCE_LOCATION = "/ee3/lang/"; + + public static String[] localeFiles = { + LANG_RESOURCE_LOCATION + "en_US.xml" + }; + +} diff --git a/ee3_common/ee3/common/lib/Reference.java b/ee3_common/ee3/common/lib/Reference.java index 3b4fff25..66e2b2ad 100644 --- a/ee3_common/ee3/common/lib/Reference.java +++ b/ee3_common/ee3/common/lib/Reference.java @@ -40,10 +40,11 @@ public class Reference { /* Texture related constants */ public static final String SPRITE_SHEET_LOCATION = "/ee3/art/sprites/"; + public static final String ARMOR_SHEET_LOCATION = "/ee3/art/armor/"; + public static final String GUI_SHEET_LOCATION = "/ee3/art/gui/"; public static final String ITEM_SPRITE_SHEET = "ee3_items.png"; public static final String BLOCK_SPRITE_SHEET = "ee3_blocks.png"; - public static final String GUI_SHEET_LOCATION = "/ee3/art/gui/"; - public static final String ARMOR_SHEET_LOCATION = "/ee3/art/armor/"; + public static final int SECOND_IN_TICKS = 20; diff --git a/resources/ee3/lang/en_US.lang b/resources/ee3/lang/en_US.lang deleted file mode 100644 index 687cbd28..00000000 --- a/resources/ee3/lang/en_US.lang +++ /dev/null @@ -1,5 +0,0 @@ -item.miniumShard=Minium Shard -item.miniumStone=Minium Stone -item.philosophersStone=Philosophers Stone -tile.redWaterStill=Red Water -tile.redWaterFlowing=Red Water \ No newline at end of file diff --git a/resources/ee3/lang/en_US.xml b/resources/ee3/lang/en_US.xml new file mode 100644 index 00000000..a860777e --- /dev/null +++ b/resources/ee3/lang/en_US.xml @@ -0,0 +1,15 @@ + + + + English (US) Localization File + Shard of Minium + Minium Stone + Philosophers Stone + Red Water (Still) + Red Water (Flowing) + Calcinator + Uninitialized + Current + Outdated + Connection Error + \ No newline at end of file