开发者

Using resourceBundle with an external file, java

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();

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="开发者_Python百科myProject.label".

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

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.


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.

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.

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


This works fine.

import java.util.Locale;
import java.util.ResourceBundle;


public class ResourceBundleTester
{
    public static void main(String[] args)
    {
        Locale locale = Locale.getDefault();
        String basename ="lang/System";
        ResourceBundle resourceBundle = ResourceBundle.getBundle(basename, locale);
        System.out.println(resourceBundle.getString("STARTING_MYAPP"));
    }
}

In the src/lang folder I created a file named System.properties that contains...

STARTING_MYAPP=Starting my app

The output is: Starting my app

This page explains how the JDK resolves the ResourceBundle names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜