Android 開發(fā)
讓 App 本土化的第一件事就是語言焚廊,知道如何讓 App 支持各種語言會很有幫助,而且可以減少字符串硬編碼
因為文檔解釋得很詳細了撤防,所以下面就跟著 文檔 走一輪虽风,有一些地方做出小小的標識和改動
創(chuàng)建
要怎樣做?按照這個 文檔 里說的
Create Locale Directories and String Files
創(chuàng)建語言區(qū)域目錄和字符串文件
如何做寄月?繼續(xù)照著 文檔 看
To add support for more languages, create additional
values
directories inside res/ that include a hyphen and the ISO language code at the end of the directory name. For example, values-es/ is the directory containing simple resources for the Locales with the language code "es". Android loads the appropriate resources according to the locale settings of the device at run time. For more information, see Providing Alternative Resources.
如需添加對更多語言的支持辜膝, 在
res/
創(chuàng)建另外的values
文件夾,并在文件夾 (values) 名稱末尾加上連字符 - 和 ISO 語言代碼漾肮。例如厂抖,values-es/
目錄包含的資源會用于語言代碼為“es”的語言區(qū)域 (這個是西班牙語) 。Android 根據運行時設備的語言區(qū)域設置加載相應語言的資源**初橘。如需了解詳細信息验游,請參閱提供備用資源
意思是在 res/
目錄下創(chuàng)建 名稱末尾 不同的 values
,其中目錄末尾 ISO 語言代碼 則對應著相應語言保檐,譬如:values-fr
則代表著手機系統(tǒng)語言為 法語 時讀取的目錄
一旦您決定了為哪些語言提供支持耕蝉,便可創(chuàng)建資源子目錄和字符串資源文件。例如 我要支持 中文
跟 法文
, 英文
(默認):
-
創(chuàng)建目錄, 對 res 右鍵-> New -> Android resource directory:
create dir -
在相應目錄下創(chuàng)建 strings.xml 文件夜只,右鍵目錄-> New -> Values res...ce file
創(chuàng)建 strings.xml
最后res
文件夾里的結構大致上如下:
MyProject/
res/
values/
strings.xml
values-zh/
strings.xml
values-fr/
strings.xml
將各個語言區(qū)域的字符串值添加到相應文件中垒在。
在運行時,Android 系統(tǒng)會根據當前為用戶設備設置的語言區(qū)域使用相應的字符串資源集的
例如扔亥,以下是一些不同語言下的不同 strings.xml
文件场躯。
英語(默認語言區(qū)域),/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">Hello World!</string>
</resources>
中文旅挤,/values-zh/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">我的應用</string>
<string name="hello_world">你好 世界踢关!</string>
</resources>
法語,/values-fr/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mon Application</string>
<string name="hello_world">Bonjour le monde !</string>
</resources>
注:您可以在任何資源類型上使用語言區(qū)域限定符(或任何配置限定符)粘茄,例如签舞,您可以提供本地化版本的可繪制位圖。 如需了解詳細信息柒瓣,請參閱本地化儒搭。
使用定義在 strings.xml
里的字符串
繼續(xù)照著 文檔 走
You can reference your string resources in your source code and other XML files using the resource name defined by the <string> element's name attribute.
您可以使用由
<string> 元素
的 name 屬性 定義的資源名稱在您的源代碼(java 代碼) 和 其他 XML 文件(布局文件) 中引用您的 字符串資源(定義在 strings.xml 里的字符串)。
字符串資源(string resources) 可以理解為 定義在 strings.xml
里的字符串
在您的源代碼中芙贫,可以使用語法 R.string.<string_name> 引用字符串資源(定義在 strings.xml 里的字符串), 有許多方法都接受以這種方式引用字符串搂鲫。
例如,這里取出名字為 "hello_world" 的字符串資源磺平,假設手機語言是中文的魂仍,那么取出的字符串拐辽,按照上面定義好的,就為: 你好 世界擦酌!
// Get a string resource from your app's Resources
//變量 hello 的值為 "你好 世界薛训!"
String hello = getResources().getString(R.string.hello_world);
// Or supply a string resource to a method that requires a string
// textView 會顯示 "你好 世界!"
TextView textView = new TextView(this);
textView.setText(R.string.hello_world);
在其他 XML 文件中仑氛,只要 XML 屬性接受字符串值,您就可以使用語法 @string/<string_name>
引用字符串資源闸英。
例如:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />