2017-02-10 132 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