netbeans - 如何在 NetBeans 中本地化 @Messages 注释

标签 netbeans localization internationalization netbeans-7 netbeans-platform

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

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

如何让添加操作根据语言而变化?

最佳答案

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 看起来像:

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

关于netbeans - 如何在 NetBeans 中本地化 @Messages 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11186312/

相关文章:

java - Netbeans 8.0 和网络项目太慢

java - Netbeans 中奇怪的 "illegal start of expression"错误

java - 在没有包或类规范的情况下从命令行执行 Java 应用程序?

xcode - 本地化无法读取字符串文件 (Xcode 6.3.2)

ios - 本地化选项卡中没有语言 (tuk) 支持,xcode 4 的下拉列表

php - Gettext 不能通过 php-cli 工作,但可以在 php-apache 中工作

Grails i18n UrlMappings

java - Netbeans 7.2.1 mac javadoc 未找到 Java Me

c# - 在 ASP.NET Core 中嵌入 View 本地化

c# - 在 URL 或 Session 或其他中存储语言