2014-11-23 42 views
1

爲了國際化一個程序,我給了下面的代碼,允許我使用多種語言,具體取決於用戶選擇的內容。Java - 內部化問題

import java.util.*; 
public class I18NSample { 
    static public void main(String[] args) { 
    String language, country; 
    if (args.length != 2) { // default is English-language 
     language = new String("en"); country = new String("US"); 
    } else { 
     language = new String(args[0]); country = new String(args[1]); 
    } 
    Locale currentLocale = new Locale(language, country); 
    ResourceBundle messages = 
     ResourceBundle.getBundle("MessagesBundle", currentLocale); 
    System.out.println(messages.getString("greetings")); 
    System.out.println(messages.getString("inquiry")); 
    System.out.println(messages.getString("farewell")); 
    } 
} 

這是我MessagesBundle文件:

greetings = Hello. 
farewell = Goodbye. 
inquiry = How are you? 

但是在執行我的程序這段代碼的時候,我不能夠使用messages.getString功能在其他班級,我需要在此代碼我的主,因爲它需要String []參數。有沒有辦法解決?

+0

這不是一個真正的國際化問題,它的代碼結構問題。我會回答這個問題,但它可能有助於獲得其他答案來編輯您的標題。 – Freiheit 2014-11-23 15:06:22

+0

另外,用'new String(x)'複製字符串沒有任何必要或有益處。只需將'「en」「US」'或'args [0] args [1]'傳遞給'Locale'。 – 2014-11-24 01:07:51

回答

2

當然,只是移動信息intitialization你的類以上並予以公佈,這樣的:

import java.util.*; 
public class I18NSample { 
    public ResourceBundle messages; 
    static public void main(String[] args) { 
    String language, country; 
    if (args.length != 2) { // default is English-language 
     language = new String("en"); country = new String("US"); 
    } else { 
     language = new String(args[0]); country = new String(args[1]); 
    } 
    Locale currentLocale = new Locale(language, country); 
    messages = 
     ResourceBundle.getBundle("MessagesBundle", currentLocale); 
    System.out.println(messages.getString("greetings")); 
    System.out.println(messages.getString("inquiry")); 
    System.out.println(messages.getString("farewell")); 
    } 
} 

這樣的話,你會採用B能夠從其他類訪問I18NSample.ResourceBundle

0

所以你在這裏不是一個國際化問題,它是一個代碼結構問題。

您的messages變量是主要方法的局部變量。將它作爲類的字段,然後在main中初始化它。

下面的例子是向你展示我的意思。以類似的方式初始化您的語言環境可能很有意義。

import java.util.*; 

public class I18NSample { 

    private ResourceBundle messages; //this may have to be static to initialize it in main 

    static public void main(String[] args) { 
    String language, country; 
    if (args.length != 2) { // default is English-language 
     language = new String("en"); country = new String("US"); 
    } else { 
     language = new String(args[0]); country = new String(args[1]); 
    } 
    Locale currentLocale = new Locale(language, country); 
    messages = 
     ResourceBundle.getBundle("MessagesBundle", currentLocale); 
    System.out.println(messages.getString("greetings")); 
    System.out.println(messages.getString("inquiry")); 
    System.out.println(messages.getString("farewell")); 
    } 

    //this getter exposes the ResourceBundle so you can use it outside of this class 
    public ResourceBundle getMessages() { return messages; } 
} 
+0

如果它是私密的,他將如何從其他方法訪問它? – 2014-11-23 15:09:57

+0

'private'字段可以被同一類中的其他方法訪問。如果它需要被其他類訪問,那麼可以很容易地引入一個公共的getter方法。 – Freiheit 2014-11-23 15:11:29

+0

「我不能在其他類中使用messages.getString函數」我認爲他需要通過類來訪問它......這也導致了一些問題,因爲所有這些都不能是靜態的 – 2014-11-23 15:12:38