Internalization in Java using ResourceBundle


In order to implement localization in your Java application, you can use ResourceBundle and PropertyResourceBundle classes, as following:

Step 1: Create a method that receives a string key for which you need the “translation”:

public String getI18NString(String key) {
    if (key == null)
        return "[" + key + "]";;
    try {
        PropertyResourceBundle resBundle = (PropertyResourceBundle) ResourceBundle.getBundle("localization");
        return resBundle.getString(key);
    } catch (Exception e) {
    return "[" + key + "]";

Step 2: Add localization files to your project, under a source folder. The default translations will be grouped under a file named like, the additional languages will use files like or


The content of such a file will be as following:


Step 3: Test the implemented method:

public void testI18N() {
    assertEquals(getI18NString("BTN_CANCEL"), "Cancel");
    Locale.setDefault(new Locale("ro", "RO"));
    assertEquals(getI18NString("BTN_CANCEL"), "Anuleaza");
    Locale.setDefault(new Locale("fr", "FR"));
    assertEquals(getI18NString("BTN_CANCEL"), "Annuler");

%d bloggers like this: