简体   繁体   English

如何忽略ResourceBundle.getBundle中的defaultLocale?

[英]How to ignore defaultLocale in ResourceBundle.getBundle?

Currently i am learning i18n and found that the Locale returned by 目前,我正在学习i18n,发现由

Locale.getDefault()

influence the look up process for the resource. 影响资源的查找过程。 The example is taken from The Java™ Tutorials - A Quick Example slightly modified: 该示例摘自The Java™Tutorials-一个经过稍微修改的快速示例

org.i18n.I18NSample org.i18n.I18NSample

package org.i18n;

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 defaultLocale = Locale.getDefault();
        System.out.println("default language = " + defaultLocale.getLanguage()
                  + "; default country = " + defaultLocale.getCountry());

        Locale inputLocale = new Locale(language, country);
        System.out.println("input language = " + inputLocale.getLanguage()
                  + "; input country = " + inputLocale.getCountry());

        ResourceBundle messages = ResourceBundle.getBundle("MessagesBundle", inputLocale);
        Locale bundleLocale = messages.getLocale();
        System.out.println("bundle language = " + bundleLocale.getLanguage()
                  + "; bundle country = " + bundleLocale.getCountry());

        System.out.println(messages.getString("greetings"));
        System.out.println(messages.getString("inquiry"));
        System.out.println(messages.getString("farewell"));
    }
}

defined default resource MessagesBundle.properties 定义的默认资源MessagesBundle.properties

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

and 3 additional resource files 和3个其他资源文件

MessagesBundle_de_DE.properties MessagesBundle_de_DE.properties

greetings = Hallo.
farewell = Tschüß.
inquiry = Wie geht's?

MessagesBundle_en_US.properties MessagesBundle_zh_CN.properties

greetings = Hi.
farewell = Bye-bye.

MessagesBundle_fr_FR.properties MessagesBundle_fr_FR.properties

greetings = Bonjour.
farewell = Au revoir.
inquiry = Comment allez-vous?

until now everything is OK java -jar I18nFirstTest.jar fr FR gives: 到现在为止一切正常java -jar I18nFirstTest.jar fr FR给出:

default language = pl; default country = PL
input language = fr; input country = FR
bundle language = fr; bundle country = FR
Bonjour.
Comment allez-vous?
Au revoir.

java -jar I18nFirstTest.jar nl NL gives: java -jar I18nFirstTest.jar nl NL给出:

default language = pl; default country = PL
input language = nl; input country = NL
bundle language = ; bundle country =
Hello.
How are you?
Goodbye.

now, I add the following resource file MessagesBundle_pl_PL.properties 现在,我添加以下资源文件MessagesBundle_pl_PL.properties

farewell = Do widzenia.
greetings = Dzie\u0144 dobry.
inquiry = \u0104\u0105\u0106\u0107\u0118\u0119\u0141\u0142\u0143\u0144Óó\u015A\u015B\u0179\u017A\u017B\u017C

and there the problem arises java -jar I18nFirstTest.jar nl NL gives: 并且出现了问题java -jar I18nFirstTest.jar nl NL给出了:

default language = pl; default country = PL
input language = nl; input country = NL
bundle language = pl; bundle country = PL
Dzień dobry.
ĄąĆćĘꣳŃńÓ󌜏źŻż
Do widzenia.

the reason pl_PL is set in the bundle is described here public static ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader) - extract: 此处描述了在捆绑包中设置pl_PL的原因,这里描述了公共静态ResourceBundle getBundle(String baseName,Locale locale,ClassLoader loader) -提取:

If no matching resource bundle is found, the default control's
getFallbackLocale method is called, which returns the current
default locale. A new sequence of candidate locale names is
generated using this locale and and searched again, as above. 

and the default locale is indeed pl_PL , but that is NOT what I want to get by the design --> there is default resource, right? 并且默认语言环境确实是pl_PL ,但这不是我想要的设计->有默认资源,对吗?

QUESTION: 题:
How do I remove that functionality of the getBundle method, that it processes the second look up using my default Locale? 如何删除使用我的默认语言环境处理第二次查找的getBundle方法的功能?

I don't want to change my locale to Locale.setDefault(new Locale("en", "GB")) because it will influence the functionality of other modules 我不想将语言环境更改为Locale.setDefault(new Locale("en", "GB"))因为这会影响其他模块的功能

Switching between locales may also not be thread-safe operation (right?) 在语言环境之间切换也可能不是线程安全操作(对吗?)

...
    Locale defaultLocale = Locale.getDefault();
    Locale.setDefault(new Locale("en", "GB"));
    ResourceBundle messages = ResourceBundle.getBundle("MessagesBundle", inputLocale);
    Locale.setDefault(defaultLocale);
...

So what is the solution? 那么解决方案是什么?

When loading a resource bundle Java basically first checks if there is a bundle for the requested locale, then if there is a bundle for the default locale and if neither of those is present it returns the base bundle (bundle for Locale("") ). 加载资源包时,Java基本上首先会检查是否存在用于请求的语言环境的捆绑软件,然后是否存在用于默认语言环境的捆绑软件,并且如果两者都不存在,它将返回基本捆绑软件( Locale("")捆绑软件) 。

As stated in the fragment of documentation you quoted the default locale is provided by the getFallbackLocale() method of control. 如文档片段中所述,您引用的默认语言环境由控件的getFallbackLocale()方法提供。 ResourceBundle provides a static Control class that controls the process. ResourceBundle提供了Control过程的静态Control类。 It is used by default when loading a ResourceBundle but you can provide your own implementation of Control as well. 加载ResourceBundle时默认使用它,但是您也可以提供自己的Control实现。

What should suffice in your case is: 在您的情况下应满足以下条件:

public class ModifiedControl extends ResourceBundle.Control {

    @Override
    public Locale getFallbackLocale( String aBaseName, Locale aLocale ) {
        if( aBaseName == null || aLocale == null ) {
            throw new NullPointerException();
        }
        return null;
    }
}

Because the method returns null ResourceBundle will return the default resource. 因为该方法返回null,所以ResourceBundle将返回默认资源。

[...] [...]
5. The control.getFallbackLocale method is called to get a fallback locale (alternative to the current target locale) to try further finding a resource bundle. 5.调用control.getFallbackLocale方法以获取回退语言环境(替代当前目标语言环境),以尝试进一步查找资源束。 If the method returns a non-null locale, it becomes the next target locale and the loading process starts over from Step 3. Otherwise, if a base bundle was found and put on hold in a previous Step 5, it is returned to the caller now. 如果该方法返回一个非null的语言环境,它将成为下一个目标语言环境,并且加载过程将从步骤3重新开始。否则,如果找到了基包并在上一个步骤5中将其保留,则将其返回给调用方现在。 Otherwise, a MissingResourceException is thrown. 否则,将抛出MissingResourceException。
[...] [...]
source: https://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html#getBundle(java.lang.String,%20java.util.Locale,%20java.lang.ClassLoader,%20java.util.ResourceBundle.Control) 来源: https : //docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html#getBundle(java.lang.String,%20java.util.Locale,%20java.lang.ClassLoader, %20java.util.ResourceBundle.Control)

Then you must simply provide an instance of your modified control when loading a resource bundle. 然后,您必须在加载资源包时简单地提供修改后的控件的实例。

ResourceBundle messages = ResourceBundle.getBundle("MessagesBundle", inputLocale, new ModifiedControl() );

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

相关问题 ResourceBundle.getBundle和Websphere - ResourceBundle.getBundle and Websphere java ResourceBundle.getBundle()不明确 - java ResourceBundle.getBundle() undeterministic Java ResourceBundle.getBundle如何加载属性文件? - How does Java ResourceBundle.getBundle load properties file? Java ResourceBundle.getBundle缺少资源异常 - Java ResourceBundle.getBundle missing resource exception ResourceBundle.getBundle(class,locale)没有获得默认捆绑 - ResourceBundle.getBundle(class,locale) don't get default Bundle 为什么ResourceBundle.getBundle(String,Locale)忽略了Locale? - Why is ResourceBundle.getBundle(String, Locale) ignoring the Locale? ResourceBundle.getBundle() 访问属性文件的默认路径是什么 - What is the default path of ResourceBundle.getBundle() to access the properties file 在Android应用程序中使用ResourceBundle.getBundle()的正确方法是什么? - What is the proper way to use ResourceBundle.getBundle() in an Android application? 无法使用 ResourceBundle.getBundle() 加载 ResourceBundle 找不到基本名称的包,区域设置 en_US - Unable to load ResourceBundle with ResourceBundle.getBundle() Can't find bundle for base name , locale en_US Java 1.5,Java EE 5,WAS 6.1:使用ResourceBundle.getBundle(…)加载资源束的异常 - Java 1.5, Java EE 5, WAS 6.1: Exception loading a resource bundle with ResourceBundle.getBundle(…)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM