編輯:Android 開發實例
Android 應用可能運行在各種地區的各種裝置上。為了讓應用更具親和力,其中的文字、數字、檔案等內容都應該配合裝置的語言環境呈現。
本章要解釋的是:如何針對不同區域本地化應用中的字串,而其他類型的資源也可以用同樣的方式處理。
要讓應用中的字串支援多語,做法是在 res 之下新增一個名為 values-local 的資料夾,並把 local 替換成對應的區域代碼。
舉例來說,義大利對應的資料夾是 values-it,放在 res 底下,如下圖:
資料夾建立完成後,把預設資料夾中的 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 | Afrikaans | af | values-af |
| 2 | Arabic | ar | values-ar |
| 3 | Bengali | bn | values-bn |
| 4 | Czech | cs | values-cs |
| 5 | Chinese | zh | values-zh |
| 6 | German | de | values-de |
| 7 | French | fr | values-fr |
| 8 | Japanese | ja | values-ja |
以下範例示範如何使用字串本地化,做出一個能依美國與義大利地區顯示不同語言的應用。
| 步驟 | 做法 |
|---|---|
| 1 | 用 Android Studio 建立專案並命名為 Locals,Target SDK 與 Compile With 選最新版本或較高 API 等級。 |
| 2 | 修改 src/MainActivity.java 加入必要程式碼。 |
| 3 | 修改 res/layout/activity_main.xml 加入版面元件。 |
| 4 | 修改 res/values/string.xml 加入字串。 |
| 5 | 建立 res/values-it/string.xml 並加入字串。 |
| 6 | 執行應用程式,選擇執行裝置並驗證結果。 |
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;
}
}
<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/textV 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>
<?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>
<?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>
<?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 並啟動,若設定與程式都沒有問題,會顯示以下模擬器視窗:
現在從選單進入系統設定,把裝置語言改為義大利語。
接著再次開啟應用程式,這次就會看到義大利文的 Hello World,如下圖所示:
整個過程中,Java 程式碼完全沒有變動,切換語言靠的純粹是資源資料夾的分流機制。
Android 應用本地化:讓 App 輕鬆支援多語言與地區
Android 應用往往運行在name不同國家、不同語言環境的裝置上。若希望 App 真正做到「在地化」,就必須讓文字、數字、日期與檔案等资源都能依裝置的語言環境自動切換
Android JSON 解析入門:用 JSONObject 撈出你要的那幾個欄位
JSON(JavaScript Object Notation)自從被提出之後,因為格式輕量、可讀性高,很快就在 Web API 的世界裡取代了 XML 的位置。不管是瀏
Android Spinner 載入動畫:按鈕觸發與顯示控制
Spinner 是 Android 中常見的等待提示元件。當使用者觸發某個操作,但系統需要一點時間處理時,可以用轉圈動畫告知目前仍在進行中。 基本設定 在版面配置中加入
打造 3D 翻轉動畫:Rotate3dAnimation 函數剖析
上圖就是最終的翻轉效果。由於 Android 並未提供現成的 3D 翻轉動畫,我們得自己動手實作。先來分析 Animation 與 Transformation 這兩個