You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2013/01/28 20:08:47 UTC
svn commit: r1439578 - in /incubator/jspwiki/trunk:
src/org/apache/wiki/i18n/InternationalizationManager.java
tests/org/apache/wiki/i18n/
tests/org/apache/wiki/i18n/InternationalizationManagerTest.java
Author: juanpablo
Date: Mon Jan 28 19:08:47 2013
New Revision: 1439578
URL: http://svn.apache.org/viewvc?rev=1439578&view=rev
Log:
New get(..) method to obtain parameterized i18n messages
Tests for I18nManager
Added:
incubator/jspwiki/trunk/tests/org/apache/wiki/i18n/
incubator/jspwiki/trunk/tests/org/apache/wiki/i18n/InternationalizationManagerTest.java
Modified:
incubator/jspwiki/trunk/src/org/apache/wiki/i18n/InternationalizationManager.java
Modified: incubator/jspwiki/trunk/src/org/apache/wiki/i18n/InternationalizationManager.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/i18n/InternationalizationManager.java?rev=1439578&r1=1439577&r2=1439578&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/org/apache/wiki/i18n/InternationalizationManager.java (original)
+++ incubator/jspwiki/trunk/src/org/apache/wiki/i18n/InternationalizationManager.java Mon Jan 28 19:08:47 2013
@@ -18,6 +18,7 @@
*/
package org.apache.wiki.i18n;
+import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
@@ -35,6 +36,11 @@ public class InternationalizationManager
* resource strings. It's value is {@value}.
*/
public static final String CORE_BUNDLE = "CoreResources";
+
+ /** The name of the ResourceBundle which contains any and all JSPWiki default templates
+ * resource strings. It's value is {@value}.
+ */
+ public static final String DEF_TEMPLATE = "templates/default";
// public static final String JSPWIKI_BUNDLE = "jspwiki";
// public static final String PLUGINS_BUNDLE = "plugins";
@@ -71,9 +77,11 @@ public class InternationalizationManager
public ResourceBundle getBundle( String bundle, Locale locale ) throws MissingResourceException
{
if( locale == null )
+ {
locale = Locale.getDefault();
+ }
- ResourceBundle b = ResourceBundle.getBundle(bundle,locale);
+ ResourceBundle b = ResourceBundle.getBundle( bundle, locale );
return b;
}
@@ -89,7 +97,22 @@ public class InternationalizationManager
*/
public String get( String bundle, Locale locale, String key ) throws MissingResourceException
{
- return getBundle(bundle,locale).getString(key);
+ return getBundle( bundle, locale ).getString( key );
+ }
+
+ /**
+ * Obtain a parameterized String from the bundle.
+ * @param bundle Which bundle the string is in
+ * @param locale Locale to use - null for default
+ * @param key Which key to use.
+ * @param args parameters to insert n the String.
+ * @return A localized string (or from the default language, if not found)
+ * @throws MissingResourceException If the key cannot be located at all, even from the default locale.
+ */
+ public String get( String bundle, Locale locale, String key, Object... args ) throws MissingResourceException
+ {
+ MessageFormat mf = new MessageFormat( get( bundle, locale, key ), locale );
+ return mf.format( args );
}
}
Added: incubator/jspwiki/trunk/tests/org/apache/wiki/i18n/InternationalizationManagerTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/i18n/InternationalizationManagerTest.java?rev=1439578&view=auto
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/i18n/InternationalizationManagerTest.java (added)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/i18n/InternationalizationManagerTest.java Mon Jan 28 19:08:47 2013
@@ -0,0 +1,47 @@
+package org.apache.wiki.i18n;
+
+import java.util.Locale;
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+import org.apache.wiki.TestEngine;
+
+public class InternationalizationManagerTest extends TestCase
+{
+ TestEngine te = null;
+ Properties props = new Properties();
+ InternationalizationManager i18n = null;
+
+ protected void setUp() throws Exception
+ {
+ props.load( TestEngine.findTestProperties() );
+ te = new TestEngine( props );
+ i18n = te.getInternationalizationManager();
+ }
+
+ public void testGetFromCoreWithArgs()
+ {
+ String str = i18n.get( InternationalizationManager.CORE_BUNDLE,
+ Locale.ENGLISH,
+ "security.error.cannot.rename",
+ "Test User" );
+ assertEquals( "Cannot rename: the login name 'Test User' is already taken.", str );
+ }
+
+ public void testGetFromDefTemplateWithArgs()
+ {
+ String str = i18n.get( InternationalizationManager.DEF_TEMPLATE,
+ Locale.ENGLISH,
+ "notification.createUserProfile.accept.content",
+ "JSPWiki", "testUser", "Test User", "test@user.com", "www.foo.com" );
+ assertEquals( "Congratulations! Your new profile on JSPWiki has been created. " +
+ "Your profile details are as follows: \n\n" +
+ "Login name: testUser \n" +
+ "Your name : Test User \n" +
+ "E-mail : test@user.com \n\n" +
+ "If you forget your password, you can reset it at www.foo.com",
+ str );
+ }
+
+}