From ac4b4953017c168c7195c5c37a799ad9c6dbbbfa Mon Sep 17 00:00:00 2001 From: Tim Ziankoski Date: Sat, 7 Mar 2015 20:50:47 -0500 Subject: [PATCH] Created internationalization helper class and began i18n process --- .../artifacts/Nautilus_Game_Arcade_jar.xml | 2 +- .../artifacts/Nautilus_Game_Arcade_test.xml | 1 + Plugins/.idea/copyright/profiles_settings.xml | 3 + .../Mineplex.Core.Common.iml | 4 +- .../core/common/lang/mineplex_de.properties | 1 + .../core/common/lang/mineplex_en.properties | 4 + .../src/mineplex/core/common/lang/Lang.java | 115 ++++++++++++++++++ .../arcade/managers/GameLobbyManager.java | 12 +- 8 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 Plugins/.idea/copyright/profiles_settings.xml create mode 100644 Plugins/Mineplex.Core.Common/resources/mineplex/core/common/lang/mineplex_de.properties create mode 100644 Plugins/Mineplex.Core.Common/resources/mineplex/core/common/lang/mineplex_en.properties create mode 100644 Plugins/Mineplex.Core.Common/src/mineplex/core/common/lang/Lang.java diff --git a/Plugins/.idea/artifacts/Nautilus_Game_Arcade_jar.xml b/Plugins/.idea/artifacts/Nautilus_Game_Arcade_jar.xml index cd4447b88..3f5cae6e4 100644 --- a/Plugins/.idea/artifacts/Nautilus_Game_Arcade_jar.xml +++ b/Plugins/.idea/artifacts/Nautilus_Game_Arcade_jar.xml @@ -17,7 +17,7 @@ - + \ No newline at end of file diff --git a/Plugins/.idea/artifacts/Nautilus_Game_Arcade_test.xml b/Plugins/.idea/artifacts/Nautilus_Game_Arcade_test.xml index 8b3142e4c..73139df16 100644 --- a/Plugins/.idea/artifacts/Nautilus_Game_Arcade_test.xml +++ b/Plugins/.idea/artifacts/Nautilus_Game_Arcade_test.xml @@ -3,6 +3,7 @@ $PROJECT_DIR$/../Testing/Arcade/plugins + \ No newline at end of file diff --git a/Plugins/.idea/copyright/profiles_settings.xml b/Plugins/.idea/copyright/profiles_settings.xml new file mode 100644 index 000000000..e7bedf337 --- /dev/null +++ b/Plugins/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/Plugins/Mineplex.Core.Common/Mineplex.Core.Common.iml b/Plugins/Mineplex.Core.Common/Mineplex.Core.Common.iml index b59695f8a..c145ccad3 100644 --- a/Plugins/Mineplex.Core.Common/Mineplex.Core.Common.iml +++ b/Plugins/Mineplex.Core.Common/Mineplex.Core.Common.iml @@ -4,10 +4,10 @@ + - - + \ No newline at end of file diff --git a/Plugins/Mineplex.Core.Common/resources/mineplex/core/common/lang/mineplex_de.properties b/Plugins/Mineplex.Core.Common/resources/mineplex/core/common/lang/mineplex_de.properties new file mode 100644 index 000000000..cb380f81e --- /dev/null +++ b/Plugins/Mineplex.Core.Common/resources/mineplex/core/common/lang/mineplex_de.properties @@ -0,0 +1 @@ +waiting.for.players=Warten auf Spieler diff --git a/Plugins/Mineplex.Core.Common/resources/mineplex/core/common/lang/mineplex_en.properties b/Plugins/Mineplex.Core.Common/resources/mineplex/core/common/lang/mineplex_en.properties new file mode 100644 index 000000000..d99ecb4b3 --- /dev/null +++ b/Plugins/Mineplex.Core.Common/resources/mineplex/core/common/lang/mineplex_en.properties @@ -0,0 +1,4 @@ +waiting.for.players=Waiting for Players +lobby=Lobby +starting.in.0.seconds=Starting in {0} Seconds +starting.in.0.seconds.singular=Starting in {0} Second diff --git a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/lang/Lang.java b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/lang/Lang.java new file mode 100644 index 000000000..cb6e3fa89 --- /dev/null +++ b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/lang/Lang.java @@ -0,0 +1,115 @@ +package mineplex.core.common.lang; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import java.util.MissingResourceException; +import java.util.ResourceBundle; +import java.util.WeakHashMap; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +public final class Lang +{ + private static final String RESOURCE_BUNDLE_PATH = "mineplex/core/common/lang/mineplex"; + + private static final Map _playerLocales = new WeakHashMap<>(); + private static final ResourceBundle _defaultResourceBundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_PATH); + private static final Map _localeResourceBundles = Collections.synchronizedMap(new HashMap()); + + private Lang() + { + + } + + public static void setPlayerLocale(Player player, Locale locale) + { + _playerLocales.put(player, locale); + } + + public static ResourceBundle getDefaultResourceBundle() + { + return _defaultResourceBundle; + } + + public static ResourceBundle getResourceBundle(Locale locale) + { + ResourceBundle bundle = getDefaultResourceBundle(); + + synchronized (_localeResourceBundles) + { + if (_localeResourceBundles.containsKey(locale)) + { + ResourceBundle b = _localeResourceBundles.get(locale); + if (b != null) + bundle = b; + } + else + { + try + { + bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_PATH, locale); + _localeResourceBundles.put(locale, bundle); + } + catch (MissingResourceException e) + { + _localeResourceBundles.put(locale, null); + + Bukkit.getLogger().warning(e.getMessage()); + } + } + } + + return bundle; + } + + public static String getString(String key, int count) + { + return getString(key, (Locale) null, count); + } + + public static String getString(String key, Locale locale, int count) + { + if (key == null) + return null; + else + { + ResourceBundle bundle; + if (locale == null) + bundle = getDefaultResourceBundle(); + else + bundle = getResourceBundle(locale); + + if (count == 1) + { + String singularKey = key + ".singular"; + if (bundle.containsKey(singularKey)) + return bundle.getString(singularKey); + } + + return bundle.getString(key); + } + } + + public static String getString(String key, Player player, int count) + { + return getString(key, _playerLocales.get(player), count); + } + + public static String getString(String key) + { + return getString(key, 0); + } + + public static String getString(String key, Locale locale) + { + return getString(key, locale, 0); + } + + public static String getString(String key, Player player) + { + return getString(key, player, 0); + } +} diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/GameLobbyManager.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/GameLobbyManager.java index fe3a858b4..688eaa4b8 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/GameLobbyManager.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/GameLobbyManager.java @@ -1,10 +1,11 @@ package nautilus.game.arcade.managers; -import java.util.ArrayList; +import java.text.MessageFormat;import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map.Entry; import org.bukkit.Bukkit; @@ -41,6 +42,7 @@ import net.minecraft.server.v1_7_R4.WatchableObject; import mineplex.core.account.CoreClient; import mineplex.core.common.Rank; +import mineplex.core.common.lang.Lang; import mineplex.core.common.util.C; import mineplex.core.common.util.F; import mineplex.core.common.util.MapUtil; @@ -948,18 +950,18 @@ public class GameLobbyManager implements Listener, IPacketHandler for (Entry entry : _scoreboardMap.entrySet()) { - Objective objective = entry.getValue().getObjective("§l" + "Lobby"); - + Objective objective = entry.getValue().getObjective(C.Bold + Lang.getString("lobby", entry.getKey())); + if (Manager.GetGame() != null && Manager.GetGame().GetCountdown() >= 0) { if (Manager.GetGame().GetCountdown() > 0) - objective.setDisplayName(C.Bold + "§lStarting in " + C.cGreen + "§l" + Manager.GetGame().GetCountdown() + (Manager.GetGame().GetCountdown() == 1 ? " Second" : " Seconds")); + objective.setDisplayName(MessageFormat.format(C.Bold + Lang.getString("starting.in.0.seconds", entry.getKey(), Manager.GetGame().GetCountdown()), C.cGreen + C.Bold + Manager.GetGame().GetCountdown())); else if (Manager.GetGame().GetCountdown() == 0) objective.setDisplayName(ChatColor.WHITE + "§lIn Progress..."); } else { - objective.setDisplayName(ChatColor.GREEN + "§l" + "Waiting for Players"); + objective.setDisplayName(C.cGreen + C.Bold + Lang.getString("waiting.for.players", entry.getKey())); } int line = 15;