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

Android 本地化:讓同一個 App 說各國語言

編輯:Android 開發實例

一支上架的 Android App,可能會被安裝在幾十個國家的裝置上。使用者期待的是打開來就看到自己熟悉的語言、符合本地習慣的數字與日期格式。這種「讓 App 融入當地環境」的工作,就稱為本地化(Localization)。

本章要說明的是最基礎也最常被用到的一塊:如何讓 App 依據裝置的語言環境,自動切換畫面上的文字。其他資源(圖片、音效、版面)也能用同一套機制處理。

本地化字串的做法

原理很單純:在 res 底下建立以「values-」開頭、後面接語言代碼的資料夾,Android 在執行時就會依裝置語系自動挑選對應的那一份資源。比方說要支援義大利文,就建一個 res/values-it:

Android Localization

資料夾建好後,把預設 res/values 裡的 strings.xml 複製一份進去,然後只改文字的內容、保留相同的 name。以下示範同一句 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南非荷蘭語 Afrikaansafvalues-af
2阿拉伯語 Arabicarvalues-ar
3孟加拉語 Bengalibnvalues-bn
4捷克語 Czechcsvalues-cs
5中文 Chinesezhvalues-zh
6德語 Germandevalues-de
7法語 Frenchfrvalues-fr
8日語 Japanesejavalues-ja

如果只想針對「某國的中文」做調整,可以把語言與地區串起來,例如 values-zh-rTW 代表台灣繁體中文,values-zh-rCN 代表中國大陸簡體中文。

範例:一個會跟著語系變臉的 App

這個範例做了兩份字串資源(預設英文與義大利文),切換裝置語言後,同一行文字會顯示不同結果。可在實機或模擬器上驗證。

步驟做法
1用 Android Studio 建立專案,命名為 Locals,Target SDK 與 Compile With 對到最新版 SDK。
2修改 src/MainActivity.java,加入必要程式碼。
3修改 res/layout/activity_main.xml,擺上顯示文字的 TextView。
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) { // Inflate the menu; this adds items to the action bar if it is present. 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>

執行結果

安裝並啟動 App,畫面上會先出現預設的英文:

接著到裝置的「設定 → 語言與輸入 → 語言」,把系統語言改成義大利文:

回到 App 重新開啟,同一行文字就換成義大利文了:

幾個實務提醒:預設的 res/values 一定要放一份完整字串,這是語系不支援時的後備;字串若要插入變數,別用字串拼接,改用格式化佔位符並搭配 getString(R.string.xxx, arg),因為不同語言的語序可能不同;另外阿拉伯語、希伯來語屬右到左書寫系統,版面需要開啟 RTL 支援並另外提供對應的版面配置。

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