我想在 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/