Home Tutorials I18N Change language according to the locale



Change language according to the locale
Posted on: September 13, 2008 at 12:00 AM
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

     

Related Tags for Change language according to the locale:
clanguagelocalechangethislocalshowexampletoexamlanwsshehangincalmcaagelochowxaxampsishaivmplcodcodeccssrdthshoavbelopleplodeo


More Tutorials from this section

Ask Questions?    Discuss: Change language according to the locale  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.