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

Android 多語言支援入門:values 資料夾與 strings.xml 的組織方式

編輯:Android 開發實例

一個上架到 Google Play 的 App,可能同時被德國、日本、巴西的使用者安裝。如果畫面上的字永遠只有英文,對不懂英文的使用者來說,這個 App 等於不存在。本地化(Localization)就是解決這個問題的標準做法,而 Android 的資源系統天生就為它設計好了。

核心觀念:用資料夾區分語言

Android 在挑選資源時會參考裝置的語言環境(Locale)。你只要在 res 底下建立名稱為 values- 加上語言代碼的資料夾,系統就會自動比對並採用對應的那一份。

舉例來說,要支援義大利文,就建立 res/values-it。專案結構看起來像這樣:

在 res 底下建立各語言 values 資料夾的專案結構

資料夾建好之後,把預設 values/strings.xml 複製進去,然後只改「值」,不要改 name。Android 正是靠 name 把不同語言的同一句話配對起來的。

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

常用語言代碼

語言代碼資料夾名稱
Afrikaansafvalues-af
Arabicarvalues-ar
Bengalibnvalues-bn
Czechcsvalues-cs
Chinesezhvalues-zh
Germandevalues-de
Frenchfrvalues-fr
Japanesejavalues-ja

需要區分同一語言的不同地區時,可以加上地區限定,例如 values-zh-rTW(台灣繁中)與 values-zh-rCN(大陸簡中)。

完整範例:英文/義大利文雙版本

做一個只顯示一行字的 App,同時準備義大利文翻譯,然後透過切換裝置語言來觀察效果。

實作步驟

步驟做法
1以 Android Studio 建立專案 Locals,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,切換裝置語言後驗證。

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/textV8iew1"
        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 畫面

接著到系統設定把語言改成義大利文(設定 → 語言與輸入 → 語言 → Italiano)。

重新開啟 App,同一行文字已換成義大利文:

切換為義大利文後畫面顯示 Ciao mondo

三個容易忽略的重點

第一,預設的 values/strings.xml 一定要完整。當某語言缺少某個 key 時,系統會回退到預設值;若預設也缺,就會在執行時炸掉。第二,德文、俄文等語言的句子通常比英文長,版面配置要預留擴展空間,避免文字被截斷。第三,阿拉伯文、希伯來文屬於從右到左的書寫系統,若要在這些市場上線,版面設計一開始就該考慮 RTL 支援。

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