自定义多语言支持

ami*_*hgc 14 android

我知道为语言创建一个具有语言代码后缀的新值目录.对于german:values-de或french:values-fr然后将我们的string.xml复制到其中并翻译每个条目.这基于电话本地化设置

我想知道我们是否可以绕过手机设置并让用户在应用程序中选择所需的语言?

我的要求是,我想在我的应用程序中提供一个语言选择选项,并让用户选择他想要的应用程序语言..如何在string.xml之间动态切换(针对不同的语言)???

提前致谢

Sha*_*ter 11

创建方法,设置您的基本Locale.Lets说

public static void setDefaultLocale(Context context,String locale) {
        Locale locJa = new Locale(locale);
        Locale.setDefault(locJa);

        Configuration config = new Configuration();
        config.locale = locJa;

        context.getResources().updateConfiguration(config, context.getResources()
                .getDisplayMetrics());

        locJa = null;
        config = null;
    }
Run Code Online (Sandbox Code Playgroud)

现在检查用户选择Locale的时间.(这里基本上我使用菜单进行语言选择).

Configuration config = new Configuration();
String newLocale = config.locale.getLanguage().substring(0, 2)
    .toLowerCase();
if ("ja".equalsIgnoreCase(newLocale)) {
// Call above method with context & newLocale
} 
// Sequentially you check for Locale & change that.
Run Code Online (Sandbox Code Playgroud)


trg*_*lia 5

看看这篇文章......基本上是一回事.

在应用程序内更改区域设置

Locale appLoc = new Locale("en");
Locale.setDefault(appLoc);
Configuration appConfig = new Configuration();
appConfig.locale = appLoc;
getBaseContext().getResources().updateConfiguration(appConfig,
    getBaseContext().getResources().getDisplayMetrics());
Run Code Online (Sandbox Code Playgroud)