[jetrix-cvs] SF.net SVN: jetrix:[846] jetrix/trunk
Brought to you by:
smanux
From: <sm...@us...> - 2010-05-03 15:29:50
|
Revision: 846 http://jetrix.svn.sourceforge.net/jetrix/?rev=846&view=rev Author: smanux Date: 2010-05-03 15:29:44 +0000 (Mon, 03 May 2010) Log Message: ----------- Modularization of the resource bundles Modified Paths: -------------- jetrix/trunk/build.xml jetrix/trunk/pom.xml jetrix/trunk/src/java/net/jetrix/Language.java jetrix/trunk/src/java/net/jetrix/commands/ModeCommand.java jetrix/trunk/src/lang/jetrix_en.properties jetrix/trunk/src/lang/jetrix_fr.properties jetrix/trunk/src/lang/jetrix_nl.properties jetrix/trunk/src/lang/jetrix_pt.properties jetrix/trunk/src/test/net/jetrix/LanguageTest.java Added Paths: ----------- jetrix/trunk/src/lang/command/ jetrix/trunk/src/lang/command/mode_en.properties jetrix/trunk/src/lang/command/mode_fr.properties jetrix/trunk/src/lang/command/mode_nl.properties jetrix/trunk/src/lang/command/mode_pt.properties Removed Paths: ------------- jetrix/trunk/src/lang/jetrix.properties Modified: jetrix/trunk/build.xml =================================================================== --- jetrix/trunk/build.xml 2010-05-03 15:12:26 UTC (rev 845) +++ jetrix/trunk/build.xml 2010-05-03 15:29:44 UTC (rev 846) @@ -186,7 +186,7 @@ <include name="lib/jetrix-launcher-${version}.jar" /> </fileset> <fileset dir="${src}"> - <include name="lang/*.properties" /> + <include name="lang/**/*.properties" /> </fileset> </copy> <copy todir="${build}/dist/lib"> Modified: jetrix/trunk/pom.xml =================================================================== --- jetrix/trunk/pom.xml 2010-05-03 15:12:26 UTC (rev 845) +++ jetrix/trunk/pom.xml 2010-05-03 15:29:44 UTC (rev 846) @@ -173,7 +173,7 @@ <directory>src/lang</directory> <targetPath>./</targetPath> <includes> - <include>*.properties</include> + <include>**/*.properties</include> </includes> </testResource> <testResource> Modified: jetrix/trunk/src/java/net/jetrix/Language.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/Language.java 2010-05-03 15:12:26 UTC (rev 845) +++ jetrix/trunk/src/java/net/jetrix/Language.java 2010-05-03 15:29:44 UTC (rev 846) @@ -1,6 +1,6 @@ /** * Jetrix TetriNET Server - * Copyright (C) 2001-2004 Emmanuel Bourg + * Copyright (C) 2001-2004,2010 Emmanuel Bourg * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -30,13 +30,19 @@ */ public class Language { + /** The names of the resource bundles imported. */ + private Set<String> bundleNames = new HashSet<String>(); + + private Map<Locale, MultiResourceBundle> bundles = new HashMap<Locale, MultiResourceBundle>(); + private static Language instance = new Language(); - private Map<Locale,ResourceBundle> bundles; - private static String resource = "jetrix"; + /** The default resource bundle containing the server messages. */ + private static final String DEFAULT_RESOURCE = "jetrix"; + private Language() { - bundles = new HashMap<Locale, ResourceBundle>(); + addResources(DEFAULT_RESOURCE); } /** @@ -48,29 +54,30 @@ } /** + * Register an extra set of localized messages. + * + * @param name the base name of the resource bundle + * @since 0.3 + */ + public void addResources(String name) + { + bundleNames.add(name); + } + + /** * Load and return the <tt>ResourceBundle</tt> for the specified locale. * Bundles are cached in a local Map. * * @param locale the locale of the returned bundle if available */ - public ResourceBundle load(Locale locale) + protected ResourceBundle load(Locale locale) { - ResourceBundle bundle = PropertyResourceBundle.getBundle(resource, locale); + MultiResourceBundle bundle = new MultiResourceBundle(locale); bundles.put(locale, bundle); return bundle; } /** - * Return the <tt>ResourceBundle</tt> for the specified locale. - * - * @param locale the locale of the bundle to return - */ - public ResourceBundle getResourceBundle(Locale locale) - { - return bundles.get(locale); - } - - /** * Tell if the specified locale has a corresponding resource file available. * * @param locale the locale to test @@ -79,12 +86,12 @@ */ public static boolean isSupported(Locale locale) { - ResourceBundle bundle = instance.getResourceBundle(locale); + MultiResourceBundle bundle = instance.bundles.get(locale); if (bundle == null) { - bundle = PropertyResourceBundle.getBundle(resource, locale); + bundle = instance.new MultiResourceBundle(locale); } - return (bundle != null && bundle.getLocale().equals(locale)); + return bundle.isSupported(); } /** @@ -116,7 +123,7 @@ { try { - ResourceBundle bundle = instance.getResourceBundle(locale); + ResourceBundle bundle = instance.bundles.get(locale); if (bundle == null) { bundle = instance.load(locale); @@ -145,18 +152,99 @@ // localize the arguments Object[] arguments2 = new Object[arguments.length]; for (int i = 0; i < arguments.length; i++) + { + arguments2[i] = getLocalizedArgument(locale, arguments[i]); + } + + return MessageFormat.format(getText(key, locale), arguments2); + } + + /** + * Transforms a localized argument into its actual value. Localized + * arguments start with the "key:" prefix and refers to another message + * in the resource bundle. + * + * @since 0.3 + * + * @param locale the target locale + * @param argument the argument to transform + */ + private static Object getLocalizedArgument(Locale locale, Object argument) + { + if (argument instanceof String && ((String) argument).startsWith("key:")) { - if (arguments[i] instanceof String && ((String) arguments[i]).startsWith("key:")) + return getText(((String) argument).substring(4), locale); + } + else + { + return argument; + } + } + + /** + * A resource bundle merging several property based resource bundles. + * + * @since 0.3 + */ + private class MultiResourceBundle extends ResourceBundle + { + private Locale locale; + + private MultiResourceBundle(Locale locale) + { + this.locale = locale; + } + + private PropertyResourceBundle getPropertyResourceBundle(String name) { + try { - arguments2[i] = getText(((String) arguments[i]).substring(4), locale); + return (PropertyResourceBundle) PropertyResourceBundle.getBundle(name, locale); } - else + catch (MissingResourceException e) { - arguments2[i] = arguments[i]; + return null; } } - return MessageFormat.format(getText(key, locale), arguments2); + protected Object handleGetObject(String key) + { + for (String name : bundleNames) + { + PropertyResourceBundle bundle = getPropertyResourceBundle(name); + if (bundle != null) + { + Object value = bundle.handleGetObject(key); + if (value != null) + { + return value; + } + } + } + + return null; + } + + public Enumeration<String> getKeys() + { + return null; + } + + /** + * Checks if at least one of the underlying resource bundles supports + * the locale assigned to this bundle. + */ + public boolean isSupported() + { + for (String name : bundleNames) + { + PropertyResourceBundle bundle = getPropertyResourceBundle(name); + if (bundle != null && bundle.getLocale().equals(locale)) + { + return true; + } + } + + return false; + } } - } Modified: jetrix/trunk/src/java/net/jetrix/commands/ModeCommand.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/commands/ModeCommand.java 2010-05-03 15:12:26 UTC (rev 845) +++ jetrix/trunk/src/java/net/jetrix/commands/ModeCommand.java 2010-05-03 15:29:44 UTC (rev 846) @@ -50,6 +50,11 @@ {0, 0, 0, 0, 0, 0, 100,1, 1} }; + static + { + Language.getInstance().addResources("command.mode"); + } + public String getAlias() { return "mode"; Added: jetrix/trunk/src/lang/command/mode_en.properties =================================================================== --- jetrix/trunk/src/lang/command/mode_en.properties (rev 0) +++ jetrix/trunk/src/lang/command/mode_en.properties 2010-05-03 15:29:44 UTC (rev 846) @@ -0,0 +1,18 @@ +# +# English resource bundle for the /mode command in Jetrix +# +# @author Emmanuel Bourg +# @version $Revision$, $Date$ + +command.mode.description=Change the channel's configuration. +command.mode.message0=PURE 15% sticks +command.mode.message1=COOKIES 1:1 (1 line cleared yields one special block) +command.mode.message2=PURE 20% sticks +command.mode.message3=COOKIE 2:1 (2 lines cleared yield one special block) +command.mode.message4=PURE 25% sticks +command.mode.message5=NO STICKS (0% stick) +command.mode.message6=ALL STICKS (100% stick) +command.mode.message7=COOKIES 1:1 with Z blocks only +command.mode.message8=COOKIES 1:1 with L blocks only +command.mode.message9=COOKIES 1:1 with T blocks only +command.mode.enabled=<gray>Configuration changed to <b>{0}</b> Property changes on: jetrix/trunk/src/lang/command/mode_en.properties ___________________________________________________________________ Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Added: jetrix/trunk/src/lang/command/mode_fr.properties =================================================================== --- jetrix/trunk/src/lang/command/mode_fr.properties (rev 0) +++ jetrix/trunk/src/lang/command/mode_fr.properties 2010-05-03 15:29:44 UTC (rev 846) @@ -0,0 +1,18 @@ +# +# French resource bundle for the /mode command in Jetrix +# +# @author Emmanuel Bourg +# @version $Revision$, $Date$ + +command.mode.description=Change la configuration du channel. +command.mode.message0=PURE 15% de lignes +command.mode.message1=COOKIES 1:1 (1 ligne effac\xE9e donne 1 bloc special) +command.mode.message2=PURE 20% de lignes +command.mode.message3=COOKIE 2:1 (2 lignes effac\xE9es donnent 1 bloc sp\xE9cial) +command.mode.message4=PURE 25% de lignes +command.mode.message5=SANS LIGNES (0% lignes) +command.mode.message6=TOUT LIGNES (100% lignes) +command.mode.message7=COOKIES 1:1 avec seulement des blocs Z +command.mode.message8=COOKIES 1:1 avec seulement des blocs L +command.mode.message9=COOKIES 1:1 avec seulement des blocs T +command.mode.enabled=<gray>Configuration chang\xE9e en <b>{0}</b> Property changes on: jetrix/trunk/src/lang/command/mode_fr.properties ___________________________________________________________________ Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Added: jetrix/trunk/src/lang/command/mode_nl.properties =================================================================== --- jetrix/trunk/src/lang/command/mode_nl.properties (rev 0) +++ jetrix/trunk/src/lang/command/mode_nl.properties 2010-05-03 15:29:44 UTC (rev 846) @@ -0,0 +1,19 @@ +# +# Dutch resource bundle for the /mode command in Jetrix +# +# @author Tim Van Wassenhove +# @author "Sobi" (so...@te...) +# @version $Revision$, $Date$ + +command.mode.description=Verander de configuratie van het kanaal. +command.mode.message0=PURE 15% sticks +command.mode.message1=COOKIES 1:1 (voor elke verwijderde regel krijg je 1 "special block") +command.mode.message2=PURE 20% sticks +command.mode.message3=COOKIE 2:1 (waneer je 2 regels verwijdert krijg je 1 "special block") +command.mode.message4=PURE 25% sticks +command.mode.message5=NO STICKS (0% stick) +command.mode.message6=ALL STICKS (100% stick) +command.mode.message7=COOKIES 1:1 alleen Z blocks +command.mode.message8=COOKIES 1:1 alleen L blocks +command.mode.message9=COOKIES 1:1 alleen T blocks +command.mode.enabled=<gray>Configuratie is veranderd naar <b>{0}</b> Property changes on: jetrix/trunk/src/lang/command/mode_nl.properties ___________________________________________________________________ Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Added: jetrix/trunk/src/lang/command/mode_pt.properties =================================================================== --- jetrix/trunk/src/lang/command/mode_pt.properties (rev 0) +++ jetrix/trunk/src/lang/command/mode_pt.properties 2010-05-03 15:29:44 UTC (rev 846) @@ -0,0 +1,20 @@ +# +# Portuguese resource bundle for the /mode command in Jetrix +# +# @author V\xEDtor Melo +# @author "Scooter" (Tetridome player) +# @author "Luminoso" <lum...@gm...> +# @version $Revision$, $Date$ + +command.mode.description=Mude as configura\xE7\xF5es do canal. +command.mode.message0=PURE 15% sticks +command.mode.message1=COOKIES 1:1 (1 linha feita 1 special block) +command.mode.message2=PURE 20% sticks +command.mode.message3=COOKIE 2:1 (2 linhas feitas 1 special block) +command.mode.message4=PURE 25% sticks +command.mode.message5=SEM STICKS (0% stick) +command.mode.message6=S\xD3 STICKS (100% stick) +command.mode.message7=COOKIES 1:1 s\xF3 blocos Z +command.mode.message8=COOKIES 1:1 s\xF3 blocos L +command.mode.message9=COOKIES 1:1 s\xF3 blocos T +command.mode.enabled=<gray>Configura\xE7\xF5es mudadas para <b>{0}</b> Property changes on: jetrix/trunk/src/lang/command/mode_pt.properties ___________________________________________________________________ Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Deleted: jetrix/trunk/src/lang/jetrix.properties =================================================================== --- jetrix/trunk/src/lang/jetrix.properties 2010-05-03 15:12:26 UTC (rev 845) +++ jetrix/trunk/src/lang/jetrix.properties 2010-05-03 15:29:44 UTC (rev 846) @@ -1,41 +0,0 @@ -# -# This is the default english resource bundle file that will be used by Jetrix -# if a resource bundle for a specific Locale cannot be found. -# -# Additional locales can be specified by creating a new resource file in this -# directory that follows the following convention : -# -# jetrix_<language code>.properties -# -# for example : -# jetrix_fr.properties (French resources) -# jetrix_de.properties (German resources) -# jetrix_ja.properties (Japanese resources) -# -# The two digit language code must be lower case. A full list of language codes -# can be found at http://www-old.ics.uci.edu/pub/ietf/http/related/iso639.txt -# -# In order to enable a specific locale you must edit the Jetrix configuration -# file (config.xml) and insert the language code into the "language" tag : -# -# <tetrinet-server> -# ... -# <language>fr</language> -# ... -# </tetrinet-server> -# -# Some property strings are parameterized using braces, for example : -# -# "{0} has joined channel {1}" -# -# This will be converted into something like : -# -# "Smanux has joined channel #jetrix" -# -# You can also use style codes like [red], [green], [b]...[/b] -# -# Feel free to send your translation to sm...@lf... to make it available -# to all Jetrix users ! -# -# @author Emmanuel Bourg -# @version $Revision$, $Date$ Modified: jetrix/trunk/src/lang/jetrix_en.properties =================================================================== --- jetrix/trunk/src/lang/jetrix_en.properties 2010-05-03 15:12:26 UTC (rev 845) +++ jetrix/trunk/src/lang/jetrix_en.properties 2010-05-03 15:29:44 UTC (rev 846) @@ -167,19 +167,6 @@ command.list.status.open=OPEN command.list.status.full=FULL -command.mode.description=Change the channel's configuration. -command.mode.message0=PURE 15% sticks -command.mode.message1=COOKIES 1:1 (1 line cleared yields one special block) -command.mode.message2=PURE 20% sticks -command.mode.message3=COOKIE 2:1 (2 lines cleared yield one special block) -command.mode.message4=PURE 25% sticks -command.mode.message5=NO STICKS (0% stick) -command.mode.message6=ALL STICKS (100% stick) -command.mode.message7=COOKIES 1:1 with Z blocks only -command.mode.message8=COOKIES 1:1 with L blocks only -command.mode.message9=COOKIES 1:1 with T blocks only -command.mode.enabled=<gray>Configuration changed to <b>{0}</b> - command.motd.description=Display the message of the day. command.move.description=Move a player to a new slot. Modified: jetrix/trunk/src/lang/jetrix_fr.properties =================================================================== --- jetrix/trunk/src/lang/jetrix_fr.properties 2010-05-03 15:12:26 UTC (rev 845) +++ jetrix/trunk/src/lang/jetrix_fr.properties 2010-05-03 15:29:44 UTC (rev 846) @@ -131,19 +131,6 @@ command.list.status.open=OUVERT command.list.status.full=COMPLET -command.mode.description=Change la configuration du channel. -command.mode.message0=PURE 15% de lignes -command.mode.message1=COOKIES 1:1 (1 ligne effac\xE9e donne 1 bloc special) -command.mode.message2=PURE 20% de lignes -command.mode.message3=COOKIE 2:1 (2 lignes effac\xE9es donnent 1 bloc sp\xE9cial) -command.mode.message4=PURE 25% de lignes -command.mode.message5=SANS LIGNES (0% lignes) -command.mode.message6=TOUT LIGNES (100% lignes) -command.mode.message7=COOKIES 1:1 avec seulement des blocs Z -command.mode.message8=COOKIES 1:1 avec seulement des blocs L -command.mode.message9=COOKIES 1:1 avec seulement des blocs T -command.mode.enabled=<gray>Configuration chang\xE9e en <b>{0}</b> - command.motd.description=Affiche le message du jour. command.move.description=D\xE9place un joueur \xE0 un autre emplacement. @@ -209,7 +196,7 @@ filter.flood.blocked=<red>Flot de messages bloqu\xE9 en provenance de <b>{0} -filter.puzzle.announce=<b><brown>Niveay <aqua>{0}</aqua> : <purple>"{1}"</purple> <i>Con\xE7u par</i> <aqua>{2}</aqua>. +filter.puzzle.announce=<b><brown>Niveau <aqua>{0}</aqua> : <purple>"{1}"</purple> <i>Con\xE7u par</i> <aqua>{2}</aqua>. filter.puzzle.cleared=<b><red>F\xE9licitations ! Puzzle r\xE9solu ! filter.tetris.start_message=Le premier joueur qui r\xE9alise {0} tetris gagne ! Modified: jetrix/trunk/src/lang/jetrix_nl.properties =================================================================== --- jetrix/trunk/src/lang/jetrix_nl.properties 2010-05-03 15:12:26 UTC (rev 845) +++ jetrix/trunk/src/lang/jetrix_nl.properties 2010-05-03 15:29:44 UTC (rev 846) @@ -125,19 +125,6 @@ command.list.status.open=OPEN command.list.status.full=VOL -command.mode.description=Verander de configuratie van het kanaal. -command.mode.message0=PURE 15% sticks -command.mode.message1=COOKIES 1:1 (voor elke verwijderde regel krijg je 1 "special block") -command.mode.message2=PURE 20% sticks -command.mode.message3=COOKIE 2:1 (waneer je 2 regels verwijdert krijg je 1 "special block") -command.mode.message4=PURE 25% sticks -command.mode.message5=NO STICKS (0% stick) -command.mode.message6=ALL STICKS (100% stick) -command.mode.message7=COOKIES 1:1 alleen Z blocks -command.mode.message8=COOKIES 1:1 alleen L blocks -command.mode.message9=COOKIES 1:1 alleen T blocks -command.mode.enabled=<gray>Configuratie is veranderd naar <b>{0}</b> - command.motd.description=Toont de boodschap van de dag. command.move.description=Verplaatst de speler naar een andere plaats. Modified: jetrix/trunk/src/lang/jetrix_pt.properties =================================================================== --- jetrix/trunk/src/lang/jetrix_pt.properties 2010-05-03 15:12:26 UTC (rev 845) +++ jetrix/trunk/src/lang/jetrix_pt.properties 2010-05-03 15:29:44 UTC (rev 846) @@ -133,19 +133,6 @@ command.list.status.open=ABERTO command.list.status.full=CHEIO -command.mode.description=Mude as configura\xE7\xF5es do canal. -command.mode.message0=PURE 15% sticks -command.mode.message1=COOKIES 1:1 (1 linha feita 1 special block) -command.mode.message2=PURE 20% sticks -command.mode.message3=COOKIE 2:1 (2 linhas feitas 1 special block) -command.mode.message4=PURE 25% sticks -command.mode.message5=SEM STICKS (0% stick) -command.mode.message6=S\xD3 STICKS (100% stick) -command.mode.message7=COOKIES 1:1 s\xF3 blocos Z -command.mode.message8=COOKIES 1:1 s\xF3 blocos L -command.mode.message9=COOKIES 1:1 s\xF3 blocos T -command.mode.enabled=<gray>Configura\xE7\xF5es mudadas para <b>{0}</b> - command.motd.description=Exibe a mensagem do dia. command.move.description=Move um jogador para um novo slot. Modified: jetrix/trunk/src/test/net/jetrix/LanguageTest.java =================================================================== --- jetrix/trunk/src/test/net/jetrix/LanguageTest.java 2010-05-03 15:12:26 UTC (rev 845) +++ jetrix/trunk/src/test/net/jetrix/LanguageTest.java 2010-05-03 15:29:44 UTC (rev 846) @@ -31,6 +31,13 @@ */ public class LanguageTest extends TestCase { + protected void setUp() throws Exception + { + Locale.setDefault(Locale.KOREA); + Language.getInstance().addResources("jetrix"); + Language.getInstance().addResources("foo"); + } + public void testLoad() { Language language = Language.getInstance(); @@ -40,6 +47,7 @@ public void testIsSupported() { assertTrue("french is not supported", Language.isSupported(Locale.FRENCH)); + assertFalse("korean is supported", Language.isSupported(Locale.KOREAN)); } public void testGetText() @@ -50,9 +58,9 @@ public void testGetTextWithParameter() { - Object[] params = new Object[] { "Smanux", "tetrinet1" }; - String englishText = "<gray>Hello Smanux, you are in channel <b>tetrinet1</b>"; - String frenchText = "<gray>Salut Smanux, tu es dans le channel <b>tetrinet1</b>"; + Object[] params = new Object[] { "Smanux", 123 }; + String englishText = "<gray>Hello Smanux, you are in channel <b>123</b>"; + String frenchText = "<gray>Salut Smanux, tu es dans le channel <b>123</b>"; assertEquals("welcome message in english", englishText, Language.getText("channel.welcome", Locale.ENGLISH, params)); assertEquals("welcome message in french", frenchText, Language.getText("channel.welcome", Locale.FRENCH, params)); } @@ -83,4 +91,10 @@ assertTrue("english locale not found", locales.contains(Locale.ENGLISH)); assertTrue("french locale not found", locales.contains(Locale.FRENCH)); } + + public void testAdditionalBundle() + { + Language.getInstance().addResources("command.mode"); + assertEquals("<gray>Configuration changed to <b>yes</b>", Language.getText("command.mode.enabled", Locale.ENGLISH, "key:common.yes")); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |