更改语言编程在Android中使用的国家 [英] Change language programatically in Android with country

查看:250
本文介绍了更改语言编程在Android中使用的国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变我的语言环境中的Andr​​oid应用程序。
如果我只用语言一切正常,但今天我加了葡萄牙语的翻译BR我的应用程序。

I try to change my locale in an android application. If I use only language all is ok, but today I added portuguese-br translation to my app.

code:

Locale locale;
        if(language.contains("-"))  // In this case, the locale specify also the country
        {
            String[] country_locale = language.split("-");
            locale = new Locale(country_locale[0], country_locale[1]); 
        }
        else
            locale = new Locale(language); 
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getApplicationContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

一切正常,直到最后一行。
我知道,因为在previous一块我计划我能得到一些字符串与此code正确PT-BR的语言环境:

All is ok untill last line. I know because in a previous piece of my program i could get some string with the correct pt-br locale with this code:

Resources resources = new Resources(ctx.getAssets(), ctx.getResources().getDisplayMetrics(), new_config);

updateConfiguration设置我的语言环境为英语语言环境,如果与一个国家code定义。

updateConfiguration set my locale to English if Locale was defined with a country code.

String.xml是价值-PT-RBR

String.xml is in values-pt-rBR

在调试区域设置值被设置为PT-BR

While debugging Locale value is set to pt-BR.

编辑:经过进一步的测试,这个工作我的Andr​​oid手机上,但在我的平板电脑(均与Android 4.4.2 Sambung)不起作用。
可能是什么原因呢?

After further tests, this works on my Android phone, but doesn't work on my tablet (both Sambung with android 4.4.2). What could be the reason?

编辑2:也超过了模拟器的工作,如果我使用的是手机,不是如果我使用平板电脑工作

EDIT 2: Also over the emulator work if I use a phone, not work if I use a tablet.

推荐答案

在当前的活动,尝试拨打按照你的code后:

On your current Activity, try call the follow after your code:

finish();
startActivity(getIntent());

这将重新创建活动,然后,重新加载字符串资源。

This will recreate your Activity and, then, reload the string resources.

这篇关于更改语言编程在Android中使用的国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆