从外部XML的Andr​​oid本地化 [英] Android localization from external XML

查看:156
本文介绍了从外部XML的Andr​​oid本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能把在从服务接收到使用XML运行Android应用程序?
如果可能的话可能有人请点我在正确的方向。

Is it possible to translate android application at runtime using XML received from service? If it is possible could someone please point me in right direction.

感谢您。

推荐答案

警告

我已阅读的一切说,它不是一个好主意,让您的应用程序更改语言,因为它不是由Android框架支持,它可能会导致问题。

Everything I have read says that its not a good idea to let your app change the language because it isn't supported by the Android framework and it may cause problems.

有了这样说,我在我的应用程序做它,虽然它一直有点痛,似乎到目前为止是工作。这里是我是如何做到的情况下,你想要做这种方式。你需要一个单独的的strings.xml 文件为每种语言。 的strings.xml 文件夹作为默认那么也许一个的strings.xml 中说一个值-ES 文件夹西班牙字符串。我用下面的code改变取决于配置设置的单选用户选择

With that being said, I have done it in my app and, while it has been a bit of a pain, it seems to be working so far. Here is how I did it in case you want to do it this way. You need a separate strings.xml file for each language. strings.xml in values folder as your default then maybe a strings.xml in say a values-es folder for Spanish strings. I have used the following code to change the configuration settings depending on a RadioButton that the user selects

final Configuration LANG_CONFIG = ChooseLocale.this.getResources().getConfiguration();
Locale newLocale = new Locale("English");
curLang = ChooseLocale.this.getLanguage();
if ((curLang.equals("English")) || (curLang.equalsIgnoreCase("Ingles")))
{
    newLocale = new Locale("en_US");
}
else
{
    newLocale = new Locale("es");
}
Toast.makeText(getApplicationContext(), newLangToast + " " + curLang , Toast.LENGTH_SHORT).show();
Configuration config = getBaseContext().getResources().getConfiguration();
Locale.setDefault(newLocale);
config.locale = newLocale;
getBaseContext().getResources().updateConfiguration(config, 
        getBaseContext().getResources().getDisplayMetrics());

SharedPreferences.Editor editor = langPref.edit();
editor.putString(LANG_PREF, curLang);
editor.commit();

使用这条线是最重要的,以更新的配置

With this line being the most important to update the config

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

我得到它可以,但是你想处理我的使用getLanguage()函数的语言环境。我还必须添加

I get the locale from my getLanguage() function which can be handled however you want. I also had to add

@Override
public void onConfigurationChanged(Configuration newConfig) {
    newConfig = Globals.getUserLanguage(this);
    super.onConfigurationChanged(newConfig);

每一个活动,使方向变化,这添加到我的的onCreate()每个

final SharedPreferences langPref = getSharedPreferences (LANG_PREF, 0);
if (Globals.langConfig != null)
    this.onConfigurationChanged(Globals.langConfig);

我还添加了的android:configChanges =方向|区域设置来在清单中,让每一个活动的方向变化。

I also added android:configChanges="orientation|locale" to each activity in the manifest that allows orientation change

Android电子文档本地化

这篇关于从外部XML的Andr​​oid本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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