更改 Android Activity 中的字符串语言
Change strings language in Android Activity
我正在开发一款多语言应用。我理解 string.xml 将应用语言设置为 phone 的默认语言的概念。有什么方法可以覆盖这个吗?例如,如果我有一个西班牙语 phone 我在西班牙使用,但我仍然想使用英语的应用程序?
您可以使用一个简单的函数以编程方式更改应用程序的语言(区域设置)。您只需要实现一种类似于下面的方法,该方法接收一个字符串作为参数(例如 'en' 表示英语,'es' 表示西班牙语),您可以在其中为您的应用程序配置语言环境和刷新当前 activity 以反映更改。在您再次手动更改之前,您应用的语言环境不会更改。
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, AndroidLocalize.class);
startActivity(refresh);
finish();
}
确保导入以下包:
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.content.res.Resources;
import android.content.res.Configuration;
在您的 activity 标签内的清单中也添加 android:configChanges="locale|orientation"
EDIT 正如正确提到的那样,这不是 OP 所要求的,我仍然会把它留在这里,以防万一有人在这里试图解决我回答的问题。
来自 Android 开发者页面:
How to Create Alternative Resources
A large part of localizing an application is providing alternative
text for different languages. In some cases you will also provide
alternative graphics, sounds, layouts, and other locale-specific
resources.
An application can specify many res// directories, each
with different qualifiers. To create an alternative resource for a
different locale, you use a qualifier that specifies a language or a
language-region combination. (The name of a resource directory must
conform to the naming scheme described in Providing Alternative
Resources, or else it will not compile.)
Example:
Suppose that your application's default language is English. Suppose
also that you want to localize all the text in your application to
French, and most of the text in your application (everything except
the application's title) to Japanese. In this case, you could create
three alternative strings.xml files, each stored in a locale-specific
resource directory:
res/values/strings.xml
Contains English text for all the strings that the application uses, including text for a string named title.
res/values-fr/strings.xml
Contain French text for all the strings, including title.
res/values-ja/strings.xml
Contain Japanese text for all the strings except title.
If your Java code refers to R.string.title, here is what will happen
at runtime:
If the device is set to any language other than French, Android will load title from the res/values/strings.xml file.
If the device is set to French, Android will load title from the res/values-fr/strings.xml file.
Notice that if the device is set to Japanese, Android will look for
title in the res/values-ja/strings.xml file. But because no such
string is included in that file, Android will fall back to the
default, and will load title in English from the
res/values/strings.xml file.
这里还有原文link供参考:http://developer.android.com/guide/topics/resources/localization.html
我正在开发一款多语言应用。我理解 string.xml 将应用语言设置为 phone 的默认语言的概念。有什么方法可以覆盖这个吗?例如,如果我有一个西班牙语 phone 我在西班牙使用,但我仍然想使用英语的应用程序?
您可以使用一个简单的函数以编程方式更改应用程序的语言(区域设置)。您只需要实现一种类似于下面的方法,该方法接收一个字符串作为参数(例如 'en' 表示英语,'es' 表示西班牙语),您可以在其中为您的应用程序配置语言环境和刷新当前 activity 以反映更改。在您再次手动更改之前,您应用的语言环境不会更改。
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, AndroidLocalize.class);
startActivity(refresh);
finish();
}
确保导入以下包:
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.content.res.Resources;
import android.content.res.Configuration;
在您的 activity 标签内的清单中也添加 android:configChanges="locale|orientation"
EDIT 正如正确提到的那样,这不是 OP 所要求的,我仍然会把它留在这里,以防万一有人在这里试图解决我回答的问题。
来自 Android 开发者页面:
How to Create Alternative Resources
A large part of localizing an application is providing alternative text for different languages. In some cases you will also provide alternative graphics, sounds, layouts, and other locale-specific resources.
An application can specify many res// directories, each with different qualifiers. To create an alternative resource for a different locale, you use a qualifier that specifies a language or a language-region combination. (The name of a resource directory must conform to the naming scheme described in Providing Alternative Resources, or else it will not compile.)
Example:
Suppose that your application's default language is English. Suppose also that you want to localize all the text in your application to French, and most of the text in your application (everything except the application's title) to Japanese. In this case, you could create three alternative strings.xml files, each stored in a locale-specific resource directory:
res/values/strings.xml Contains English text for all the strings that the application uses, including text for a string named title. res/values-fr/strings.xml Contain French text for all the strings, including title. res/values-ja/strings.xml Contain Japanese text for all the strings except title.
If your Java code refers to R.string.title, here is what will happen at runtime:
If the device is set to any language other than French, Android will load title from the res/values/strings.xml file. If the device is set to French, Android will load title from the res/values-fr/strings.xml file.
Notice that if the device is set to Japanese, Android will look for title in the res/values-ja/strings.xml file. But because no such string is included in that file, Android will fall back to the default, and will load title in English from the res/values/strings.xml file.
这里还有原文link供参考:http://developer.android.com/guide/topics/resources/localization.html