Skip to content

Commit

Permalink
+i18n for help descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
elkodus committed Sep 29, 2012
1 parent 70b8fad commit fd647c9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/main/java/joptsimple/BuiltinHelpFormatter.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class BuiltinHelpFormatter implements HelpFormatter {


public String format( Map<String, ? extends OptionDescriptor> options ) { public String format( Map<String, ? extends OptionDescriptor> options ) {
if ( options.isEmpty() ) 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 = Comparator<OptionDescriptor> comparator =
new Comparator<OptionDescriptor>() { new Comparator<OptionDescriptor>() {
Expand All @@ -69,10 +69,10 @@ public int compare( OptionDescriptor first, OptionDescriptor second ) {
private String optionHeader( Map<String, ? extends OptionDescriptor> options ) { private String optionHeader( Map<String, ? extends OptionDescriptor> options ) {
for ( OptionDescriptor each : options.values() ) { for ( OptionDescriptor each : options.values() ) {
if ( each.isRequired() ) if ( each.isRequired() )
return "Option (* = required)"; return Messages.getString( "BuiltinHelpFormatter.optionRequired" );
} }


return "Option"; return Messages.getString( "BuiltinHelpFormatter.option" );
} }


private void addHelpLineFor( OptionDescriptor descriptor ) { private void addHelpLineFor( OptionDescriptor descriptor ) {
Expand Down Expand Up @@ -111,7 +111,7 @@ else if ( argDescription.length() > 0 )
} }


private String createOptionDisplay( OptionDescriptor descriptor ) { 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(); ) { for ( Iterator<String> iter = descriptor.options().iterator(); iter.hasNext(); ) {
String option = iter.next(); String option = iter.next();
Expand All @@ -131,7 +131,7 @@ private String createDescriptionDisplay( OptionDescriptor descriptor ) {
return descriptor.description(); return descriptor.description();


String defaultValuesDisplay = createDefaultValuesDisplay( defaultValues ); String defaultValuesDisplay = createDefaultValuesDisplay( defaultValues );
return descriptor.description() + ' ' + surround( "default: " + defaultValuesDisplay, '(', ')' ); return descriptor.description() + ' ' + surround( Messages.getString( "BuiltinHelpFormatter.default" ) + defaultValuesDisplay, '(', ')' );
} }


private String createDefaultValuesDisplay( List<?> defaultValues ) { private String createDefaultValuesDisplay( List<?> defaultValues ) {
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/joptsimple/Messages.java
Original file line number Original file line Diff line number Diff line change
@@ -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 + '!';
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/joptsimple/messages.properties
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
BuiltinHelpFormatter.default=default:
BuiltinHelpFormatter.description=Description
BuiltinHelpFormatter.noOptionsSpecified=No options specified
BuiltinHelpFormatter.option=Option
BuiltinHelpFormatter.optionRequired=Option (* = required)
5 changes: 5 additions & 0 deletions src/main/java/joptsimple/messages_en_US.properties
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
BuiltinHelpFormatter.default=default:
BuiltinHelpFormatter.description=Description
BuiltinHelpFormatter.noOptionsSpecified=No options specified
BuiltinHelpFormatter.option=Option
BuiltinHelpFormatter.optionRequired=Option (* = required)
5 changes: 5 additions & 0 deletions src/main/java/joptsimple/messages_pl_PL.properties
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
BuiltinHelpFormatter.default=domy\u015Blnie:
BuiltinHelpFormatter.description=Opis
BuiltinHelpFormatter.noOptionsSpecified=Nie podano opcji
BuiltinHelpFormatter.option=Opcja
BuiltinHelpFormatter.optionRequired=Opcja (* = wymagana)
2 changes: 2 additions & 0 deletions src/test/java/joptsimple/OptionParserHelpTest.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ a copy of this software and associated documentation files (the
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;


import static java.lang.System.*; import static java.lang.System.*;
Expand Down Expand Up @@ -62,6 +63,7 @@ public class OptionParserHelpTest extends AbstractOptionParserFixture {


@Before @Before
public final void createSink() { public final void createSink() {
Locale.setDefault( Locale.US );
sink = new StringWriter(); sink = new StringWriter();
} }


Expand Down

2 comments on commit fd647c9

@pholser
Copy link

@pholser pholser commented on fd647c9 Oct 1, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Owner Author

@elkodus elkodus commented on fd647c9 Oct 1, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.