Hello World: A New Requirement
I am proud to say that this blog gets hits from all over the world. (Hello Finland, France, Brazil!)
These hits help me with the Big Picture, as does the calendar of Paris on my desk: it is a big world out there. Many people, many cultures, many languages.
Which led me to this thought: it bothers me that "Hello World" is always in English.
I think the venerable first program should always be internationalized, because:
- It helps keep the Big Picture in mind. I18n is one of the universal issues of computing
- In addition to showing language syntax, it shows the i18n features. "What do you mean the language doesn't have Unicode support?"
- In an education environment, it teaches i18n to children from the start while reminding them that there's more than English. It also teaches issues of font, Unicode, etc.
Here's a version in Groovy. It presumes that there is an i18nized labels.properties file in the current directory.
For example: labels_fr_ca.properties would contain
msg=Salut Monde!
Idea: why not leave "Hello World", in a different language, as a comment? Be sure to specify language, country, and variant codes as appropriate. (This is Java-centric: see here for details)
If you leave a comment, and email me at codetojoy through gmail, I'll send you a sticker!
The Groovy version:
// Requires labels_[Locale].properties
// with definition of key called 'msg'
//
// Locale = the various ways of specifying locale in Java
Locale locale
boolean hasLanguage = ( args.length >= 1 && args[0].length() > 0 )
boolean hasCountry = ( args.length >= 2 && args[1].length() > 0 )
boolean hasVariant = ( args.length >= 3 && args[2].length() > 0 )
if( hasLanguage && hasCountry && hasVariant ) {
locale = new Locale(args[0], args[1], args[2])
} else if ( hasLanguage && hasCountry ) {
locale = new Locale(args[0], args[1])
} else if ( hasLanguage ) {
locale = new Locale(args[0])
} else {
locale = Locale.getDefault()
}
ResourceBundle labels = ResourceBundle.getBundle("labels", locale)
println labels.getString("msg")