本文节选自《Netkiller Java 手札》
Netkiller Java 手札
Mr. Neo Chan, 陈景峯(BG7NYT)
中国广东省深圳市望海路半岛城邦三期 518067 +86 13113668890 <netkiller@msn.com>
$Id: book.xml 606 2013-05-29 09:52:58Z netkiller $
版权 © 2015-2018 Neo Chan
版权声明
转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。
http://netkiller.github.io |
http://netkiller.sourceforge.net |
我的系列文档
编程语言
Netkiller Architect 手札 | Netkiller Developer 手札 | Netkiller Java 手札 | Netkiller Spring 手札 | Netkiller PHP 手札 | Netkiller Python 手札 |
---|---|---|---|---|---|
Netkiller Testing 手札 | Netkiller Cryptography 手札 | Netkiller Perl 手札 | Netkiller Docbook 手札 | Netkiller Project 手札 | Netkiller Database 手札 |
第 37 章 Internationalization i18n with Android (国际化)
目录
37.1. 创建国际化文件
进入 Android Studio 文件菜单 File -> New -> New Resource File
![]() |
在左侧列表中找到 Locale 点击 “>>” 按钮
![]() |
选择国家后,点击 OK 按钮即可。
![]() |
资源文件夹中已经显示出国际化文件,上面并有对应的国旗。
查看项目文件夹
neo@MacBook-Pro ~/AndroidStudioProjects/locale % find app/src/main/res | grep values app/src/main/res/values-zh-rCN app/src/main/res/values-zh-rCN/strings.xml app/src/main/res/values app/src/main/res/values/colors.xml app/src/main/res/values/dimens.xml app/src/main/res/values/styles.xml app/src/main/res/values/strings.xml
37.2. strings.xml 文件
<resources> <string name="app_name">Netkiller</string> <string name="title_home">Home</string> <string name="title_dashboard">Dashboard</string> <string name="title_notifications">Notifications</string> </resources>
37.3. 翻译语言
再 res/values/strings 目录上面单击鼠标右键,打开 Open Translations Editor 翻译编辑器。
![]() |
单击地球图标,添加 zh-cn 语言
![]() |
现在就可以对照翻译语言包文件了。
37.4. 引用国际化文件
String test = "Sign Up"; String test = getResources().getString(R.string.sign_up);
R.string.browserSentence = "You are using $1%s to browse the Internet."; String browser = getString(R.string.browserSentence, browser.getBrowser());
TextView textView = new TextView(this); TextView.setText(“Sign Up”); TextView textView = new TextView(this); textView.setText(R.string.sign_up);
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />