Suthar
Suthar

Reputation: 91

How to specify, which resource file to be used while running?

I wants my application to be in both Hindi and English language, so I have created a ResourceBundle named as Resources. In this directory I have create label.properties,label_en_US.properties and label_hi_IN.properties and these file have putted some values like ab=xy_default, ab=xy_in_en and ab=xy_in_hindi respectively.
enter image description here

And Now I planned to use them as label text, so in property of that label i mantioned the default one, like thisenter image description here
Here code is key and In label_defualt is code=Code : , in label_en_US code=Code : and in label_hi_IN code=कोड :. How can I specify which of label.properties to be useout of label, label_en_US or label_hi_in. I have stored user preference in my database like which language to use.
I want to know how I can force or tell it use that particular file out of label, label_en_US, label_hi_IN in main function or somewhere else.
As now it's taking values from label.properties file only, if user want it to be in hindi then how internally we force to use that label_hi_IN.properties file.

Upvotes: 0

Views: 396

Answers (3)

Suthar
Suthar

Reputation: 91

Finally I got it, We have to just set Default Locale.

Locale locale = new Locale("hi","IN");
Locale.setDefault(locale);

Upvotes: 0

VGR
VGR

Reputation: 44414

ResourceBundle uses a “fallback” strategy. If the user’s current locale is hi-IN:

  • ResourceBundle.getBundle will look in label_hi_IN.properties (if that resource exists)
  • If that file is not found, ResourceBundle.getBundle will look for label_hi.properties (if that resource exists)
  • If that file is not found, ResourceBundle.getBundle will look for label.properties (if that resource exists)

This means you should do the following:

  • If label.properties contains English entries, remove label_en_US.properties from your project
  • If label.properties contains Hindi entries, remove label_hi_IN.properties from your project
  • Rename label_hi_IN.properties, if it is still present, to label_hi.properties, so all Hindi locales will use that file
  • Rename label_en_US.properties, if it is still present, to label_en.properties, so all English locales will use that file

Upvotes: 0

g00se
g00se

Reputation: 4296

If you've got your files sorted out OK, the right internationalization should occur naturally depending on your locale. If you want to test other supported locales in your software, set the appropriate system properties:

java -Duser.country=IN -Duser.language=hi …

or

java -Duser.country=US -Duser.language=en …

Upvotes: 1

Related Questions