編輯:Android 開發實例
一支上架的 App,可能同時被義大利米蘭的上班族、日本東京的學生、德國柏林的工程師打開。使用者對文字、數字、日期與檔案格式的期待各不相同,因此「本地化」不是加分題,而是基本功。
本章要說明的是:如何讓 App 依據裝置的語言環境,自動切換顯示的字串;而同一套邏輯,也能延伸到其他資源的本地化。
做法是替 res 底下的資源建立一個新的資料夾,命名規則為 values-/ 加上語言代碼,例如義大利文的 values-it:
資料夾建立完成後,把預設 values 裡的 strings.xml 複製一份進去,再改寫內容。以下把 Hello World 換成各語言的說法:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello_world">Ciao mondo!</string> </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello_world">Hola Mundo!</string> </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello_world">Bonjour le monde !</string> </resources>
其他常見語言的代碼與對應資料夾如下:
| 編號 | 語言 | 代碼 | 資料夾名稱 |
|---|---|---|---|
| 1 | 南非荷蘭語 | af | values-af |
| 2 | 阿拉伯語 | ar | values-ar |
| 3 | 孟加拉語 | bn | values-bn |
| 4 | 捷克語 | cs | values-cs |
| 5 | 中文 | zh | values-zh |
| 6 | 德語 | de | values-de |
| 7 | 法語 | fr | values-fr |
| 8 | 日語 | ja | values-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!:
一個好的本地化策略,除了字串,也該留意數字格式、貨幣符號、日期寫法與由右至左語言的版面鏡像需求。
Android 本地化實戰:用資源限定符自動切換語言
Android App 可能同時被台灣、日本、德國的使用者安裝使用。若介面文字硬寫在程式碼或版面檔中,就永遠只能呈現一種語言。本地化要做的,是把這些文字抽成資源,並依裝置
Android 自訂 ViewGroup 打造多風格 SlidingMenu
側滑選單是許多 App 常見的互動模式。本文以自訂 ViewGroup 的方式,從最基礎的側滑菜單開始,逐步加入 Scroller 自動吸附、事件攔截、滑動衝突處理,最後
Android GPS 定位用法實例:LocationManager 與 LocationListener 完整示範
GPS 定位是手機上非常實用的能力。本文以實例說明在 Android 中如何透過 GPS 取得當前經緯度,並即時顯示在畫面上。 基本觀念 在 Android 中取得位置,
Android 圖片異步上傳到 PHP 伺服器實作
在 Android 應用中,將圖片上傳到伺服器是很常見的需求。本文以 PHP 作為接收端,說明 Android 如何透過 HTTP POST 與 multipart