【问题标题】:How to localize @Messages annotations in NetBeans如何在 NetBeans 中本地化 @Messages 注释
【发布时间】:2012-06-25 09:06:08
【问题描述】:

我想在 NetBeans 中使用 @Messages 注释来简化我的应用程序中的本地化。但是,我找不到任何有关如何使用此机制为其他语言添加翻译(捆绑)的信息。

使用@Messages的动作示例如下

@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")

如何使添加操作因语言而异?

【问题讨论】:

    标签: netbeans localization internationalization netbeans-7 netbeans-platform


    【解决方案1】:

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

    @Messages 注释将生成一个 Bundle.java 类和一个 Bundle.properties 文件。 Bundle.java 类将包含用于本地化的函数,而 Bundle.properties 文件包含确定根区域设置的确切字符串的键值对。

    为了正确本地化,您应该检查 Bundle.properties 文件,然后创建一个 Bundle_fr.properties 文件(用于法语)或一个 Bundle_whatever.properties 文件,其中“whatever”是您要添加的语言环境。

    然后,为您的应用程序设置区域设置后,Bundle.java 类应该使用正确的 Bundle_xx.properties 文件来本地化您对 Bundle.java 类函数的调用。

    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());
        }
    }
    

    我的捆绑包看起来像:

    Bundle.properties
        OpenIDE-Module-Name=testmodule
    Bundle_fr.properties
        OpenIDE-Module-Name=french testmodule
        CTL_AddAction=Ajouter une action
    

    【讨论】:

    • Bundle.properties 存在。它还包含其他 i18n 文本。我添加了一个本地化的属性文件,但是选择的文本是默认语言,即使使用 NbBundle 的其他文本选择了正确的区域设置文本。你会碰巧有一个我可以比较的工作示例吗?
    • 只是确保...您是否使用 Bundle.java 访问器来获取您的本地化字符串?它应该看起来像 Bundle.CTL_AddAction() 我将处理一个示例...
    • 我要替换的代码是@Messages({ "CTL_AddAction=Add Action" })。我希望“添加操作”会自动被语言环境版本取代,但事实并非如此。所以要么我不理解魔法,要么我做得不好。我在 Bundle_fr.properties 中有一个本地化文本,但它没有被拾取。我不知道如何使用注释来指定消息。也许,我缺少一些明显的东西......
    • 我已经添加了我的 Bundle.properties 文件的内容,以及在执行操作时设置默认语言环境的行。您此时在我的回答中看到的内容对我有用。首先,我的操作显示为添加操作(默认语言环境 = en,指向默认的 bundle.properties),然后一旦我执行操作,语言环境就会更改,日志和操作显示文本也会更改。
    • 谢谢。我认为我犯的错误是我在子包中定义了操作,并且我希望它能够查找基本备份的 Bundle.properties 文件。所以我在包中创建了一个新的 Bundle.properties,添加了 OpenIDE-Module-Name 属性,重新构建并且它可以工作。非常感谢:)
    猜你喜欢
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 2011-01-23
    相关资源
    最近更新 更多