Internationalization in JRuby

by on April 30, 2007

I have been using and teaching Java since 1995. The other day, I was talking to my students about Internationalization in Java.

Internationalization is the process of designing an application so that it can be adapted to various languages and regions without engineering changes.

Localization is the process of adapting software for a specific region or language by adding locale-specific components and translating text.

The following is a simple example (in Java) to demonstrate how to internationalize a program so that it displays text messages in the appropriate language. Notice that the text of the messages is not hardcoded.

import java.util.*;
public class InternationalizationEx
{
  public static void main(String[] args)
  {
    String lang, country;
    Locale cLocale;
    ResourceBundle msg;

    lang = new String("de");
    country = new String("DE");

    cLocale = new Locale(lang, country);
    msg = ResourceBundle.getBundle("MessagesBundle", cLocale);

    System.out.println(msg.getString("greetings"));
    System.out.println(msg.getString("welcome"));
  }
}

To compile and run the above program, you need these source files:

The internationalized program uses a language and a country code. In the above example the language code is de (German) and the country code is DE (Germany), so the program displays the messages in German:

java InternationalizationEx
Hallo
HeiBen Sie Willkommen nach Indien

If the language code is en (English) and the country code is US (United States), the program displays the messages in English:

java InternationalizationEx
Hello
Welcome to India

The arguments passed to the getBundle method identify which properties file will be accessed. The first argument, MessagesBundle, refers to this family of properties files:

  • MessagesBundle_de_DE.properties
  • MessagesBundle_en_US.properties

The Locale, which is the second argument of getBundle, specifies which of the MessagesBundle files is chosen. When the Locale was created, the language code and the country code were passed to its constructor. Note that the language and country codes follow MessagesBundle in the names of the properties files.

You may be wondering what’s the point of this post and that too about code in Java, whereas RubyLearning.com is a blog that talks about Ruby and Rails!

Internationalization in JRuby

Well, since I am currently exploring JRuby, I wanted to call these Java classes in Ruby using JRuby. In Ruby, one can use Masao Mutoh’s Ruby-GetText-Package to do the same, though personally I find it quite cumbersome to use.

So here’s JRuby to my rescue! The program code in JRuby is:

#javaInternational.rb
require 'java'

module JavaLang
  include_package "java.lang"
end

ResourceBundle = java.util.ResourceBundle
Locale = java.util.Locale

lang = JavaLang::String.new("de")
country = JavaLang::String.new("DE")

cLocale = Locale.new(lang, country)
msg = ResourceBundle.getBundle("MessagesBundle", cLocale)

puts msg.getString("greetings")
puts msg.getString("welcome")

Run the program on a command prompt as follows:

jruby javaInternational.rb

It works! Okay, a little explanation of some of the JRuby code – There can be name clashes between Java class names and Ruby class names. String is an example of this; Java has java.util.String and Ruby also has Kernel::String. To resolve this name clash, define a Ruby module that includes the Java class definition as follows:

module JavaLang
  include_package "java.lang"
end

If you know Java and have started using Ruby, please do explore JRuby – you shall not be disappointed.

Technorati Tags: , , ,

Posted by Satish Talim

Follow me on Twitter to communicate and stay connected

{ 2 comments… read them below or add one }

rubylover September 14, 2007 at 1:33 pm

I am getting following error :
ResourceBundle.java:836:in `java.util.ResourceBundle.throwMissingResourceException’: java.util.MissingResourceException: Can’t find bundle for base name MessagesBundle, locale de_DE (NativeException)

I have created MessageBundle.properties , MessageBundle_de_DE.properties in same directory where my .rb files exists.

Reply

bahaw November 5, 2008 at 10:56 am

I think you have to put it in jruby classpath, that’s what I did and it worked alright.

Reply

Leave a Comment

Previous post:

Next post: