Skip to content
Browse files

+i18n for help descriptions

  • Loading branch information...
1 parent 70b8fad commit fd647c9576fc6e25bd6cf9864b22c590a10ee33e @elkodus committed Sep 30, 2012
View
12 src/main/java/joptsimple/BuiltinHelpFormatter.java
@@ -46,9 +46,9 @@ a copy of this software and associated documentation files (the
public String format( Map<String, ? extends OptionDescriptor> options ) {
if ( options.isEmpty() )
- return "No options specified";
+ return Messages.getString( "BuiltinHelpFormatter.noOptionsSpecified" );
- grid = new ColumnarData( optionHeader( options ), "Description" );
+ grid = new ColumnarData( optionHeader( options ), Messages.getString( "BuiltinHelpFormatter.description" ) );
Comparator<OptionDescriptor> comparator =
new Comparator<OptionDescriptor>() {
@@ -69,10 +69,10 @@ public int compare( OptionDescriptor first, OptionDescriptor second ) {
private String optionHeader( Map<String, ? extends OptionDescriptor> options ) {
for ( OptionDescriptor each : options.values() ) {
if ( each.isRequired() )
- return "Option (* = required)";
+ return Messages.getString( "BuiltinHelpFormatter.optionRequired" );
}
- return "Option";
+ return Messages.getString( "BuiltinHelpFormatter.option" );
}
private void addHelpLineFor( OptionDescriptor descriptor ) {
@@ -111,7 +111,7 @@ else if ( argDescription.length() > 0 )
}
private String createOptionDisplay( OptionDescriptor descriptor ) {
- StringBuilder buffer = new StringBuilder( descriptor.isRequired() ? "* " : "" );
+ StringBuilder buffer = new StringBuilder( descriptor.isRequired() ? "* " : "" ); //$NON-NLS-2$
for ( Iterator<String> iter = descriptor.options().iterator(); iter.hasNext(); ) {
String option = iter.next();
@@ -131,7 +131,7 @@ private String createDescriptionDisplay( OptionDescriptor descriptor ) {
return descriptor.description();
String defaultValuesDisplay = createDefaultValuesDisplay( defaultValues );
- return descriptor.description() + ' ' + surround( "default: " + defaultValuesDisplay, '(', ')' );
+ return descriptor.description() + ' ' + surround( Messages.getString( "BuiltinHelpFormatter.default" ) + defaultValuesDisplay, '(', ')' );
}
private String createDefaultValuesDisplay( List<?> defaultValues ) {
View
47 src/main/java/joptsimple/Messages.java
@@ -0,0 +1,47 @@
+/*
+ The MIT License
+
+ Copyright (c) 2004-2011 Paul R. Holser, Jr.
+
+ Permission is hereby granted, free of charge, to any person obtaining
+ a copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+package joptsimple;
+
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+public class Messages {
+ private static final String BUNDLE_NAME = "joptsimple.messages";
+
+ private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle( BUNDLE_NAME, Locale.getDefault() );
+
+ private Messages() {
+ }
+
+ public static String getString( String key ) {
+ try {
+ return RESOURCE_BUNDLE.getString( key );
+ } catch ( MissingResourceException e ) {
+ return '!' + key + '!';
+ }
+ }
+}
View
5 src/main/java/joptsimple/messages.properties
@@ -0,0 +1,5 @@
+BuiltinHelpFormatter.default=default:
+BuiltinHelpFormatter.description=Description
+BuiltinHelpFormatter.noOptionsSpecified=No options specified
+BuiltinHelpFormatter.option=Option
+BuiltinHelpFormatter.optionRequired=Option (* = required)
View
5 src/main/java/joptsimple/messages_en_US.properties
@@ -0,0 +1,5 @@
+BuiltinHelpFormatter.default=default:
+BuiltinHelpFormatter.description=Description
+BuiltinHelpFormatter.noOptionsSpecified=No options specified
+BuiltinHelpFormatter.option=Option
+BuiltinHelpFormatter.optionRequired=Option (* = required)
View
5 src/main/java/joptsimple/messages_pl_PL.properties
@@ -0,0 +1,5 @@
+BuiltinHelpFormatter.default=domy\u015Blnie:
+BuiltinHelpFormatter.description=Opis
+BuiltinHelpFormatter.noOptionsSpecified=Nie podano opcji
+BuiltinHelpFormatter.option=Opcja
+BuiltinHelpFormatter.optionRequired=Opcja (* = wymagana)
View
2 src/test/java/joptsimple/OptionParserHelpTest.java
@@ -31,6 +31,7 @@ a copy of this software and associated documentation files (the
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import static java.lang.System.*;
@@ -62,6 +63,7 @@ a copy of this software and associated documentation files (the
@Before
public final void createSink() {
+ Locale.setDefault( Locale.US );
sink = new StringWriter();
}

2 comments on commit fd647c9

@pholser
pholser commented on fd647c9 Oct 1, 2012

Thanks for this -- I suspected I'd need to add i18n support, but no one had asked yet. Glad you took the initiative to add this in! I'll pluck this commit into master soon.

@elkodus
Owner
elkodus commented on fd647c9 Oct 1, 2012

Will be glad :)
I prefer to have it in main (your) branch.

Please sign in to comment.
Something went wrong with that request. Please try again.