多國語言 語言選擇

MoneyCitys 2016-01-21 09:08:27
想請教一下
在android studio裡要使用多國語言只要根據國家的縮寫創多個strings.xml就可以了
app會根據系統現在的語系去切換

但是有沒有辦法自己去切換?
也就是說 現在假如我系統語言是美國, 所以app切到英文
但是我想要手動直接在app裡面把語系切換成中文
有辦法嗎? 還是必須自己額外做
...全文
216 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
蒲锦_up 2016-01-22
  • 打赏
  • 举报
回复
把需要的文字保存为文件 写个工具类 去读文件就是了啊
柒加伊 2016-01-22
  • 打赏
  • 举报
回复
你去看看手机App切换主题的框架, 应该跟这个是一个类型的. 切换主题的时候, 会修改要加载的文件. 切换语言的应该也可以吧. 具体没研究. 你可以看看, 完事儿再教教我哦
MoneyCitys 2016-01-22
  • 打赏
  • 举报
回复
引用 8 楼 sagittarius1988 的回复:
.....
太感謝了, 我回去試試
sagittarius1988 2016-01-22
  • 打赏
  • 举报
回复
/*****************************************************************************
 * VLCApplication.java
 *****************************************************************************
 * Copyright © 2010-2013 VLC authors and VideoLAN
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
package org.videolan.vlc;

import java.util.Locale;

import org.videolan.vlc.gui.audio.AudioUtil;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.util.Log;

public class VLCApplication extends Application {
    public final static String TAG = "VLC/VLCApplication";
    private static VLCApplication instance;

    public final static String SLEEP_INTENT = "org.videolan.vlc.SleepIntent";

    @Override
    public void onCreate() {
        super.onCreate();

        // Are we using advanced debugging - locale?
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        String p = pref.getString("set_locale", "");
        if (p != null && !p.equals("")) {
            Locale locale;
            // workaround due to region code
            if(p.equals("zh-TW")) {
                locale = Locale.TRADITIONAL_CHINESE;
            } else if(p.startsWith("zh")) {
                locale = Locale.CHINA;
            } else if(p.equals("pt-BR")) {
                locale = new Locale("pt", "BR");
            } else if(p.equals("bn-IN") || p.startsWith("bn")) {
                locale = new Locale("bn", "IN");
            } else {
                /**
                 * Avoid a crash of
                 * java.lang.AssertionError: couldn't initialize LocaleData for locale
                 * if the user enters nonsensical region codes.
                 */
                if(p.contains("-"))
                    p = p.substring(0, p.indexOf('-'));
                locale = new Locale(p);
            }
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());
        }

        instance = this;

        // Initialize the database soon enough to avoid any race condition and crash
        MediaDatabase.getInstance(this);
        // Prepare cache folder constants
        AudioUtil.prepareCacheFolder(this);
    }

    /**
     * Called when the overall system is running low on memory
     */
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Log.w(TAG, "System is running low on memory");

        BitmapCache.getInstance().clear();
    }

    /**
     * @return the main context of the Application
     */
    public static Context getAppContext()
    {
        return instance;
    }

    /**
     * @return the main resources from the Application
     */
    public static Resources getAppResources()
    {
        if(instance == null) return null;
        return instance.getResources();
    }
}
知道该怎么做了吧
MoneyCitys 2016-01-22
  • 打赏
  • 举报
回复
引用 5 楼 woshiyyshow 的回复:
你去看看手机App切换主题的框架, 应该跟这个是一个类型的. 切换主题的时候, 会修改要加载的文件. 切换语言的应该也可以吧. 具体没研究. 你可以看看, 完事儿再教教我哦
大致上看了一下, 一開始看感覺有機會了 做法是設定存入工具類, 接著重啟app去類讀取 也許切換語言也能這樣做 但關鍵的重點是 : 不知道如何指定讀取哪一個strings.xml 這也是我詢問的點了
引用 6 楼 yueqinglkong 的回复:
把需要的文字保存为文件 写个工具类 去读文件就是了啊
這是最後不得已才做的辦法 需要的文字很多, 就是整個app的text 如果是重頭做起也沒什麼, 可惜的是項目已經完成了 要改外部讀入會更動很大 更何況內建的都有了相同的功能, 儘管我目前還沒找到控制的辦法 但是系統都能自選, 怎可能無法人為更動
MoneyCitys 2016-01-21
  • 打赏
  • 举报
回复
引用 2 楼 罗博士的回复:
你可以在app里面提供跳转到系统设置界面的功能。不过这样也是把整个手机的语言环境改了。
沒錯 但那就跟我的目的衝突了 因為我看到有不少app都有直接切換語言的功能 難道真的要全部拉出來額外做嗎 但是這樣做感覺愚蠢又麻煩
罗博士 2016-01-21
  • 打赏
  • 举报
回复
你可以在app里面提供跳转到系统设置界面的功能。不过这样也是把整个手机的语言环境改了。
MoneyCitys 2016-01-21
  • 打赏
  • 举报
回复
這個...真沒人知道嗎

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧