Home Tutorials I18N Change language according to the locale
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Change language according to the locale

Advertisement
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.

ChangeLanguage.java 

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

public class ChangeLanguage {

 public ChangeLanguage(Locale currentLocale) {

 System.out.println("currentLocale = " + currentLocale.toString());
 System.out.println();
 ResourceBundle bundle = ResourceBundle.getBundle("ChoiceBundle",
 currentLocale);
 MessageFormat messageFormat = new MessageFormat("");
 messageFormat.setLocale(currentLocale);
 double[] fileLimits = {012};
 String[] fileStrings = {
   bundle.getString
("noFiles"),
 bundle.getString("oneFile"),
   bundle.getString
("multipleFiles")
  };
 ChoiceFormat choiceForm = new ChoiceFormat(fileLimits, fileStrings);
   String pattern = bundle.getString("pattern");
 Format[] formats = {choiceForm, null, NumberFormat.getInstance()};
   messageFormat.applyPattern(pattern);
 messageFormat.setFormats(formats);
   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);
  System.out.println(result);
  }
  }
public static void main(String args[]){
  new ChangeLanguage(new Locale("en""US"));
  System.out.println();
  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

     

Advertisement

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

Ask Questions?    Discuss: Change language according to the locale  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
user
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)
Qess
November 17, 2013
Fix:

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