如何忽略系统默认语言环境以检索resourceBundle [英] How to ignore the system default Locale to retrieve resourceBundle

查看:119
本文介绍了如何忽略系统默认语言环境以检索resourceBundle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用java.util.ResourceBundle类和属性文件来本地化Web应用程序.

I am localizing a web application using a java.util.ResourceBundle class and property files.

我有两个语言环境fr_FR和en_US,并且我想使用en_US作为默认语言,所以我编写了以下文件:

I have two locales, fr_FR and en_US, and I want to use en_US as the default, so I wrote the following files :

  • messages_fr_FR.properties带有fr_FR消息
  • messages.properties带有en_US消息
  • messages_fr_FR.properties with fr_FR messages
  • messages.properties with en_US messages

我的问题是返回fr_FR束.

My problem is that the ResourceBundle.getBundle(BUNDLE_NAME, locale) method fall back Locale.getDefault() before using the default property file, which means if the JVM Locale is set to fr_FR, ResourceBundle.getBundle("name", new Locale("en", "US")) returns the fr_FR bundle.

我可以重命名messages.properties文件,但这可能会返回MissingResourceException以及其他所需的默认语言环境.

I could rename the messages.properties file, but this could returns a MissingResourceException with other desired and default locales.

如何在不复制属性文件或调用Locale.setDefault的情况下忽略系统区域设置?

How can I ignore the System Locale, without duplicating the property file or calling Locale.setDefault ?

推荐答案

您可以使用自定义ResourceBundle.Control来实现此目的,方法是显式重载getFallbackLocale或使用:

You can do that by using a custom ResourceBundle.Control, either by overloading the getFallbackLocaleexplicitly, or by using :

ResourceBundle.getBundle("name", new Locale("en", "US"), ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_PROPERTIES))

这篇关于如何忽略系统默认语言环境以检索resourceBundle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆