Supporting Different Languages

PREVIOUS NEXT

THIS CLASS TEACHES YOU TO

  1. Create Locale Directories and String Files
  2. Use the String Resources

YOU SHOULD ALSO READ

  • Localization

It’s always a good practice to extract UI strings from your app code and keep them in an external file. Android makes this easy with a resources directory in each Android project.

If you created your project using the Android SDK Tools (readCreating an Android Project), the tools create ares/directory in the top level of the project. Within thisres/directory are subdirectories for various resource types. There are also a few default files such asres/values/strings.xml, which holds your string values.

Create Locale Directories and String Files


To add support for more languages, create additionalvaluesdirectories insideres/that include a hyphen and the ISO country code at the end of the directory name. For example,values-es/is the directory containing simple resourcess for the Locales with the language code "es". Android loads the appropriate resources according to the locale settings of the device at run time.

Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:

MyProject/    res/       values/           strings.xml       values-es/           strings.xml       values-fr/           strings.xml

Add the string values for each locale into the appropriate file.

At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.

For example, the following are some different string resource files for different languages.

English (default locale),/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?><resources><stringname="title">My Application</string><stringname="hello_world">Hello World!</string></resources>

Spanish,/values-es/strings.xml:

<?xml version="1.0" encoding="utf-8"?><resources><stringname="title">Mi Aplicación</string><stringname="hello_world">Hola Mundo!</string></resources>

French,/values-fr/strings.xml:

<?xml version="1.0" encoding="utf-8"?><resources><stringname="title">Mon Application</string><stringname="hello_world">Bonjour le monde !</string></resources>

Note:You can use the locale qualifier (or any configuration qualifer) on any resource type, such as if you want to provide localized versions of your bitmap drawable. For more information, seeLocalization.

Use the String Resources


You can reference your string resources in your source code and other XML files using the resource name defined by the<string>element'snameattribute.

In your source code, you can refer to a string resource with the syntaxR.string.<string_name>. There are a variety of methods that accept a string resource this way.

For example:

// Get a string resource from your app's ResourcesString hello =getResources().getString(R.string.hello_world);// Or supply a string resource to a method that requires a stringTextView textView =newTextView(this);textView.setText(R.string.hello_world);

In other XML files, you can refer to a string resource with the syntax@string/<string_name>whenever the XML attribute accepts a string value.

For example:

<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world"/>

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android 开发屏蔽home键,返回键
  2. Android标签AndroidTagGroup(系列1)
  3. android 添加触摸反馈
  4. Android(安卓)音频播放
  5. Android(安卓)中Observer模式的使用
  6. Android中使用自身携带的Junit新建一个测
  7. android 中使用TableLayout实现表单布局
  8. 关于android安装sdk时找不到jdk的解决办
  9. Android(安卓)3.0 Hardware Acceleration
  10. Android(安卓)Handler 分析学习