android 工作室中的多语言支持
multi language support in android studio
我读了this google doc。
它说我们必须使用这种格式:
<resource type>-b+<language code>[+<country code>]
例如:value-b+es/string.xml
但它在某处使用 value-es/string.xml
这是真的吗?
还有系统如何理解必须选择哪种语言?
例如我这样调用字符串:
String hello = getResources().getString(R.string.hello_world);
我必须使用条件吗? (如果是的话怎么办?)...我不能很好地理解文档。
是的。 Android OS 可以通过搜索 res 文件夹从您的应用程序中为用户选择最佳语言。
例如,您可以在 res/values-es/strings.xml.
中定义西班牙语字符串值
因此,如果用户在 phone 中将他们的主要语言设置为西班牙语,Android OS 将从您的 res/values-es/strings 中读取字符串。xml 文件夹而不是 res/values/strings.xml.
如果 res/values-es/strings.xml 中缺少某些字符串,那么它将从 res/values/strings.xml
中引用
Android 从项目的“res”目录加载文本和媒体资源。基于当前的设备配置和语言环境。
例如,如果代码加载名为 ‘R.string.title’
的字符串,Android 将通过从匹配的 ‘res/values’
目录.
AndroidAppProject/
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
在运行时,Android 系统根据当前为用户设备设置的语言环境使用一组适当的字符串资源。
现在您可以使用以下方法从 res 文件夹加载特定的语言环境字符串:
getResources().getString(R.string.hello_world);
例如:
Configuration conf = getResources().getConfiguration();
conf.locale = new Locale("fr"); //french language locale
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Resources resources = new Resources(getAssets(), metrics, conf);
/* get localized string */
String str = resources.getString(R.string.hello_world);
这将从 values-fr/
目录加载 R.string.hello_world
。
见Doc
我读了this google doc。
它说我们必须使用这种格式:
<resource type>-b+<language code>[+<country code>]
例如:value-b+es/string.xml
但它在某处使用 value-es/string.xml
这是真的吗?
还有系统如何理解必须选择哪种语言?
例如我这样调用字符串:
String hello = getResources().getString(R.string.hello_world);
我必须使用条件吗? (如果是的话怎么办?)...我不能很好地理解文档。
是的。 Android OS 可以通过搜索 res 文件夹从您的应用程序中为用户选择最佳语言。
例如,您可以在 res/values-es/strings.xml.
中定义西班牙语字符串值因此,如果用户在 phone 中将他们的主要语言设置为西班牙语,Android OS 将从您的 res/values-es/strings 中读取字符串。xml 文件夹而不是 res/values/strings.xml.
如果 res/values-es/strings.xml 中缺少某些字符串,那么它将从 res/values/strings.xml
中引用Android 从项目的“res”目录加载文本和媒体资源。基于当前的设备配置和语言环境。
例如,如果代码加载名为 ‘R.string.title’
的字符串,Android 将通过从匹配的 ‘res/values’
目录.
AndroidAppProject/
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
在运行时,Android 系统根据当前为用户设备设置的语言环境使用一组适当的字符串资源。
现在您可以使用以下方法从 res 文件夹加载特定的语言环境字符串:
getResources().getString(R.string.hello_world);
例如:
Configuration conf = getResources().getConfiguration();
conf.locale = new Locale("fr"); //french language locale
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Resources resources = new Resources(getAssets(), metrics, conf);
/* get localized string */
String str = resources.getString(R.string.hello_world);
这将从 values-fr/
目录加载 R.string.hello_world
。
见Doc