Java internationalisation properties file
Once the property files for specific locales are ready, all you need to do is initialize the resource bundle with correct Locale. Java provides two classes java. ResourceBundle and java. Locale that are used for this purpose. ResourceBundle reads the locale specific property file and you can get the locale specific value for any key. This is very helpful in making your web application texts locale specific, you can get the locale information from the HTTP request and generate the dynamic page with that locale resource bundle files.
Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related Hot Network Questions.
Question feed. Stack Overflow works best with JavaScript enabled. Simply add another properties file containing the same keys as in bundle. The multiple resources together form a resource bundle which can be used for our i18n purposes. This will simply represent an Italian speaking geographical region in Italy. Refer to this list for locales supported on Java 8. Note that we must always provide the fully-qualified class name of the base name when providing the first parameter of the ResourceBundle.
This should properly localize and print out the resource value corresponding to the it-IT locale. This is because, as we discussed earlier when initially creating property resources, our Java application will always resort to the default resource file if no match was found.
In this case it would be the bundle. How about we create a simple GUI app to get a visual idea of how locale switching looks in the User Interface of a Java I18n application? There will be an additional Switch button that switches the language of the Java app each time the user presses it. Create a new Maven project named java-i18n-gui.
Create a gui package in the project and make a new JFrame form named SwitchLang inside it. Now we need to create a resource bundle containing a few language resources and synchronize it with our SwitchLang JFrame form.
Follow these steps to achieve this:. Note that we simply set resource keys representing names of the Swing elements we created earlier and put their corresponding resource values on an en-US locale. Now, our resource bundle is nicely synced with the SwitchLang JFrame form elements. Double click on the jButtonSwitchLang button in the Editor to get us into the jButtonSwitchLangActionPerformed private method which will hold the action performed upon clicking the Switch button.
First off, we need to retrieve the it-IT locale resource bundle. Run the app and press the Switch button to check that switching between locales works properly. Handling the proper pluralization of text can become quite a necessity when dealing with Java i18n. So f or the English language it would take this form:.
However, as you can see, this is still a tedious amount of work just to handle some text about apples on a Java app. For an an Additional properties file, the name of this file begins with the name that your ResourceBundle uses as above , add codes for the language and the country, end the name with the.
To retrieve the translated value from the ResourceBundle, invoke the getString method as follows:. Set by the Java Virtual Machine when it starts up, the default Locale corresponds to the locale of the host platform. You can download a zip file of all the. It allows you to write -visually- both applets and applications for jdk 1.
0コメント