plexus-i18n-1.0-beta-10/0000755000175000017500000000000011226432471014536 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/0000755000175000017500000000000011226432471015325 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/test/0000755000175000017500000000000011226432471016304 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/test/resources/0000755000175000017500000000000011226432471020316 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/test/resources/org/0000755000175000017500000000000011226432471021105 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/test/resources/org/codehaus/0000755000175000017500000000000011226432471022700 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/test/resources/org/codehaus/plexus/0000755000175000017500000000000011226432471024220 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/test/resources/org/codehaus/plexus/i18n/0000755000175000017500000000000011226432471024777 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/test/resources/org/codehaus/plexus/i18n/Messages.properties0000644000175000017500000000004711043774766030702 0ustar twernertwernermsg1=Hellow Orld! msg2=Hellow {0} Orld!plexus-i18n-1.0-beta-10/src/test/resources/org/codehaus/plexus/i18n/BarBundle_xx.properties0000644000175000017500000000001511041733116031461 0ustar twernertwernername = plexusplexus-i18n-1.0-beta-10/src/test/resources/org/codehaus/plexus/i18n/DefaultI18NTest.xml0000644000175000017500000000054111041733116030340 0ustar twernertwerner org.codehaus.plexus.i18n.I18N org.codehaus.plexus.i18n.DefaultI18N org.codehaus.plexus.i18n.BarBundle plexus-i18n-1.0-beta-10/src/test/resources/org/codehaus/plexus/i18n/i18n.properties0000644000175000017500000000016111041733116027665 0ustar twernertwernerkey1 = [] value1 thanks.message = Thanks {0}! thanks.message1 = Thanks {0} {1}! thanks.message2 = Thanks {0} {1}!plexus-i18n-1.0-beta-10/src/test/resources/org/codehaus/plexus/i18n/i18n_it.properties0000644000175000017500000000016311041733116030363 0ustar twernertwernerkey1 = [it] value1 thanks.message = Grazie {0}! thanks.message1 = Grazie {0} {1}! thanks.message2 = Grazie {0} {1}!plexus-i18n-1.0-beta-10/src/test/resources/org/codehaus/plexus/i18n/i18n_fr.properties0000644000175000017500000000016011041733116030353 0ustar twernertwernerkey1 = [fr] value1 thanks.message = Merci {0}! thanks.message1 = Merci {0} {1}! thanks.message2 = Merci {0} {1}!plexus-i18n-1.0-beta-10/src/test/java/0000755000175000017500000000000011226432470017224 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/test/java/org/0000755000175000017500000000000011226432471020014 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/test/java/org/codehaus/0000755000175000017500000000000011226432471021607 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/test/java/org/codehaus/plexus/0000755000175000017500000000000011226432471023127 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/test/java/org/codehaus/plexus/i18n/0000755000175000017500000000000011226432471023706 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/test/java/org/codehaus/plexus/i18n/BarBundle_ko_KR.java0000644000175000017500000000210311041733116027463 0ustar twernertwernerpackage org.codehaus.plexus.i18n; /* * Copyright 2001-2007 Codehaus Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.ListResourceBundle; /** * A dummied up Korean resource bundle for use in testing. */ public class BarBundle_ko_KR extends ListResourceBundle { private static final Object[][] CONTENTS = { { "key1", "[ko] value1" }, { "key2", "[ko] value2" }, { "key3", "[ko] value3" }, { "key4", "[ko] value4" } }; protected Object[][] getContents() { return CONTENTS; } } plexus-i18n-1.0-beta-10/src/test/java/org/codehaus/plexus/i18n/BarBundle.java0000644000175000017500000000205711041733116026406 0ustar twernertwernerpackage org.codehaus.plexus.i18n; /* * Copyright 2001-2007 Codehaus Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.ListResourceBundle; /** * A default resource bundle for use in testing. */ public class BarBundle extends ListResourceBundle { private static final Object[][] CONTENTS = { { "key1", "[] value1" }, { "key2", "[] value2" }, { "key3", "[] value3" }, { "key4", "[] value4" } }; protected Object[][] getContents() { return CONTENTS; } } plexus-i18n-1.0-beta-10/src/test/java/org/codehaus/plexus/i18n/FooBundle_fr.java0000644000175000017500000000210011041733116027101 0ustar twernertwernerpackage org.codehaus.plexus.i18n; /* * Copyright 2001-2007 Codehaus Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.ListResourceBundle; /** * A dummied up French resource bundle for use in testing. */ public class FooBundle_fr extends ListResourceBundle { private static final Object[][] CONTENTS = { { "key1", "[fr] value1" }, { "key2", "[fr] value2" }, { "key3", "[fr] value3" }, { "key4", "[fr] value4" } }; protected Object[][] getContents() { return CONTENTS; } } plexus-i18n-1.0-beta-10/src/test/java/org/codehaus/plexus/i18n/I18NTokenizerTest.java0000644000175000017500000001012111041733116027751 0ustar twernertwernerpackage org.codehaus.plexus.i18n; /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.codehaus.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Turbine" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact codehaus@codehaus.org. * * 5. Products derived from this software may not be called "Apache", * "Apache Turbine", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . */ import java.util.Locale; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * Test case for the locale tokenizer. * * @author Daniel Rall * @author Jason van Zyl * @version $Id: I18NTokenizerTest.java 831 2004-06-16 17:01:12Z jvanzyl $ */ public class I18NTokenizerTest extends TestCase { private static final String HEADER = "en, es;q=0.8, zh-TW;q=0.1"; public I18NTokenizerTest( String name ) { super( name ); } public static Test suite() { return new TestSuite( I18NTokenizerTest.class ); } public void testLocaleTokenizer() { try { I18NTokenizer tok = new I18NTokenizer( HEADER ); Locale locale = (Locale) tok.next(); assertEquals( "Either wrong language or order parsing: " + locale, locale.getLanguage(), "en" ); locale = (Locale) tok.next(); assertEquals( "Either wrong language or order parsing: " + locale, locale.getLanguage(), "es" ); locale = (Locale) tok.next(); assertEquals( "Either wrong country or order parsing: " + locale, locale.getCountry(), "TW" ); } catch ( Exception e ) { e.printStackTrace(); fail(); } } } plexus-i18n-1.0-beta-10/src/test/java/org/codehaus/plexus/i18n/BarBundle_en_US.java0000644000175000017500000000211211041733116027467 0ustar twernertwernerpackage org.codehaus.plexus.i18n; /* * Copyright 2001-2007 Codehaus Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.ListResourceBundle; /** * An english resource bundle for use in testing. */ public class BarBundle_en_US extends ListResourceBundle { private static final Object[][] CONTENTS = { { "key1", "[en_US] value1" }, { "key2", "[en_US] value2" }, { "key3", "[en_US] value3" }, { "key4", "[en_US] value4" } }; protected Object[][] getContents() { return CONTENTS; } } plexus-i18n-1.0-beta-10/src/test/java/org/codehaus/plexus/i18n/DefaultLanguageTest.java0000644000175000017500000000103711043651764030447 0ustar twernertwernerpackage org.codehaus.plexus.i18n; import junit.framework.TestCase; public class DefaultLanguageTest extends TestCase { private static Language lang; protected void setUp() throws Exception { lang = new DefaultLanguage(DefaultLanguageTest.class); } public void testGetMessage() { String msg = lang.getMessage( "msg1" ); assertEquals( "Hellow Orld!", msg ); } public void testGetMessageArgs() { String msg = lang.getMessage( "msg2", "deer" ); assertEquals( "Hellow deer Orld!", msg ); } } plexus-i18n-1.0-beta-10/src/test/java/org/codehaus/plexus/i18n/FooBundle_en.java0000644000175000017500000000206711041733116027110 0ustar twernertwernerpackage org.codehaus.plexus.i18n; /* * Copyright 2001-2007 Codehaus Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.ListResourceBundle; /** * An english resource bundle for use in testing. */ public class FooBundle_en extends ListResourceBundle { private static final Object[][] CONTENTS = { { "key1", "[en] value1" }, { "key2", "[en] value2" }, { "key3", "[en] value3" }, { "key4", "[en] value4" } }; protected Object[][] getContents() { return CONTENTS; } } plexus-i18n-1.0-beta-10/src/test/java/org/codehaus/plexus/i18n/DefaultI18NTest.java0000644000175000017500000001372711041733116027402 0ustar twernertwernerpackage org.codehaus.plexus.i18n; /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001-2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.codehaus.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Turbine" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact codehaus@codehaus.org. * * 5. Products derived from this software may not be called "Apache", * "Apache Turbine", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . */ import org.codehaus.plexus.PlexusTestCase; import java.util.Locale; /** * Tests the API of the * {@link org.codehaus.plexus.i18n.I18N}. *
* * @author Daniel Rall * @author Jason van Zyl */ public class DefaultI18NTest extends PlexusTestCase { private I18N i18n; protected void setUp() throws Exception { super.setUp(); /* Set an unsupported locale to default to ensure we do not get unexpected matches */ Locale.setDefault( new Locale( "jp" ) ); i18n = (I18N) lookup( I18N.ROLE ); } public void testLocalization() { String s0 = i18n.getString( null, null, "key1" ); assertEquals( "Unable to retrieve localized text for locale: default", s0, "[] value1" ); String s1 = i18n.getString( null, new Locale( "en", "US" ), "key2" ); assertEquals( "Unable to retrieve localized text for locale: en-US", s1, "[en_US] value2" ); String s2 = i18n.getString( "org.codehaus.plexus.i18n.BarBundle", new Locale( "ko", "KR" ), "key3" ); assertEquals( "Unable to retrieve localized text for locale: ko-KR", s2, "[ko] value3" ); String s3 = i18n.getString( "org.codehaus.plexus.i18n.BarBundle", new Locale( "ja" ), "key1" ); assertEquals( "Unable to fall back from non-existant locale: jp", "[] value1", s3 ); String s4 = i18n.getString( "org.codehaus.plexus.i18n.FooBundle", new Locale( "fr" ), "key3" ); assertEquals( "Unable to retrieve localized text for locale: fr", s4, "[fr] value3" ); String s5 = i18n.getString( "org.codehaus.plexus.i18n.FooBundle", new Locale( "fr", "FR" ), "key3" ); assertEquals( "Unable to retrieve localized text for locale: fr-FR", s5, "[fr] value3" ); String s6 = i18n.getString( "org.codehaus.plexus.i18n.i18n", null, "key1" ); assertEquals( "Unable to retrieve localized properties for locale: default", "[] value1", s6 ); Locale old = Locale.getDefault(); Locale.setDefault(Locale.FRENCH); try { String s7 = i18n.getString( "org.codehaus.plexus.i18n.i18n", Locale.ENGLISH, "key1" ); assertEquals( "Not picking up new default locale: fr", "[fr] value1", s7 ); String s8 = i18n.getString( "org.codehaus.plexus.i18n.i18n", Locale.ITALIAN, "key1" ); assertEquals( "Unable to retrieve localized properties for locale: it", "[it] value1", s8 ); } finally { Locale.setDefault(old); } } public void testLocalizedMessagesWithFormatting() { // Format methods String s6 = i18n.format( "org.codehaus.plexus.i18n.i18n", null, "thanks.message", "jason" ); assertEquals( s6, "Thanks jason!" ); String s7 = i18n.format( "org.codehaus.plexus.i18n.i18n", null, "thanks.message1", "jason", "van zyl" ); assertEquals( s7, "Thanks jason van zyl!" ); String s8 = i18n.format( "org.codehaus.plexus.i18n.i18n", null, "thanks.message2", new Object[]{ "jason", "van zyl" } ); assertEquals( s8, "Thanks jason van zyl!" ); } public void testLocalizedMessagesWithNonStandardLocale() { String s0 = i18n.getString( "name", new Locale( "xx" ) ); assertEquals( "plexus", s0 ); } } plexus-i18n-1.0-beta-10/src/main/0000755000175000017500000000000011226432471016251 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/main/java/0000755000175000017500000000000011226432471017172 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/main/java/org/0000755000175000017500000000000011226432471017761 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/main/java/org/codehaus/0000755000175000017500000000000011226432471021554 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/main/java/org/codehaus/plexus/0000755000175000017500000000000011226432471023074 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/main/java/org/codehaus/plexus/i18n/0000755000175000017500000000000011226432471023653 5ustar twernertwernerplexus-i18n-1.0-beta-10/src/main/java/org/codehaus/plexus/i18n/LanguageException.java0000644000175000017500000000053411041733116030115 0ustar twernertwernerpackage org.codehaus.plexus.i18n; public class LanguageException extends Exception { public LanguageException() { } public LanguageException(String message) { super(message); } public LanguageException(Throwable cause) { super(cause); } public LanguageException(String message, Throwable cause) { super(message, cause); } } plexus-i18n-1.0-beta-10/src/main/java/org/codehaus/plexus/i18n/DefaultLanguage.java0000644000175000017500000000344511043651740027553 0ustar twernertwernerpackage org.codehaus.plexus.i18n; import java.util.Locale; import java.util.ResourceBundle; import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; /** * * * @author Oleg Gusakov * * @plexus.component * role="org.codehaus.plexus.i18n.Language" */ public class DefaultLanguage implements Language { private String bundleName; private Locale locale; // private ResourceBundle rb; private DefaultI18N i18n = new DefaultI18N(); private String error; //------------------------------------------------------------------------------------- public DefaultLanguage() { } //------------------------------------------------------------------------------------- public DefaultLanguage( Class clazz ) { this.bundleName = clazz.getPackage().getName()+"."+DEFAULT_NAME; try { i18n.initialize(); } catch( InitializationException e ) { error = e.getMessage(); } } //------------------------------------------------------------------------------------- public DefaultLanguage( Class clazz, Locale locale ) { this( clazz ); this.locale = locale; // rb = ResourceBundle.getBundle( clazz.getPackage().getName()+"."+DEFAULT_NAME, locale, clazz.getClassLoader() ); } //------------------------------------------------------------------------------------- public String getMessage( String key, String... args ) { if( error != null ) return error; if( args == null || args.length == 0) return i18n.getString( bundleName, locale, key ); return i18n.format( bundleName, locale, key, args ); } //------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------- } plexus-i18n-1.0-beta-10/src/main/java/org/codehaus/plexus/i18n/DefaultI18N.java0000644000175000017500000003745611041733116026514 0ustar twernertwernerpackage org.codehaus.plexus.i18n; /* * Copyright 2001-2007 Codehaus Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; import org.codehaus.plexus.util.StringUtils; import java.text.MessageFormat; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.MissingResourceException; import java.util.ResourceBundle; import java.lang.reflect.Field; /** * @plexus.component * role="org.codehaus.plexus.i18n.I18N" */ public class DefaultI18N extends AbstractLogEnabled implements I18N, Initializable { private static final Object[] NO_ARGS = new Object[0]; private HashMap bundles; private String[] bundleNames; private String defaultBundleName; private Locale defaultLocale = Locale.getDefault(); private String defaultLanguage = Locale.getDefault().getLanguage(); private String defaultCountry = Locale.getDefault().getCountry(); private boolean devMode; // ---------------------------------------------------------------------- // Accessors // ---------------------------------------------------------------------- public String getDefaultLanguage() { return defaultLanguage; } public String getDefaultCountry() { return defaultCountry; } public String getDefaultBundleName() { return defaultBundleName; } public String[] getBundleNames() { return (String[]) bundleNames.clone(); } public ResourceBundle getBundle() { return getBundle( getDefaultBundleName(), (Locale) null ); } public ResourceBundle getBundle( String bundleName ) { return getBundle( bundleName, (Locale) null ); } /** * This method returns a ResourceBundle given the bundle name and * the Locale information supplied in the HTTP "Accept-Language" * header. * * @param bundleName Name of bundle. * @param languageHeader A String with the language header. * @return A localized ResourceBundle. */ public ResourceBundle getBundle( String bundleName, String languageHeader ) { return getBundle( bundleName, getLocale( languageHeader ) ); } /** * This method returns a ResourceBundle for the given bundle name * and the given Locale. * * @param bundleName Name of bundle (or null for the * default bundle). * @param locale The locale (or null for the locale * indicated by the default language and country). * @return A localized ResourceBundle. */ public ResourceBundle getBundle( String bundleName, Locale locale ) { // Assure usable inputs. bundleName = ( bundleName == null ? getDefaultBundleName() : bundleName.trim() ); // ---------------------------------------------------------------------- // A hack to make sure the properties files are always checked // ---------------------------------------------------------------------- if ( devMode ) { try { Class klass = ResourceBundle.getBundle( bundleName ).getClass().getSuperclass(); Field field = klass.getDeclaredField( "cacheList" ); field.setAccessible( true ); // SoftCache cache = (SoftCache) field.get( null ); // // cache.clear(); Object cache = field.get( null ); cache.getClass().getDeclaredMethod( "clear", null ).invoke( cache, null ); field.setAccessible( false ); } catch ( Exception e ) { // Intentional } } if ( locale == null ) { locale = getLocale( null ); } // Find/retrieve/cache bundle. ResourceBundle rb; HashMap bundlesByLocale = (HashMap) bundles.get( bundleName ); if ( bundlesByLocale != null ) { // Cache of bundles by locale for the named bundle exists. // Check the cache for a bundle corresponding to locale. rb = (ResourceBundle) bundlesByLocale.get( locale ); if ( rb == null ) { // Not yet cached. rb = cacheBundle( bundleName, locale ); } } else { rb = cacheBundle( bundleName, locale ); } return rb; } /** * @see I18N#getLocale(String) */ public Locale getLocale( String header ) { if ( !StringUtils.isEmpty( header ) ) { I18NTokenizer tok = new I18NTokenizer( header ); if ( tok.hasNext() ) { return (Locale) tok.next(); } } // Couldn't parse locale. return defaultLocale; } public String getString( String key ) { return getString( key, null ); } public String getString( String key, Locale locale ) { return getString( getDefaultBundleName(), locale, key ); } /** * @throws MissingResourceException Specified key cannot be matched. * @see I18N#getString(String, Locale, String) */ public String getString( String bundleName, Locale locale, String key ) { String value; if ( locale == null ) { locale = getLocale( null ); } // Look for text in requested bundle. ResourceBundle rb = getBundle( bundleName, locale ); value = getStringOrNull( rb, key ); // Look for text in list of default bundles. if ( value == null && bundleNames.length > 0 ) { String name; for ( int i = 0; i < bundleNames.length; i++ ) { name = bundleNames[i]; if ( !name.equals( bundleName ) ) { rb = getBundle( name, locale ); value = getStringOrNull( rb, key ); if ( value != null ) { locale = rb.getLocale(); break; } } } } if ( value == null ) { String loc = locale.toString(); String mesg = "Noticed missing resource: " + "bundleName=" + bundleName + ", locale=" + loc + ", key=" + key; getLogger().debug( mesg ); // Just send back the key, we don't need to throw an exception. value = key; } return value; } public String format( String key, Object arg1 ) { return format( defaultBundleName, defaultLocale, key, new Object[]{arg1} ); } public String format( String key, Object arg1, Object arg2 ) { return format( defaultBundleName, defaultLocale, key, new Object[]{arg1, arg2} ); } /** * @see I18N#format(String, Locale, String, Object) */ public String format( String bundleName, Locale locale, String key, Object arg1 ) { return format( bundleName, locale, key, new Object[]{arg1} ); } /** * @see I18N#format(String, Locale, String, Object, Object) */ public String format( String bundleName, Locale locale, String key, Object arg1, Object arg2 ) { return format( bundleName, locale, key, new Object[]{arg1, arg2} ); } /** * Looks up the value for key in the * ResourceBundle referenced by * bundleName, then formats that value for the * specified Locale using args. * * @return Localized, formatted text identified by * key. */ public String format( String bundleName, Locale locale, String key, Object[] args ) { if ( locale == null ) { // When formatting Date objects and such, MessageFormat // cannot have a null Locale. locale = getLocale( null ); } String value = getString( bundleName, locale, key ); if ( args == null ) { args = NO_ARGS; } // FIXME: after switching to JDK 1.4, it will be possible to clean // this up by providing the Locale along with the string in the // constructor to MessageFormat. Until 1.4, the following workaround // is required for constructing the format with the appropriate locale: MessageFormat messageFormat = new MessageFormat( "" ); messageFormat.setLocale( locale ); messageFormat.applyPattern( value ); return messageFormat.format( args ); } /** * Called the first time the Service is used. */ public void initialize() throws InitializationException { bundles = new HashMap(); defaultLocale = new Locale( defaultLanguage, defaultCountry ); initializeBundleNames(); if ( "true".equals( System.getProperty( "PLEXUS_DEV_MODE" ) ) ) { devMode = true; } } // ---------------------------------------------------------------------- // Implementation // ---------------------------------------------------------------------- protected void initializeBundleNames() { //System.err.println("cfg=" + getConfiguration()); if ( defaultBundleName != null && defaultBundleName.length() > 0 ) { // Using old-style single bundle name property. if ( bundleNames == null || bundleNames.length <= 0 ) { bundleNames = new String[]{defaultBundleName}; } else { // Prepend "default" bundle name. String[] array = new String[bundleNames.length + 1]; array[0] = defaultBundleName; System.arraycopy( bundleNames, 0, array, 1, bundleNames.length ); bundleNames = array; } } if ( bundleNames == null ) { bundleNames = new String[0]; } } /** * Caches the named bundle for fast lookups. This operation is * relatively expesive in terms of memory use, but is optimized * for run-time speed in the usual case. * * @throws MissingResourceException Bundle not found. */ private synchronized ResourceBundle cacheBundle( String bundleName, Locale locale ) throws MissingResourceException { HashMap bundlesByLocale = (HashMap) bundles.get( bundleName ); ResourceBundle rb = ( bundlesByLocale == null ? null : (ResourceBundle) bundlesByLocale.get( locale ) ); if ( rb == null ) { bundlesByLocale = ( bundlesByLocale == null ? new HashMap( 3 ) : new HashMap( bundlesByLocale ) ); try { rb = ResourceBundle.getBundle( bundleName, locale ); } catch ( MissingResourceException e ) { rb = findBundleByLocale( bundleName, locale, bundlesByLocale ); if ( rb == null ) { throw (MissingResourceException) e.fillInStackTrace(); } } if ( rb != null ) { // Cache bundle. bundlesByLocale.put( rb.getLocale(), rb ); HashMap bundlesByName = new HashMap( bundles ); bundlesByName.put( bundleName, bundlesByLocale ); this.bundles = bundlesByName; } } return rb; } /** *

Retrieves the bundle most closely matching first against the * supplied inputs, then against the defaults.

*

*

Use case: some clients send a HTTP Accept-Language header * with a value of only the language to use * (i.e. "Accept-Language: en"), and neglect to include a country. * When there is no bundle for the requested language, this method * can be called to try the default country (checking internally * to assure the requested criteria matches the default to avoid * disconnects between language and country).

*

*

Since we're really just guessing at possible bundles to use, * we don't ever throw MissingResourceException.

*/ private ResourceBundle findBundleByLocale( String bundleName, Locale locale, Map bundlesByLocale ) { ResourceBundle rb = null; if ( !StringUtils.isNotEmpty( locale.getCountry() ) && defaultLanguage.equals( locale.getLanguage() ) ) { /* category.debug("Requested language '" + locale.getLanguage() + "' matches default: Attempting to guess bundle " + "using default country '" + defaultCountry + '\''); */ Locale withDefaultCountry = new Locale( locale.getLanguage(), defaultCountry ); rb = (ResourceBundle) bundlesByLocale.get( withDefaultCountry ); if ( rb == null ) { rb = getBundleIgnoreException( bundleName, withDefaultCountry ); } } else if ( !StringUtils.isNotEmpty( locale.getLanguage() ) && defaultCountry.equals( locale.getCountry() ) ) { Locale withDefaultLanguage = new Locale( defaultLanguage, locale.getCountry() ); rb = (ResourceBundle) bundlesByLocale.get( withDefaultLanguage ); if ( rb == null ) { rb = getBundleIgnoreException( bundleName, withDefaultLanguage ); } } if ( rb == null && !defaultLocale.equals( locale ) ) { rb = getBundleIgnoreException( bundleName, defaultLocale ); } return rb; } /** * Retrieves the bundle using the * ResourceBundle.getBundle(String, Locale) method, * returning null instead of throwing * MissingResourceException. */ private ResourceBundle getBundleIgnoreException( String bundleName, Locale locale ) { try { return ResourceBundle.getBundle( bundleName, locale ); } catch ( MissingResourceException ignored ) { return null; } } /** * Gets localized text from a bundle if it's there. Otherwise, * returns null (ignoring a possible * MissingResourceException). */ protected final String getStringOrNull( ResourceBundle rb, String key ) { if ( rb != null ) { try { return rb.getString( key ); } catch ( MissingResourceException ignored ) { // intentional } } return null; } } plexus-i18n-1.0-beta-10/src/main/java/org/codehaus/plexus/i18n/Language.java0000644000175000017500000000052111043651740026236 0ustar twernertwernerpackage org.codehaus.plexus.i18n; import java.util.Locale; public interface Language { public static String ROLE = Language.class.getName(); /** look for Messages.properties in the clazz package by default */ public static final String DEFAULT_NAME = "Messages"; public String getMessage( String key, String... args ) ; } plexus-i18n-1.0-beta-10/src/main/java/org/codehaus/plexus/i18n/I18NTokenizer.java0000644000175000017500000001173211041733116027067 0ustar twernertwernerpackage org.codehaus.plexus.i18n; /* * Copyright 2001-2007 Codehaus Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.Locale; import java.util.NoSuchElementException; import java.util.StringTokenizer; /** * Parses the HTTP Accept-Language header as per section * 14.4 of RFC 2068 (HTTP 1.1 header field definitions). * * @author Daniel Rall * @version $Id: I18NTokenizer.java 6675 2007-07-20 23:05:53Z olamy $ * * @todo Move this class out of here as its purely web related. */ public class I18NTokenizer implements Iterator { /** * Separates elements of the Accept-Language HTTP * header. */ private static final String LOCALE_SEPARATOR = ","; /** * Separates locale from quality within elements. */ private static final char QUALITY_SEPARATOR = ';'; /** * The default quality value for an AcceptLanguage * object. */ private static final Float DEFAULT_QUALITY = new Float(1.0f); /** * The parsed locales. */ private ArrayList locales = new ArrayList(3); /** * Parses the Accept-Language header. * * @param header The Accept-Language header * (i.e. en, es;q=0.8, zh-TW;q=0.1). */ public I18NTokenizer(String header) { StringTokenizer tok = new StringTokenizer(header, LOCALE_SEPARATOR); while (tok.hasMoreTokens()) { AcceptLanguage acceptLang = new AcceptLanguage(); String element = tok.nextToken().trim(); int index; // Record and cut off any quality value that comes after a // semi-colon. if ( (index = element.indexOf(QUALITY_SEPARATOR)) != -1 ) { String q = element.substring(index); element = element.substring(0, index); if ( (index = q.indexOf('=')) != -1 ) { try { acceptLang.quality = Float.valueOf(q.substring(index + 1)); } catch (NumberFormatException useDefault) { } } } element = element.trim(); // Create a Locale from the language. A dash may separate the // language from the country. if ( (index = element.indexOf('-')) == -1 ) { // No dash means no country. acceptLang.locale = new Locale(element, ""); } else { acceptLang.locale = new Locale(element.substring(0, index), element.substring(index + 1)); } locales.add(acceptLang); } // Sort by quality in descending order. Collections.sort(locales, Collections.reverseOrder()); } /** * @return Whether there are more locales. */ public boolean hasNext() { return !locales.isEmpty(); } /** * Creates a Locale from the next element of the * Accept-Language header. * * @return The next highest-rated Locale. * @throws NoSuchElementException No more locales. */ public Object next() { if (locales.isEmpty()) { throw new NoSuchElementException(); } return ((AcceptLanguage) locales.remove(0)).locale; } /** * Not implemented. */ public final void remove() { throw new UnsupportedOperationException(getClass().getName() + " does not support remove()"); } /** * Struct representing an element of the HTTP * Accept-Language header. */ private class AcceptLanguage implements Comparable { /** * The language and country. */ Locale locale; /** * The quality of our locale (as values approach * 1.0, they indicate increased user preference). */ Float quality = DEFAULT_QUALITY; public final int compareTo(Object acceptLang) { return quality.compareTo( ((AcceptLanguage) acceptLang).quality ); } } } plexus-i18n-1.0-beta-10/src/main/java/org/codehaus/plexus/i18n/I18N.java0000644000175000017500000000335111041733116025172 0ustar twernertwernerpackage org.codehaus.plexus.i18n; /* * Copyright 2001-2007 Codehaus Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Locale; import java.util.ResourceBundle; public interface I18N { public static String ROLE = I18N.class.getName(); String ACCEPT_LANGUAGE = "Accept-Language"; String getDefaultLanguage(); String getDefaultCountry(); String getDefaultBundleName(); String[] getBundleNames(); ResourceBundle getBundle(); ResourceBundle getBundle( String bundleName ); ResourceBundle getBundle( String bundleName, String languageHeader ); ResourceBundle getBundle( String bundleName, Locale locale ); Locale getLocale( String languageHeader ); String getString( String key ); String getString( String key, Locale locale ); String getString( String bundleName, Locale locale, String key ); String format( String key, Object arg1 ); String format( String key, Object arg1, Object arg2 ); String format( String bundleName, Locale locale, String key, Object arg1 ); String format( String bundleName, Locale locale, String key, Object arg1, Object arg2 ); String format( String bundleName, Locale locale, String key, Object[] args ); } plexus-i18n-1.0-beta-10/pom.xml0000644000175000017500000000403311051352372016050 0ustar twernertwerner plexus-components org.codehaus.plexus 1.1.12 4.0.0 plexus-i18n Plexus I18N Component 1.0-beta-10 Plexus Central Development Repository dav:https://dav.codehaus.org/snapshots.repository/plexus codehaus.org ${distMgmtSnapshotsName} ${distMgmtSnapshotsUrl} org.codehaus.plexus plexus-maven-plugin 1.3.5 descriptor maven-compiler-plugin 1.5 1.5 org.codehaus.plexus plexus-utils 1.4.5 scm:svn:http://svn.codehaus.org/plexus/plexus-components/tags/plexus-i18n-1.0-beta-10 scm:svn:https://svn.codehaus.org/plexus/plexus-components/tags/plexus-i18n-1.0-beta-10 http://fisheye.codehaus.org/browse/plexus/plexus-components/tags/plexus-i18n-1.0-beta-10