5

我想在NetBeans中使用@Messages註釋來簡化我的應用程序中的本地化。但是,我找不到任何有關如何使用此機制爲其他語言添加翻譯(捆綁)的信息。使用@Messages動作的如何在NetBeans中本地化@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") 

我怎樣才能添加行動取決於語言有什麼不同?

回答

6

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 
+0

的Bundle.properties存在。它還包含其他i18n文本。我添加了一個本地化的屬性文件,但是拾取的文本是默認語言,即使其他使用NbBundle的文本拾取正確的語言環境文本。你會碰巧有一個我可以比較的實例嗎? – Nasir

+0

只要確保...您是否使用Bundle.java訪問器來獲取您的本地化字符串?它應該看起來像Bundle.CTL_AddAction() 我會在一個例子上工作... – naugler

+0

我想要替換的代碼是@Messages({CTL_AddAction = Add Action「 })。我預計「添加動作」將自動地由區域設置版本取代,但事實並非如此。所以要麼我不明白這個魔法,要麼我沒有做好。我在Bundle_fr.properties中有一個本地化的文本,但它沒有被拾取。我不知道如何使用註釋來指定消息。也許,有一些明顯的我缺少... – Nasir