Java國際化,使用ResourceBundle的方式讀取設定檔__Java

來源:互聯網
上載者:User

如Struts,spring等架構以及Tomcat容器,都是通過ResourceBundle的方式從資源檔(如messages.properties)中自動讀取並進行資源綁定的。java.util.ResourceBundle類非常地靈活,可以通過設定的Locale(語言環境)來選擇讀取的檔案,進行國際化。

最近在看Tomcat的源碼,看到了Tomcat國際化的實現方式,下面寫一個例子介紹如何使用:


資源設定檔:

LocalStrings_fr.java:

package terry.codex;import java.util.Collections;import java.util.Enumeration;import java.util.ResourceBundle;/** * @編寫人: yh.zeng * @編寫時間:2017-12-3 下午4:58:26 * @檔案描述: LocalStrings法語設定檔 */public class LocalStrings_fr extends ResourceBundle{@Overrideprotected Object handleGetObject(String key) {if(key.equals("tmpdir")){return "{0} itinéraires désignés avec moins";}return null;}@Overridepublic Enumeration<String> getKeys() { return Collections.enumeration(keySet());}} 
LocalStrings_en.properties:

tmpdir=Cannot find specified temporary folder at {0}
LocalStrings_zh.properties:

tmpdir=\u5728{0}\u8def\u5f84\u4e0b\u627e\u4e0d\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u5939
LocalStrings.properties:

tmpdir=\u5728{0}\u8def\u5f84\u4e0b\u627e\u4e0d\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u5939

測試例子demo


ResourceBundleTest:

package terry.codex;import java.io.File;import java.net.MalformedURLException;import java.net.URL;import java.net.URLClassLoader;import java.text.MessageFormat;import java.util.Locale;import java.util.MissingResourceException;import java.util.ResourceBundle;/** * @編寫人: yh.zeng * @編寫時間:2017-12-3 上午12:35:42 * @檔案描述: 通過ResourceBundle的方式從資源檔(如messages.properties)中自動讀取並進行資源綁定的demo *           可以用於實現國際化 */public class ResourceBundleTest {public static void main(String args[]){        String bundleName = "terry.codex.LocalStrings";                /**         * 1、在當前類載入器下,找JAVA 虛擬機器執行個體的當前預設語言環境值對應的設定檔,會找以下檔案內容         *        terry/codex/LocalStrings_zh.class         *        terry/codex/LocalStrings_zh.properties         *        terry/codex/LocalStrings.class         *        terry/codex/LocalStrings.properties         *    檔案內容自上而下覆蓋         *    LocalStrings_zh.class檔案會覆蓋掉LocalStrings_zh.properties檔案的內容         *    LocalStrings_zh.properties檔案會覆蓋掉LocalStrings.class檔案的內容         *    LocalStrings.class檔案會覆蓋掉LocalStrings.properties檔案的內容         *    注意:LocalStrings_zh.class檔案必須是ResourceBundle的子類,實現handleGetObject和getKeys方法。         */        try {        System.out.println("Example1:");        ResourceBundle bundle  = ResourceBundle.getBundle(bundleName);            if(bundle != null){            String value = bundle.getString("tmpdir");            String params[] = new String[]{"D:\\TMP"};                MessageFormat mf = new MessageFormat(value);                System.out.println(mf.format(params, new StringBuffer(), null).toString());            }} catch (MissingResourceException ex) {ex.printStackTrace();}                        /**         * 2、Locale.ENGLISH在當前類載入器下找以下檔案的內容:         *       terry/codex/LocalStrings_en.class         *       terry/codex/LocalStrings_en.properties         *       terry/codex/LocalStrings.class         *       terry/codex/LocalStrings.properties         *    檔案內容自上而下覆蓋         *    LocalStrings_en.class檔案會覆蓋掉LocalStrings_en.properties檔案的內容         *    LocalStrings_en.properties檔案會覆蓋掉LocalStrings.class檔案的內容         *    LocalStrings.class檔案會覆蓋掉LocalStrings.properties檔案的內容         *    注意:LocalStrings_en.class檔案必須是ResourceBundle的子類,實現handleGetObject和getKeys方法。         */        try {        System.out.println("Example2:");        ResourceBundle bundle  = ResourceBundle.getBundle(bundleName, Locale.ENGLISH);            if(bundle != null){            String value = bundle.getString("tmpdir");            String params[] = new String[]{"D:\\TMP"};                MessageFormat mf = new MessageFormat(value);                mf.setLocale(Locale.ENGLISH);                System.out.println(mf.format(params, new StringBuffer(), null).toString());            }} catch (MissingResourceException ex) {ex.printStackTrace();}                        /**         * 3、在當前類載入器下找以下檔案:         *       terry/codex/LocalStrings_zh.class         *       terry/codex/LocalStrings_zh.properties         *       terry/codex/LocalStrings.class         *       terry/codex/LocalStrings.properties         *    檔案內容自上而下覆蓋         *    LocalStrings_zh.class檔案會覆蓋掉LocalStrings_zh.properties檔案的內容         *    LocalStrings_zh.properties檔案會覆蓋掉LocalStrings.class檔案的內容         *    LocalStrings.class檔案會覆蓋掉LocalStrings.properties檔案的內容         *    注意:LocalStrings_zh.class檔案必須是ResourceBundle的子類,實現handleGetObject和getKeys方法。         */        try {        System.out.println("Example3:");        ResourceBundle bundle  = ResourceBundle.getBundle(bundleName, Locale.CHINA);            if(bundle != null){            String value = bundle.getString("tmpdir");            String params[] = new String[]{"D:\\TMP"};                MessageFormat mf = new MessageFormat(value);                mf.setLocale(Locale.CHINA);                System.out.println(mf.format(params, new StringBuffer(), null).toString());            }} catch (MissingResourceException ex) {ex.printStackTrace();}           /**         * 4、在當前類載入器下找以下檔案:         *       terry/codex/LocalStrings_fr.class         *       terry/codex/LocalStrings_fr.properties         *       terry/codex/LocalStrings.class         *       terry/codex/LocalStrings.properties         *    檔案內容自上而下覆蓋         *    LocalStrings_fr.class檔案會覆蓋掉LocalStrings_fr.properties檔案的內容         *    LocalStrings_fr.properties檔案會覆蓋掉LocalStrings.class檔案的內容         *    LocalStrings.class檔案會覆蓋掉LocalStrings.properties檔案的內容           *    注意:LocalStrings_fr.class檔案必須是ResourceBundle的子類,實現handleGetObject和getKeys方法。         */        try {        System.out.println("Example4:");        ResourceBundle bundle  = ResourceBundle.getBundle(bundleName, Locale.FRANCE);            if(bundle != null){            String value = bundle.getString("tmpdir");            String params[] = new String[]{"D:\\TMP"};                MessageFormat mf = new MessageFormat(value);                mf.setLocale(Locale.FRANCE);                System.out.println(mf.format(params, new StringBuffer(), null).toString());            }} catch (MissingResourceException ex) {ex.printStackTrace();}                /**         * 5、在D:\\loader這個類載入路徑下找以下檔案:         *       terry/codex/LocalStrings_zh.class         *       terry/codex/LocalStrings_zh.properties         *       terry/codex/LocalStrings.class         *       terry/codex/LocalStrings.properties         *    檔案內容自上而下覆蓋         *    LocalStrings_zh.class檔案會覆蓋掉LocalStrings_zh.properties檔案的內容         *    LocalStrings_zh.properties檔案會覆蓋掉LocalStrings.class檔案的內容         *    LocalStrings.class檔案會覆蓋掉LocalStrings.properties檔案的內容         *    注意:LocalStrings_zh.class檔案必須是ResourceBundle的子類,實現handleGetObject和getKeys方法。         */try {bundleName = "terry.codex2.LocalStrings";System.out.println("Example5:");        String fileUrlString = new File("D:\\loader").toURI().toString();        fileUrlString = fileUrlString.replaceAll("!/", "%21/");ClassLoader classLoader = new URLClassLoader(new URL[]{new URL(fileUrlString)});        if (classLoader != null) {            try {            ResourceBundle bundle = ResourceBundle.getBundle(bundleName, Locale.CHINA, classLoader);                if(bundle != null){                String value = bundle.getString("tmpdir");                String params[] = new String[]{"D:\\TMP"};                    MessageFormat mf = new MessageFormat(value);                    mf.setLocale(Locale.CHINA);                    System.out.println(mf.format(params, new StringBuffer(), null).toString());                }            } catch (MissingResourceException ex2) {            ex2.printStackTrace();            }        }} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

程式運行結果:

Example1:在D:\TMP路徑下找不到指定的檔案夾Example2:Cannot find specified temporary folder at D:\TMPExample3:在D:\TMP路徑下找不到指定的檔案夾Example4:D:\TMP itinéraires désignés avec moinsExample5:在D:\TMP路徑下找不到指定的檔案夾

demo:見 https://github.com/zengyh/CodeLibary/tree/master/src/resource/bundle/ResourceBundleTest.java


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.