2013-03-04 40 views
0

例如,我有一個Language類,它將包含類型String的常量屬性,它將在整個程序中基於啓動程序時的設置打印xyStrings如何根據設置本地化程序的文本?

我該怎麼做?我經常使用Enum,但我缺乏經驗,沒有正確應用它們;嘗試了不同的類,繼承Language,並且必須指定每個String的值,但看起來像是自制的Enum。作爲一個加號,我很想擁有包含每個String內容的XML文件,但是我完全不知道如何在java中獲取XML文件的值(我可能知道如何分開執行)。如果可能的話,一個小例子或文檔。

謝謝。

+3

http://docs.oracle.com/javase/tutorial/i18n/index.html – 2013-03-04 15:48:55

+0

好像我根本不是很難,因爲我可以搜索。謝謝! – Alxe 2013-03-04 15:49:56

回答

1

您可以使用屬性文件,即命名它們en-GB.properties,en-US.properties等,這是文件的樣子:

en-GB.properties

file=File 
edit=Edit 

it-IT.properties

file=File 
edit=Modifica 

在「=」符號之前的字符串是屬性名稱,之後它的字符串是屬性值,這是訪問它的代碼:

Properties prop = new Properties(); 

    try { 
     //load a properties file 
     prop.load(new FileInputStream("it-IT.properties")); 

     //get the property value and print it out 
     System.out.println(prop.getProperty("file")); //prints "File" 
     System.out.println(prop.getProperty("edit"));//prints "Modifica" 

    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 

希望這有助於

相關問題