国际化之使用ResourceBundle.getBundle详解

首先新建一个Resources文件夹
国际化之使用ResourceBundle.getBundle详解_第1张图片

新建有个Resource Bundle ‘DigitalDialog’
国际化之使用ResourceBundle.getBundle详解_第2张图片

Resources 效果如下
国际化之使用ResourceBundle.getBundle详解_第3张图片

中文

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;
    }

你可能感兴趣的:(resources)