Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 應用本地化:讓 App 說出使用者的語言

編輯:Android 開發實例

一支上架的 App,可能同時被義大利米蘭的上班族、日本東京的學生、德國柏林的工程師打開。使用者對文字、數字、日期與檔案格式的期待各不相同,因此「本地化」不是加分題,而是基本功。

本章要說明的是:如何讓 App 依據裝置的語言環境,自動切換顯示的字串;而同一套邏輯,也能延伸到其他資源的本地化。

本地化字串

做法是替 res 底下的資源建立一個新的資料夾,命名規則為 values-/ 加上語言代碼,例如義大利文的 values-it:

資料夾建立完成後,把預設 values 裡的 strings.xml 複製一份進去,再改寫內容。以下把 Hello World 換成各語言的說法:

義大利語 res/values-it/strings.xml

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello_world">Ciao mondo!</string>
</resources>

西班牙語 res/values-es/strings.xml

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello_world">Hola Mundo!</string>
</resources>

法語 res/values-fr/strings.xml

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

其他常見語言的代碼與對應資料夾如下:

編號語言代碼資料夾名稱
1南非荷蘭語afvalues-af
2阿拉伯語arvalues-ar
3孟加拉語bnvalues-bn
4捷克語csvalues-cs
5中文zhvalues-zh
6德語devalues-de
7法語frvalues-fr
8日語javalues-ja

小提醒:若同一語言存在地區差異(例如繁體中文與簡體中文),可以加上地區限定符,寫成 values-zh-rTW 這類形式。

完整範例:切換語言後字串也跟著變

這個範例建立一個最小可用的 App,當系統語言設為義大利文時,畫面上的文字就會變成義大利文。

步驟做法
1用 Android Studio 建立專案,命名為 Locals。
2修改 src/MainActivity.java。
3修改 res/layout/activity_main.xml。
4修改 res/values/string.xml。
5新增 res/values-it/string.xml 並填入義大利文字串。
6執行 App,於裝置上驗證結果。

src/com.yiibai.locals/MainActivity.java:

 package com.example.locals;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

值得注意的是,這個 Activity 完全沒有處理語言——切換語言的工作交給 Android 資源系統自動完成,這正是本地化的美麗之處。

res/layout/activity_main.xml:

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView android:id="@+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" android:layout_marginTop="174dp"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

預設 res/values/string.xml:

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Locals</string>
<string name="hello_world">Hello world!</string>
</resources>

義大利文版 res/values-it/string.xml:

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Locals</string>
<string name="hello_world">Ciao mondo!</string>
</resources>

AndroidManifest.xml:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.yiibai.locals" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<application android:allowBackup="true" android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@style/AppTheme" >
<activity android:name="com.yiibai.locals.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

執行結果

將 App 安裝到 AVD 並啟動,畫面如下:

接著到「設定 → 系統 → 語言與輸入」把裝置語言改為義大利文。

重新開啟 App,這次顯示的就是義大利文的 Ciao mondo!:

一個好的本地化策略,除了字串,也該留意數字格式、貨幣符號、日期寫法與由右至左語言的版面鏡像需求。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved