将resourceBundle与外部文件一起使用java [英] Using resourceBundle with an external file, java

查看:385
本文介绍了将resourceBundle与外部文件一起使用java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读其他问题和答案,但我没有得到资源边界如何工作完整。
我认为在Joomla中使用多语言选项的方式类似。
基本上,您有针对不同语言要阅读的不同消息的文件。
所以我在src / Lang文件夹中创建了System.properties
我创建内部

I have been reading other questions and answers around this but I am not getting how resource boundle works for completly. I think it is similar at Joomla way of using multilingual options. Basically you have files for the different messages you want to read for different languages. So I created System.properties inside my src/Lang folder Inside I create

STARTING_MYAPP=Starting my app

我可能在这里错了,但无论如何我无法读取默认文件。现在我脑子死了,我不能理解我读到的任何东西
这就是我这样做的方式

I might be wrong here, but anyways I am not able to read the default file. Right now I am brain death, and I cant make sense of anything I read this is how I am doing this

Locale locale = Locale.getDefault();
String basename ="System";
ResourceBundle resourceBundle = ResourceBundle.getBundle(basename, locale);
System.out.println(resourceBundle.getString("STARTING_MYAPP"));
//UserPreferences UserPrefs = new UserPreferences();

当我调用getBundle()时,它有两个输入,basename和locale,right。我在使用basename时遇到问题。如果我的文件在src / lang / System.properties中,我该如何查找它。 basename =System,basename =Lang / System,basename =System.properties,basename =myProject.label。

when I call getBundle(),it has two inputs, the basename and the locale, right. I am having trouble with the basename. If my file is inside src/lang/System.properties, how should I look for it. basename="System", basename="Lang/System", basename="System.properties", basename="myProject.label".

我尝试了所有这一切和一些更多的组合,但没有工作,我总是得到像
线程主java.util.MissingResourceException异常:无法找到基本名称系统的捆绑,locale en_US

I tried all of this and some more combinations but non work, I always get something like Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name System, locale en_US

关于语言环境,我如何将其保留为默认值,因此我不必使用System_en_US.properties或类似的东西。

About the locale, how I leave it up to default, so I dont have to use a System_en_US.properties or something like that.

推荐答案

basename的结构类似于完全限定的类名(因为资源包可以是类),所以如果你的类路径根是 src 和资源是一个文件 src / lang / System.properties ,然后基本名称是 lang.System

The basename is structured like a fully qualified class name (because resource bundles can be classes), so if your classpath root is src and the resource is a file src/lang/System.properties, then the basename is lang.System.


关于语言环境,我如何将它保留为
默认值,所以我不必使用
System_en_US.properties或$
就是这样。

About the locale, how I leave it up to default, so I dont have to use a System_en_US.properties or something like that.

只需使用 getBundle() 仅采用基本名称的方法。

Simply use the getBundle() method that takes only the basename.

这篇关于将resourceBundle与外部文件一起使用java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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