必威体育Betway必威体育官网
当前位置:首页 > IT技术

国际化之使用ResourceBundle.getBundle详解

时间:2019-10-02 10:12:19来源:IT技术作者:seo实验室小编阅读:78次「手机版」
 

resourcebundle

首先新建一个resources文件夹

Resources文件夹

新建有个Resource Bundle ‘digitalDialog’

Resource Bundle 'DigitalDialog'

Resources 效果如下

Resources  效果如下

中文

tips.state.initializing=\u9ea6\u514b\u98ce\u542f\u52a8\u4e2d
tips.state.wait=\u8bf7\u7b49\u5f85
tips.state.ready=\u8bf7\u8bf4\u8bdd
tips.state.listening=\u503e\u542c\u4e2d
tips.state.recognizing=\u8bc6\u522b\u4e2d
tips.copyright=\u004d\u0055\u0043\u0048\u63d0\u4f9b\u6280\u672f\u652f\u6301

btn.done=\u8bf4\u5b8c\u4e86
btn.cancel=\u53d6\u6d88
btn.retry=\u91cd\u8bd5
btn.start=\u5f00\u59cb\u8bf4\u8bdd

英文

tips.state.initializing=Mic is initializing
tips.state.wait=Wait a moment
tips.state.ready=Speak now
tips.state.listening=Listening
tips.state.recognizing=Recognizing
tips.copyright=Copyright by MUCH Voice

btn.done=Finish
btn.cancel=Cancel
btn.retry=Retry
btn.start=Start
   /**
     * 国际化文本资源
     */

    private ResourceBundle mLableRes;

    private static final String KEY_TIPS_STATE_READY = "tips.state.ready";

    private static final String KEY_TIPS_STATE_WAIT = "tips.state.wait";

    private static final String KEY_TIPS_STATE_INITIALIZING = "tips.state.initializing";

    private static final String KEY_TIPS_STATE_LISTENING = "tips.state.listening";

    private static final String KEY_TIPS_STATE_RECOGNIZING = "tips.state.recognizing";

    private static final String KEY_TIPS_COPYRIGHT = "tips.copyright";

    private static final String KEY_TIPS_WAITNET = "tips.wait.net";

    private static final String KEY_BTN_DONE = "btn.done";

    private static final String KEY_BTN_CANCEL = "btn.cancel";

    private static final String KEY_BTN_RETRY = "btn.retry";

/**
     * 加载国际化字符串,{{@link #initView()}之后调用
     */
    private void loadI18N() {
        try {
            mLableRes = ResourceBundle.getBundle("DigitalDialog");
            mLogoText1.setText(getString(KEY_TIPS_COPYRIGHT));
            mLogoText2.setText(getString(KEY_TIPS_COPYRIGHT));
            mRetryTextView.setText(getString(KEY_BTN_RETRY));
            mtitle.setText(getString(KEY_TIPS_HELP_TITLE));
            mPrefix = getString(KEY_TIPS_PREFIX);
        } catch (MissingResourceException e) {
            Log.w(TAG, "loadI18N ERROR", e);
        }
    }

    /**
     * 获取国际化字符串
     *
     * @param key
     * @return 资源不存在返回Null
     */
    private String getString(String key) {
        String label = null;
        if (mLableRes != null) {
            try {
                label = mLableRes.getString(key);
            } catch (Exception e) {
                Log.w(TAG, "get internationalization error key:" + key, e);
            }
        }
        return label;
    }

相关阅读

ResourceBundle与Properties读取配置文件

ResourceBundle与Properties的区别在于ResourceBundle通常是用于国际化的属性配置文件读取,Properties则是一般的属性配置文件读取

分享到:

栏目导航

推荐阅读

热门阅读