Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to localize @Messages annotations in NetBeans

I would like to use the @Messages annotation in NetBeans to simplify localization in my application. However, I can not find any information about how to add translations (bundles) for other languages using this mechanism.

Example of an action using the @Messages is as follows

@ActionID(category = "category",
id = "AddAction")
@ActionRegistration(iconBase = "actions/action-icon.png",
displayName = "#CTL_AddAction")
@ActionReferences({
    @ActionReference(path = "Menu/Shapes", position = 160),
    @ActionReference(path = "Toolbars/Shapes", position = 5133)
})
@Messages("CTL_AddAction=Add Action")

How can I get the Add Action to vary depending on the language?

like image 947
Rico Leuthold Avatar asked Jun 25 '12 09:06

Rico Leuthold


1 Answers

http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/NbBundle.Messages.html

The @Messages annotation will generate a Bundle.java class and a Bundle.properties file. The Bundle.java class will include functions for localizing, and the Bundle.properties file contains the key-value pairs that determine the exact strings for the root locale.

In order to properly localize, you should examine the Bundle.properties file, and then create a Bundle_fr.properties file (for french) or a Bundle_whatever.properties file where 'whatever' is the locale you wish to add.

Then, when the locale is set for your application, the Bundle.java class should use the correct Bundle_xx.properties file to localize your calls to the Bundle.java class functions.

package com.testmodule;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;

@ActionID(category = "category",
id = "com.testmodule.AddAction")
@ActionRegistration(iconBase = "com/testmodule/action-icon.png",
displayName = "#CTL_AddAction")
@ActionReferences({
    @ActionReference(path = "Menu/Shapes", position = 160),
    @ActionReference(path = "Toolbars/Shapes", position = 5133)
})
@Messages({
    "CTL_AddAction=Add Action"
})
public final class AddAction implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        Locale.setDefault(Locale.FRENCH);
        System.out.println("I am action "+Bundle.CTL_AddAction());
    }
}

My Bundles look like:

Bundle.properties
    OpenIDE-Module-Name=testmodule
Bundle_fr.properties
    OpenIDE-Module-Name=french testmodule
    CTL_AddAction=Ajouter une action
like image 54
naugler Avatar answered Nov 07 '22 14:11

naugler