gpt4 book ai didi

Java - 内部化问题

转载 作者:行者123 更新时间:2023-11-30 03:39:29 24 4
gpt4 key购买 nike

为了使程序国际化,我获得了以下代码,允许我根据用户选择使用多种语言。

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 函数,并且我在 main 中需要此代码,因为它需要 String[] 参数。有没有办法解决?

最佳答案

当然,只需将消息初始化移至类上方并将其公开,如下所示:

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"));
}
}

这样,您就可以使用 I18NSample.ResourceBundle 从其他类访问它

关于Java - 内部化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27090344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com