如何重新加载 JSF ResourceBundle?

发布于 2024-12-21 07:39:21 字数 5288 浏览 2 评论 0原文

我目前正在设计一个需要以多种不同语言提供的应用程序,经过大量研究后,我恢复使用 resouceBundles。我正在使用 java se 1.6 和 java ee 6 以及 jsf 1.2 和 glassfish 3.1..

我从互联网上得到了以下代码,并以一种有意义的方式排列它..现在的问题是,ResourceBundle 加载正常,但是它没有刷新..我尝试了很多不同的 ResourceBundle.clearCache() 方法,但这些方法似乎都不起作用...

请注意,在 Control 类中,我正在检查区域设置并加载数据以适应 那。这稍后将被数据库支持的系统取代。这只是测试代码,以确保我的底层结构正常工作...

CODE_SAMPLE:LocalTest.java - 资源包

public class LocalTester extends ResourceBundle {

    protected static final String BUNDLE_NAME = "local.lantest.LocalTester";
    protected static final Control UTF8_CONTROL = new LocalTesterControl();

    public LocalTester() {
        setParent(ResourceBundle.getBundle(BUNDLE_NAME, FacesContext.getCurrentInstance().getViewRoot().getLocale(), UTF8_CONTROL));
    }

    @Override
    protected Object handleGetObject(String key) {
        return parent.getObject(key);
    }

    @Override
    public Enumeration<String> getKeys() {
        return parent.getKeys();
    }

    public static Control getUTF8_CONTROL() {
        return UTF8_CONTROL;
    }
}

CODE_SAMPLE:LocalTestControl.java - 资源包控制

public class LocalTesterControl extends Control {

    @Override
    public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime) {
        return true;
    }

    @Override
    public long getTimeToLive(String baseName, Locale locale) {
        return TTL_DONT_CACHE;
    }

    @Override
    public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
            throws IllegalAccessException, InstantiationException, IOException {

        System.out.println("---------------------------LOADING LOCALE PACK-------------------------------------");
        System.out.println("Printing baseName = " + baseName);
        System.out.println("Printing locale.getLanguage() = " + locale.getLanguage());
        System.out.println("Printing locale.getDisplayLanguage() = " + locale.getDisplayLanguage());

        ResourceBundle rbdl;

        if (locale.getLanguage().compareTo("fr") == 0) {
            System.out.println("------- LOAD FRENCH language pack load");
            rbdl = new ListResourceBundle() {

                @Override
                protected Object[][] getContents() {
                    Object[][] aa = {
                        {"welcome", "FR- Welcome" + (new Date().toString())},
                        {"message", "FR- message"}, 
                        {"signoff", "FR- Sign Off"}, 
                        {"parametWelcome", "FR-Para Meter Welcome... {0} is parameter..."},
                        {"numberParaFormat", "FR-NUMBER parameter = {0}...."}
                    };
                    return aa;
                }
            };
        } else if (locale.getLanguage().compareTo("es") == 0) {
            System.out.println("------- LOAD ESPANIOLA language pack load");
            rbdl = new ListResourceBundle() {

                @Override
                protected Object[][] getContents() {
                    Object[][] aa = {
                        {"welcome", "ES- Welcome" + (new Date().toString())},
                        {"message", "ES- message"},
                        {"signoff", "ES- Sign Off"},
                        {"parametWelcome", "ES-Para Meter Welcome... {0} is parameter..."},
                        {"numberParaFormat", "ES-NUMBER parameter = {0}...."}
                    };
                    return aa;
                }
            };
        } else {
            System.out.println("------- LOAD DEFAULT language pack load");
            rbdl = new ListResourceBundle() {

                @Override
                protected Object[][] getContents() {
                    Object[][] aa = {
                        {"welcome", "EN- Welcome" + (new Date().toString())},
                        {"message", "EN- message"}, 
                        {"signoff", "EN- Sign Off"}, 
                        {"parametWelcome", "EN-Para Meter Welcome... {0} is parameter..."},
                        {"numberParaFormat", "EN-NUMBER parameter = {0}...."},
                        {"notrans", "EN- No Language Translation"}
                    };
                    return aa;
                }
            };
        }

        return rbdl;
    }
}

最后是 Faces 配置:

<faces-config version="2.0"
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
    <application>
        <resource-bundle>
            <base-name>local.lantest.LocalTester</base-name>
            <var>bundi</var>
        </resource-bundle>
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>en</supported-locale>
            <supported-locale>es</supported-locale>
            <supported-locale>fr</supported-locale>
        </locale-config>
    </application>

</faces-config>

任何帮助和建议都非常有价值。

I am currently designing an application that needs to be available in so many different languages and after much research I reverted in to using resouceBundles. I am using java se 1.6 and java ee 6 with jsf 1.2 and glassfish 3.1..

I got following codes from all over the internet and arranged it in a manner to make sense.. Now the question is, the ResourceBundle loads alright, however it doesnt get refreshed.. i tried so many different ResourceBundle.clearCache() methods but none of those seems to be working...

Please note in the Control class I am checking the locale and loading the data to fit that. This will be later replaced by a database backed system. This is only test code to make sure i have the underlying structure working...

CODE_SAMPLE: LocalTest.java - The Resource Bundle

public class LocalTester extends ResourceBundle {

    protected static final String BUNDLE_NAME = "local.lantest.LocalTester";
    protected static final Control UTF8_CONTROL = new LocalTesterControl();

    public LocalTester() {
        setParent(ResourceBundle.getBundle(BUNDLE_NAME, FacesContext.getCurrentInstance().getViewRoot().getLocale(), UTF8_CONTROL));
    }

    @Override
    protected Object handleGetObject(String key) {
        return parent.getObject(key);
    }

    @Override
    public Enumeration<String> getKeys() {
        return parent.getKeys();
    }

    public static Control getUTF8_CONTROL() {
        return UTF8_CONTROL;
    }
}

CODE_SAMPLE: LocalTestControl.java - The Resource Bundle Control

public class LocalTesterControl extends Control {

    @Override
    public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime) {
        return true;
    }

    @Override
    public long getTimeToLive(String baseName, Locale locale) {
        return TTL_DONT_CACHE;
    }

    @Override
    public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
            throws IllegalAccessException, InstantiationException, IOException {

        System.out.println("---------------------------LOADING LOCALE PACK-------------------------------------");
        System.out.println("Printing baseName = " + baseName);
        System.out.println("Printing locale.getLanguage() = " + locale.getLanguage());
        System.out.println("Printing locale.getDisplayLanguage() = " + locale.getDisplayLanguage());

        ResourceBundle rbdl;

        if (locale.getLanguage().compareTo("fr") == 0) {
            System.out.println("------- LOAD FRENCH language pack load");
            rbdl = new ListResourceBundle() {

                @Override
                protected Object[][] getContents() {
                    Object[][] aa = {
                        {"welcome", "FR- Welcome" + (new Date().toString())},
                        {"message", "FR- message"}, 
                        {"signoff", "FR- Sign Off"}, 
                        {"parametWelcome", "FR-Para Meter Welcome... {0} is parameter..."},
                        {"numberParaFormat", "FR-NUMBER parameter = {0}...."}
                    };
                    return aa;
                }
            };
        } else if (locale.getLanguage().compareTo("es") == 0) {
            System.out.println("------- LOAD ESPANIOLA language pack load");
            rbdl = new ListResourceBundle() {

                @Override
                protected Object[][] getContents() {
                    Object[][] aa = {
                        {"welcome", "ES- Welcome" + (new Date().toString())},
                        {"message", "ES- message"},
                        {"signoff", "ES- Sign Off"},
                        {"parametWelcome", "ES-Para Meter Welcome... {0} is parameter..."},
                        {"numberParaFormat", "ES-NUMBER parameter = {0}...."}
                    };
                    return aa;
                }
            };
        } else {
            System.out.println("------- LOAD DEFAULT language pack load");
            rbdl = new ListResourceBundle() {

                @Override
                protected Object[][] getContents() {
                    Object[][] aa = {
                        {"welcome", "EN- Welcome" + (new Date().toString())},
                        {"message", "EN- message"}, 
                        {"signoff", "EN- Sign Off"}, 
                        {"parametWelcome", "EN-Para Meter Welcome... {0} is parameter..."},
                        {"numberParaFormat", "EN-NUMBER parameter = {0}...."},
                        {"notrans", "EN- No Language Translation"}
                    };
                    return aa;
                }
            };
        }

        return rbdl;
    }
}

And finally the Faces config:

<faces-config version="2.0"
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
    <application>
        <resource-bundle>
            <base-name>local.lantest.LocalTester</base-name>
            <var>bundi</var>
        </resource-bundle>
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>en</supported-locale>
            <supported-locale>es</supported-locale>
            <supported-locale>fr</supported-locale>
        </locale-config>
    </application>

</faces-config>

Any help and advise is much valued.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

五里雾 2024-12-28 07:39:21

您可以执行以下操作:

  1. 首先,使用区域设置代码创建多个 ApplicationResources:
    • ApplicationResources.properties(默认文件)
    • ApplicationResources_es.properties(西班牙语文件,即)
  2. 修改 faces-config.xml 以支持不同的区域设置:
  <application>
  <locale-config>
   <default-locale>en</default-locale>
   <supported-locale>en</supported-locale>
   <supported-locale>es</supported-locale>
  </locale-config>
  <message-bundle>com.business.resources.ApplicationResources</message-bundle>
 </application>

编辑您的 jsf 并添加:

<f:loadBundle basename="com.business.resources.ApplicationResources" var="msg"/>

您可以在以下网站中看到区域设置列表: http://www.science.co.il/language/locale-codes.asp

you can do something like this:

  1. First, create several ApplicationResources using locale codes:
    • ApplicationResources.properties (default file)
    • ApplicationResources_es.properties (spanish file, i.e.)
  2. Modify faces-config.xml to support different locales:
  <application>
  <locale-config>
   <default-locale>en</default-locale>
   <supported-locale>en</supported-locale>
   <supported-locale>es</supported-locale>
  </locale-config>
  <message-bundle>com.business.resources.ApplicationResources</message-bundle>
 </application>

Edit your jsf and add:

<f:loadBundle basename="com.business.resources.ApplicationResources" var="msg"/>

You can see a locale list in the following website: http://www.science.co.il/language/locale-codes.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文