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

Android 本地化實戰:用資源資料夾做多語系 App

編輯:Android 開發實例

同一支 App,會在不同國家、不同語言環境的裝置上被開啟。要讓各地使用者都覺得順手,App 就必須依語言環境調整文字、數字、日期與檔案格式。這件事在 Android 裡有現成的機制可以借力。

本章說明如何讓 App 依裝置語言自動切換字串;相同的思路也適用於圖片、版面等其他資源。

把字串抽出來,才有本地化的可能

關鍵觀念是:所有顯示給使用者看的文字,都不該寫死在程式碼或版面檔裡,而應放進資源檔。接著在 res 底下新增資料夾,命名格式為 values- 加上語言代碼。

以義大利文為例,資料夾名稱就是 values-it:

資料夾建好後,把預設 values 裡的 strings.xml 複製過去,改寫內容即可。以下示範把同一句問候語寫成三種語言:

義大利語 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

範例:切到義大利文環境就顯示義大利文

這個範例示範如何讓同一支 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;
}
}

請注意這段程式碼裡完全沒有語言判斷的邏輯。切換語言是資源系統自動完成的,開發者不必多寫一行——這是 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/text iew1" 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,這次問候語已經變成義大利文的寫法:

若要更進階,還可以加上地區限定(如 values-zh-rTW)、用 plurals 處理可數量詞,或針對由右至左語言調整版面方向。

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