简体   繁体   English

Java国际化 - 我是否必须为每个类加载资源包?

[英]Java internationalization - Do I have to load a resource bundle for every class?

noob here. 诺布在这里。

I'm trying to implement internationalisation to my command line based program. 我正在尝试实现基于命令行的程序的国际化。 Below is what is available in the java internationalisation trail. 以下是java国际化路径中提供的内容。

import java.util.*;

public class I18NSample {

    static public void main(String[] args) {

        String language;
        String country;

        if (args.length != 2) {
            language = new String("en");
            country = new String("US");
        } else {
            language = new String(args[0]);
            country = new String(args[1]);
        }

        Locale currentLocale;
        ResourceBundle messages;

        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 obviously works, but I have several classes (currently not in a package). 这显然有效,但我有几个类(目前不在一个包中)。 Do I need to load the same bundle in all of these classes in order to use them? 我是否需要在所有这些类中加载相同的包才能使用它们?

What I'd like to end up with, is at the beginning of the program, let the user choose which language they'd like to use (from a list of available .properties files), by having them enter a command that will load the specific file. 我想最终得到的是,在程序的开头,让用户选择他们想要使用的语言(从可用的.properties文件列表中),让他们输入一个将加载的命令具体文件。

Is this possible? 这可能吗?

Thanks 谢谢

You can create a helper class with a public static method for your getString method. 您可以使用getString方法的公共静态方法创建一个帮助器类。 Something like: 就像是:

public class Messages {
    private static Locale locale;

    public static void setLocale(Locale locale) {
        Messages.locale = locale;
    }

    public static String getString(String key) {
        return ResourceBundle.getBundle("MessagesBundle", locale).getString(key);
    }
}

After setting the locale for Messages, you can get messages by 设置消息的区域设置后,您可以通过以下方式获取消息

Messages.getString("greetings");

There doesn't seem to be any reason why all of your classes couldn't share the same Locale and ResourceBundle . 似乎没有任何理由为什么所有类都无法共享相同的LocaleResourceBundle I'm assuming that even though your classes aren't all in the same package, you're using them all within the same application. 我假设即使您的类不在同一个包中,您也可以在同一个应用程序中使用它们。 You just need to make them public or provide public getters. 您只需要公开它们或提供公共吸气剂。 For instance: 例如:

public class YourClass {

    private static Locale currentLocale;
    private static ResourceBundle messages;

    static public void main(String[] args) {

        String language;
        String country;

        if (args.length != 2) {
            language = new String("en");
            country = new String("US");
        } else {
            language = new String(args[0]);
            country = new String(args[1]);
        }

        currentLocale = new Locale(language, country);

        messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
    }

    public static Locale getCurrentLocale() {
        return currentLocale;
    }

    public static ResourceBundle getMessages() {
        return messages;
    }
}

From your other classes, you can call: 在其他课程中,您可以致电:

Locale currentLocale = YourClass.getCurrentLocale();
ResourceBundle messages = YourClass.getMessages();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Java中的资源包以国际化方式加载多个特定于语言环境的属性文件 - Load multiple locale specific properties files in internationalization with resource bundle in java JavaFX8。 是否必须为每个FXMLLoader设置资源束? - JavaFX8. Do I have to set the resource bundle for every FXMLLoader? 使用Java国际化(i18n)修改Wicket的XML资源包 - Modify Wicket's XML Resource Bundle with Java internationalization (i18n) 在 Java 中,我如何找出我的资源包可用的语言 - In Java how do I find out what languages I have available my Resource Bundle 如何使用/导入要在 HTML 文件(非 JSP)中使用的 Java 区域设置类和资源包属性? - How Do I Use/Import my Java Locale class and Resource Bundle properties to be used in my HTML files (not JSP)? 如何从 Java 中的文件资源加载资源包? - How to load a resource bundle from a file resource in Java? 我必须在Java中的每个方法中编写Class.forname(“ com.mysql.jdbc.Driver”)吗? - do i have to write Class.forname(“com.mysql.jdbc.Driver”) in every method in java? 如何使用类加载器作为文件加载资源? - How do I load resource using Class Loader as File? 在运行时加载资源包 - Load resource bundle at runtime 如何使用Java加载插件类? - How do i load a plugin class with Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM