本文整理了Java中java.util.ResourceBundle
类的一些代码示例,展示了ResourceBundle
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ResourceBundle
类的具体详情如下:
包路径:java.util.ResourceBundle
类名称:ResourceBundle
[英]ResourceBundle is an abstract class which is the superclass of classes which provide Locale-specific resources. A bundle contains a number of named resources, where the names are Strings. A bundle may have a parent bundle, and when a resource is not found in a bundle, the parent bundle is searched for the resource. If the fallback mechanism reaches the base bundle and still can't find the resource it throws a MissingResourceException.
All bundles for the same group of resources share a common base bundle. This base bundle acts as the root and is the last fallback in case none of its children was able to respond to a request.
The first level contains changes between different languages. Only the differences between a language and the language of the base bundle need to be handled by a language-specific ResourceBundle.
The second level contains changes between different countries that use the same language. Only the differences between a country and the country of the language bundle need to be handled by a country-specific ResourceBundle.
The third level contains changes that don't have a geographic reason (e.g. changes that where made at some point in time like PREEURO where the currency of come countries changed. The country bundle would return the current currency (Euro) and the PREEURO variant bundle would return the old currency (e.g. DM for Germany).
Examples
BaseName (base bundle)
BaseName_de (german language bundle)
BaseName_fr (french language bundle)
BaseName_de_DE (bundle with Germany specific resources in german)
BaseName_de_CH (bundle with Switzerland specific resources in german)
BaseName_fr_CH (bundle with Switzerland specific resources in french)
BaseName_de_DE_PREEURO (bundle with Germany specific resources in german of the time before the Euro)
BaseName_fr_FR_PREEURO (bundle with France specific resources in french of the time before the Euro)
It's also possible to create variants for languages or countries. This can be done by just skipping the country or language abbreviation: BaseName_us__POSIX or BaseName__DE_PREEURO. But it's not allowed to circumvent both language and country: BaseName___VARIANT is illegal.
[中]ResourceBundle是一个抽象类,它是提供特定于语言环境的资源的类的超类。捆绑包包含许多命名资源,其中的名称是字符串。捆绑包可能有父捆绑包,当在捆绑包中找不到资源时,会在父捆绑包中搜索资源。如果回退机制到达基本包,但仍然找不到资源,它会抛出MissingResourceException。
*同一组资源的所有捆绑包共享一个公共基本捆绑包。这个基本捆绑包充当根,是在其子级都无法响应请求的情况下的最后一个回退。
*第一级包含不同语言之间的变化。只有一种语言和基础捆绑包的语言之间的差异需要由特定于语言的ResourceBundle来处理。
*第二个层次包含使用相同语言的不同国家之间的变化。只有一个国家和语言包中的国家之间的差异需要由一个特定于国家的资源包来处理。
*第三个级别包含没有地理原因的更改(例如,在某个时间点进行的更改,如来自国家的货币发生变化的PREEURO。国家捆绑将返回当前货币(欧元),PREEURO变体捆绑将返回旧货币(例如德国马克)。
例子
*BaseName(基本包)
*BaseName_de(德语包)
*BaseName_fr(法语包)
*BaseName_de_de(与德语中特定于德国的资源捆绑在一起)
*BaseName_de_CH(与瑞士特定的德语资源捆绑)
*BaseName_fr_CH(与瑞士特定的法语资源捆绑)
*BaseName_de_de_PREEURO(与欧元之前的德国特定资源捆绑在一起)
*BaseName_fr_fr_PREEURO(与欧元之前的法国特定资源捆绑在一起)
也可以为语言或国家创建变体。只需跳过国家或语言缩写:BaseName_us__POSIX或BaseName_DE_PREEURO即可。但不允许同时绕过语言和国家:BaseName____;变体是非法的。
代码示例来源:origin: lets-blade/blade
public String format(String key,String ... params) {
return MessageFormat.format(resourceBundle.getString(key),params);
}
}
代码示例来源:origin: xalan/xalan
throws MissingResourceException
Locale locale = Locale.getDefault();
return (ListResourceBundle)ResourceBundle.getBundle(className, locale);
return (ListResourceBundle)ResourceBundle.getBundle(
className, new Locale("en", "US"));
throw new MissingResourceException(
"Could not load any resource bundles." + className, className, "");
代码示例来源:origin: SonarSource/sonarqube
protected void initPlugin(String pluginKey) {
try {
String bundleKey = BUNDLE_PACKAGE + pluginKey;
ResourceBundle bundle = ResourceBundle.getBundle(bundleKey, Locale.ENGLISH, this.classloader, control);
Enumeration<String> keys = bundle.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
propertyToBundles.put(key, bundleKey);
}
} catch (MissingResourceException e) {
// ignore
}
}
代码示例来源:origin: jenkinsci/jenkins
/**
* Gets a human readable message for the given Win32 error code.
*
* @return
* null if no such message is available.
*/
@CheckForNull
public static String getWin32ErrorMessage(int n) {
try {
ResourceBundle rb = ResourceBundle.getBundle("/hudson/win32errors");
return rb.getString("error"+n);
} catch (MissingResourceException e) {
LOGGER.log(Level.WARNING,"Failed to find resource bundle",e);
return null;
}
}
代码示例来源:origin: SonarSource/sonarqube
public Locale getEffectiveLocale(Locale locale) {
Locale bundleLocale = ResourceBundle.getBundle(BUNDLE_PACKAGE + "core", locale, this.classloader, this.control).getLocale();
locale.getISO3Language();
return bundleLocale.getLanguage().isEmpty() ? Locale.ENGLISH : bundleLocale;
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Register bean definitions contained in a ResourceBundle.
* <p>Similar syntax as for a Map. This method is useful to enable
* standard Java internationalization support.
* @param rb the ResourceBundle to load from
* @param prefix a filter within the keys in the map: e.g. 'beans.'
* (can be empty or {@code null})
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
*/
public int registerBeanDefinitions(ResourceBundle rb, @Nullable String prefix) throws BeanDefinitionStoreException {
// Simply create a map and call overloaded method.
Map<String, Object> map = new HashMap<>();
Enumeration<String> keys = rb.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
map.put(key, rb.getObject(key));
}
return registerBeanDefinitions(map, prefix);
}
代码示例来源:origin: checkstyle/checkstyle
/**
* Gets the check message 'as is' from appropriate 'messages.properties'
* file.
*
* @param messageBundle the bundle name.
* @param messageKey the key of message in 'messages.properties' file.
* @param arguments the arguments of message in 'messages.properties' file.
* @return The message of the check with the arguments applied.
*/
private static String internalGetCheckMessage(
String messageBundle, String messageKey, Object... arguments) {
final ResourceBundle resourceBundle = ResourceBundle.getBundle(
messageBundle,
Locale.getDefault(),
Thread.currentThread().getContextClassLoader(),
new LocalizedMessage.Utf8Control());
final String pattern = resourceBundle.getString(messageKey);
final MessageFormat formatter = new MessageFormat(pattern, Locale.ROOT);
return formatter.format(arguments);
}
代码示例来源:origin: javax.el/javax.el-api
locale = Locale.getDefault();
ResourceBundle rb = null;
if (null == (rb = (ResourceBundle)
threadMap.get(locale.toString()))) {
rb = ResourceBundle.getBundle("javax.el.PrivateMessages",
locale);
threadMap.put(locale.toString(), rb);
result = rb.getString(messageId);
if (null != params) {
result = MessageFormat.format(result, params);
代码示例来源:origin: org.netbeans.api/org-openide-util
if (throwex) {
throw new IllegalArgumentException(
MessageFormat.format(
NbBundle.getBundle(MapFormat.class).getString("MSG_FMT_ObjectForKey"),
new Object[] { new Integer(key) }
代码示例来源:origin: igniterealtime/Openfire
value = bundle.getString(key);
MessageFormat messageFormat = new MessageFormat("");
messageFormat.setLocale(bundle.getLocale());
messageFormat.applyPattern(value);
try {
+ " in locale " + locale.toString());
value = "";
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testInternalResourceViewResolverWithJstlAndContextParam() throws Exception {
Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;
vr.setApplicationContext(wac);
View view = vr.resolveViewName("example1", Locale.getDefault());
assertEquals("Correct view class", JstlView.class, view.getClass());
assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());
assertEquals("message1", lc.getResourceBundle().getString("code1"));
assertEquals("message2", lc.getResourceBundle().getString("code2"));
代码示例来源:origin: wildfly/wildfly
private static ResourceBundle getResourceBundle(Locale locale) {
if (locale == null) {
locale = Locale.getDefault();
}
return ResourceBundle.getBundle(RESOURCE_NAME, locale);
}
}
代码示例来源:origin: languagetool-org/languagetool
/**
* Translate a text string based on our i18n files.
* @since 3.1
*/
public static String i18n(ResourceBundle messages, String key, Object... messageArguments) {
MessageFormat formatter = new MessageFormat("");
formatter.applyPattern(messages.getString(key).replaceAll("'", "''"));
return formatter.format(messageArguments);
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-web-jsf
public FileObject showDialog() {
JButton[] options = new JButton[]{
new JButton(NbBundle.getMessage(BrowseFolders.class, "LBL_SelectFile")), // NOI18N
new JButton(NbBundle.getMessage(BrowseFolders.class, "LBL_Cancel")), // NOI18N
};
OptionsListener optionsListener = new OptionsListener(this);
options[0].setActionCommand(OptionsListener.COMMAND_SELECT);
options[0].addActionListener(optionsListener);
options[0].getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/dialogs/Bundle").getString("ACSD_SelectFile"));
options[1].setActionCommand(OptionsListener.COMMAND_CANCEL);
options[1].addActionListener(optionsListener);
options[1].getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/web/jsf/dialogs/Bundle").getString("ACSD_Cancel"));
DialogDescriptor dialogDescriptor = new DialogDescriptor(
this, // innerPane
NbBundle.getMessage(BrowseFolders.class, "LBL_BrowseFiles"), // displayName
true, // modal
options, // options
options[ 0], // initial value
DialogDescriptor.BOTTOM_ALIGN, // options align
null, // helpCtx
null); // listener
dialogDescriptor.setClosingOptions(new Object[]{options[0], options[1]});
Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
dialog.setVisible(true);
return optionsListener.getResult();
}
代码示例来源:origin: org.jboss.jbossas/jboss-as-profileservice
public DeploymentManagerImpl()
{
currentLocale = Locale.getDefault();
formatter.setLocale(currentLocale);
i18n = ResourceBundle.getBundle(BUNDLE_NAME, currentLocale);
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-web-core
public boolean showDialog() {
dialogOK = false;
String displayName = ""; // NOI18N
try {
displayName = NbBundle.getBundle("org.netbeans.modules.web.core.palette.items.resources.Bundle").getString("NAME_jsp-GetProperty"); // NOI18N
} catch (Exception e) {
Exceptions.printStackTrace(e);
}
descriptor = new DialogDescriptor(this, NbBundle.getMessage(GetPropertyCustomizer.class, "LBL_Customizer_InsertPrefix") + " " + displayName, true, DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION, new ActionListener() { // NOI18N
public void actionPerformed(ActionEvent e) {
if (descriptor.getValue().equals(DialogDescriptor.OK_OPTION)) {
evaluateInput();
dialogOK = true;
}
dialog.dispose();
}
});
dialog = DialogDisplayer.getDefault().createDialog(descriptor);
dialog.setVisible(true);
repaint();
return dialogOK;
}
代码示例来源:origin: org.netbeans.api/org-openide-awt
private void initAccessible() {
if (nameFormat == null) {
ResourceBundle bundle = NbBundle.getBundle(SplittedPanel.class);
nameFormat = new MessageFormat(bundle.getString("ACS_SplittedPanel_Name"));
getAccessibleContext().setAccessibleName(
nameFormat.format(
new Object[] {
((firstComponent == null) || !(firstComponent instanceof Accessible)) ? null
: firstComponent.getAccessibleContext()
.getAccessibleName(),
((secondComponent == null) || !(secondComponent instanceof Accessible)) ? null
: secondComponent.getAccessibleContext()
.getAccessibleName()
ResourceBundle bundle = NbBundle.getBundle(SplittedPanel.class);
descriptionFormat = new MessageFormat(bundle.getString("ACS_SplittedPanel_Description"));
代码示例来源:origin: dcaoyuan/nbscala
getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(MainClassChooser.class).getString("AD_MainClassChooser"));
jLabel1.setLabelFor(jMainClassList);
org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getBundle(MainClassChooser.class).getString("CTL_AvaialableMainClasses"));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
jMainClassList.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(MainClassChooser.class).getString("AD_jMainClassList"));
代码示例来源:origin: net.sf.squirrel-sql.thirdparty-non-maven/openide-loaders
public String getDisplayName () {
if (format == null) {
format = new MessageFormat (NbBundle.getBundle (DataShadow.class).getString ("FMT_shadowName"));
}
String n = format.format (createArguments ());
try {
obj.getPrimaryFile().getFileSystem().getStatus().annotateName(n, obj.files());
} catch (FileStateInvalidException fsie) {
// ignore
}
return n;
}
代码示例来源:origin: robovm/robovm
if (cl != null) {
try {
return ResourceBundle.getBundle(resourceBundleName, Locale.getDefault(), cl);
} catch (MissingResourceException ignored) {
if (cl != null) {
try {
return ResourceBundle.getBundle(resourceBundleName, Locale.getDefault(), cl);
} catch (MissingResourceException ignored) {
throw new MissingResourceException("Failed to load the specified resource bundle \"" +
resourceBundleName + "\"", resourceBundleName, null);
内容来源于网络,如有侵权,请联系作者删除!