Uncategorized

Internationalization

ResourceBundle

In this tutorial we are going to demonstrate the power of ResourceBundle and how we can leverage the same to make Java apps internationalize.

In order to retrieve local specific values from a properties file we need to create multiple local specific MessageBundle files in the format MessageBundle_<locale>.properties and put them in classpath.

Now based on the local in which the java application is running the application will pick message from that specific file.

package com.resourcebundle.test;

import java.util.Arrays;
import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleTest {
	public static void main(String args[]) {
		Locale[] locales = new Locale[] {
				Locale.GERMAN,
				Locale.US
		};
		Arrays.asList(locales).forEach(ResourceBundleTest::resouceBundle);

	}

	public static void resouceBundle(Locale locale) {
		ResourceBundle bundle = ResourceBundle.getBundle("MessageBundle", locale);  
		System.out.println("Locale = \'" + locale + "\' message is =\'"+bundle.getString("message")+ "\'");  
	}
}

Output

Locale = ‘de’ message is =’Er hat Glück’
Locale = ‘en_US’ message is =’He is lucky’

Git location

https://github.com/gotechmind/Miscellaneous/tree/master/ResourceBundle

Leave a Reply

Your email address will not be published. Required fields are marked *