Home Tutorials I18N Change language according to the locale

Related Tutorials


Share on Google+Share on Google+

Change language according to the locale

This Example shows you how to change language according to the locale. In the code given below we have changed language according to the locale.

Change language according to the locale


This Example shows you how to change language according to the locale. In the code given below we have changed language according to the locale.

Methods used in this example are described below :

ResourceBundle.getBundle() : ResourceBundle class object holds locale-specific objects. When a program needs a locale-specific resource on that time program can load locale from the resource bundle that is suitable for the current user's locale. Method getBundle() returns a ResourceBundle class object using the specified name and locale.

MessageFormat.setLocale() : setLocale method use to set the locale in the MessageFormat class object.


import java.text.*;
import java.util.*;

public class ChangeLanguage {

 public ChangeLanguage(Locale currentLocale) {

 System.out.println("currentLocale = " + currentLocale.toString());
 ResourceBundle bundle = ResourceBundle.getBundle("ChoiceBundle",
 MessageFormat messageFormat = new MessageFormat("");
 double[] fileLimits = {012};
 String[] fileStrings = {
 ChoiceFormat choiceForm = new ChoiceFormat(fileLimits, fileStrings);
   String pattern = bundle.getString("pattern");
 Format[] formats = {choiceForm, null, NumberFormat.getInstance()};
   Object[] messageArguments = {null, "desk"null};
for (int numFiles = 0; numFiles < 4; numFiles++) {
 messageArguments[0new Integer(numFiles);
 messageArguments[2new Integer(numFiles);
 String result = messageFormat.format(messageArguments);
public static void main(String args[]){
  new ChangeLanguage(new Locale("en""US"));
  new ChangeLanguage(new Locale("fr""FR"));

ChoiceBundle.properties :

noFiles = are no files
oneFile = is one file
multipleFiles = are {2files
pattern = There {0on {1}.

ChoiceBundle_en_US.properties :

noFiles = are no files
oneFile = is one file
multipleFiles = are {2files
pattern = There {0on {1}.

ChoiceBundle_fr_FR.properties :

noFiles = n' y a pas des fichiers
oneFile = y a un fichier
multipleFiles = y a {2fichiers
pattern = Il {0sur {1}.

output :
There are no files on desk.
There is one file on desk.
There are files on desk.
There are files on desk.

currentLocale = fr_FR

Il n' y a pas des fichiers sur desk.
Il y a un fichier sur desk.
Il y a fichiers sur desk.
Il y a fichiers sur desk.

Download code



If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 13, 2008

Related Tutorials

Discuss: Change language according to the locale  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
February 26, 2012
can run the program

this Exception is thrown when i run the program : Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name ChoiceBundle, locale en_US at java.util.ResourceBundle.throwMissingResourceException(Unknown Source) at java.util.ResourceBundle.getBundleImpl(Unknown Source) at java.util.ResourceBundle.getBundle(Unknown Source) at ChangeLanguage.<init>(ChangeLanguage.java:11) at ChangeLanguage.main(ChangeLanguage.java:36)
November 17, 2013

You need to add the location of the Java bundle file: ResourceBundle bundle = ResourceBundle.getBundle("languagetest.ChoiceBundle", currentLocale);