By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,651 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,651 IT Pros & Developers. It's quick & easy.

How can I change the Language of the Program in Java code?

P: 28
Hi everyone,
In my Download Manager Java project, I put two options for the user to change the langauge (Turkish and English).My project is written according to MVC structure, so I have also view(User interface).I prepared JFrame and I put Options and Language item in it.Language extens to English and Turkish.When the user selects Turkish all the text in the program should be returned to Turkish.I made also some Google search and saw Resorcebundle examples, but I couldn't apply it to my project.How can write Java code to change the language?Thanks.
Apr 24 '13 #1

✓ answered by chaarmann

Without showing some piece of code, and without giving any reasons why you couldn't apply resource bundles, I can give you general advice with an easier to understand, homebrew method:
Use keys when you need to to print/assign the words in different languages.
Put all your keys and its associated english words in a file "english.properties"
For example:
TURKEY=Turkey
SELECTION_NEEDED=Please select an item
Put all your keys and its associated turkish words in a file "turkish.properties"
TURKEY=Türkiye
SELECTION_NEEDED=...
Then read this text into a property object:
Expand|Select|Wrap|Line Numbers
  1. Properties englishProperties = new Properties();
  2. englishProperties.load(new FileInputStream("english.properties"));
  3. Properties turkishProperties = new Properties();
  4. turkishProperties.load(new FileInputStream("turkish.properties"));
  5.  
Now, whenever you need a text in your program, don't hardcode it there, but read it from the properties. For example a language button:
Expand|Select|Wrap|Line Numbers
  1. public String getText(String language, String key) {
  2.   if (language.equals("English")) {
  3.     return englishProperties.getProperty(key);
  4.   }
  5.   if (language.equals("Turkish")) {
  6.     return turkishProperties.getProperty(key);
  7.   }
  8.   return null;
  9. }
  10. ...
  11. String currentLanguage = ... // get the preferred language from the user
  12. myButton = ... // get the button GUI element
  13. myMessageScreen= ... // get the message screen GUI element
  14. myButton.setText(getText(currentLanguage, "TURKEY"));
  15. myMessageScreen.setText(getText(currentLanguage, "SELECTION_NEEDED"));
  16.  
This way, it is easy to implement more languages, like a german.properties file.

Improvements:
- pass language as Enum, not as String
- read properties from database, not from file (easier deployment, no problems with special characters etc.)

Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
What did you try and where did you fail?
Apr 25 '13 #2

Expert 100+
P: 785
Without showing some piece of code, and without giving any reasons why you couldn't apply resource bundles, I can give you general advice with an easier to understand, homebrew method:
Use keys when you need to to print/assign the words in different languages.
Put all your keys and its associated english words in a file "english.properties"
For example:
TURKEY=Turkey
SELECTION_NEEDED=Please select an item
Put all your keys and its associated turkish words in a file "turkish.properties"
TURKEY=Türkiye
SELECTION_NEEDED=...
Then read this text into a property object:
Expand|Select|Wrap|Line Numbers
  1. Properties englishProperties = new Properties();
  2. englishProperties.load(new FileInputStream("english.properties"));
  3. Properties turkishProperties = new Properties();
  4. turkishProperties.load(new FileInputStream("turkish.properties"));
  5.  
Now, whenever you need a text in your program, don't hardcode it there, but read it from the properties. For example a language button:
Expand|Select|Wrap|Line Numbers
  1. public String getText(String language, String key) {
  2.   if (language.equals("English")) {
  3.     return englishProperties.getProperty(key);
  4.   }
  5.   if (language.equals("Turkish")) {
  6.     return turkishProperties.getProperty(key);
  7.   }
  8.   return null;
  9. }
  10. ...
  11. String currentLanguage = ... // get the preferred language from the user
  12. myButton = ... // get the button GUI element
  13. myMessageScreen= ... // get the message screen GUI element
  14. myButton.setText(getText(currentLanguage, "TURKEY"));
  15. myMessageScreen.setText(getText(currentLanguage, "SELECTION_NEEDED"));
  16.  
This way, it is easy to implement more languages, like a german.properties file.

Improvements:
- pass language as Enum, not as String
- read properties from database, not from file (easier deployment, no problems with special characters etc.)
Apr 25 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.