2017-02-10 93 views
0

我读this google docandroid studio的多语言支持

它说,我们必须使用这种格式:

<resource type>-b+<language code>[+<country code>] 

例如:value-b+es/string.xml 但地方使用它value-es/string.xml

是真的吗?

如何系统可以了解哪种语言必须选择?

比如我叫串本:

String hello = getResources().getString(R.string.hello_world); 

我一定要使用条件? (如果是的话,怎么样?)......我不能不好意思地对待文档。

回答

3

是的。 Android操作系统可以通过搜索res文件夹从应用程序中为用户选择最佳语言。

例如,你可以在资源定义西班牙字符串值/值-ES/strings.xml中。

所以,如果用户在手机上建立自己的主要语言为西班牙语,Android操作系统会从你的RES读字符串/值-ES/strings.xml中的文件夹,而不是第一的RE /价值/ strings.xml中。

如果某些字符串在res /值-ES/strings.xml中那么它将从RES /价值/ strings.xml中引用

1

的Android负载文本和媒体从项目的“资源”目录资源缺少。基于当前的设备配置和区域设置。

例如,如果代码加载一个名为‘R.string.title’的字符串,Android将在运行时通过从匹配的‘res/values’目录加载适当的strings.xml文件来为该字符串选择正确的值。

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); 

这将加载R.string.hello_worldvalues-fr/目录。

查看Doc