編輯:Android 開發實例
一個上架 Google Play 的 App,可能在幾十個國家的裝置上被開啟。使用者對「文字、數字、日期、檔案」的期待各不相同,如果 App 永遠只顯示一種語言,體驗與留存都會受影響。這就是本地化(Localization)要解決的問題。
好消息是,Android 從設計之初就把「介面文字」與「程式邏輯」分開管理,因此本地化的大部分工作,其實只是多準備幾份資源檔。
做法是:在 res 目錄下建立一個新的資料夾,命名規則為 values-後面接語言代碼。Android 會依裝置的語言設定自動挑選對應的資料夾;若找不到,就退回預設的 res/values。
以義大利文為例,資料夾名稱是 values-it,路徑看起來像這樣:
資料夾建好後,把預設的 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>
其他常見語言的代碼與資料夾名稱如下,需要時直接照表建立即可:
接下來做一個最小可用的示範:畫面上只有一行文字,當系統語言是英文時顯示 Hello world,切換成義大利文後就變成 Ciao mondo。
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;
}
}
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="action_settings">Settings</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="action_settings">Settings</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>
裝到 AVD 啟動,畫面如下:
接著到系統的「設定 → 語言與輸入 → 語言」,把語言改成 Italiano,然後重新開啟 App:
程式碼一行都沒改,文字卻已經換成義大利文——這就是資源系統的威力。
實務上還有兩個觀念值得補充:其一,除了文字,日期格式、貨幣符號、數字分隔與由右至左(RTL)的版面方向,也都需要一併考量;其二,千萬別把使用者看得懂的內容寫死在程式碼或版面檔裡,一律走 @string/...,否則後續要補語言時會非常痛苦。
Android JSON 解析入門
JSON 是一種輕量級的資料交換格式,常用於應用程式與伺服器之間傳輸資料。相比 XML,JSON 結構更簡潔,也更容易在 Android 中解析。本文說明 JSON 的基
Android Permission 權限級別與組件保護
Android 權限不僅用於安裝宣告,也可用於保護組件間通訊。不同 protectionLevel 會影響授權方式與風險提示。常見權限級別normal 屬於低風險權限,安
Android 本地化入門:讓同一個 App 說各國語言
你的 App 可能會被安裝在幾十個國家的裝置上。使用者看到一整片英文介面時的挫折感,往往就是卸載的起點。本地化(Localization)要解決的正是這件事:讓文字、數字
Android 下拉刷新控制項 RTPullListView 範例
RTPullListView 可用於下拉刷新與載入更多。頂部樣式可改 pulllist_head.xml,底部可改 list_footview.xml。Activity