爲了國際化一個程序,我給了下面的代碼,允許我使用多種語言,具體取決於用戶選擇的內容。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 []參數。有沒有辦法解決?
這不是一個真正的國際化問題,它的代碼結構問題。我會回答這個問題,但它可能有助於獲得其他答案來編輯您的標題。 – Freiheit 2014-11-23 15:06:22
另外,用'new String(x)'複製字符串沒有任何必要或有益處。只需將'「en」「US」'或'args [0] args [1]'傳遞給'Locale'。 – 2014-11-24 01:07:51