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

Android 本地化入門:靠資源資料夾自動切換語言

編輯:Android 開發實例

你的 App 可能同時被台灣、日本、德國的使用者開啟。如果介面永遠只有一種語言,對不諳該語言的人來說就是一道門檻。本地化要處理的範圍包括文字、數字格式、日期寫法,乃至於圖示與檔案。

Android 的做法非常直覺:把各語言的翻譯分別放進命名規則固定的資源資料夾,系統會依裝置的語言設定自動挑選合適的一份。你的程式碼完全不必知道目前是哪個語言。

建立 values-xx 資料夾

在 res 底下新增資料夾,名稱為 values- 加上語言代碼。以義大利文為例,資料夾就是 res/values-it:

Android 本地化資料夾結構

接著把預設 values 裡的 strings.xml 複製到新資料夾,只改寫值、保留鍵名稱。舉例來說,把 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>

其他語言代碼對照如下:

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

範例:切換語言看效果

這個範例只有一行文字,但同時準備了預設英文與義大利文兩份資源。改裝置語言後重開 App,文字就會改變。可於實機或模擬器測試。

步驟做法
1用 Android Studio 建立專案 Locals,Target SDK 選較新版本。
2調整 src/MainActivity.java(此例邏輯極簡)。
3在 res/layout/activity_main.xml 放一個 TextView。
4在 res/values/string.xml 放預設字串。
5新增 res/values-it/string.xml 放義大利文。
6執行並驗證。

主活動 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/textViedw1" 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" >
    <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 啟動後,畫面顯示預設的英文:

接著從設定選單進入系統語言,把語言改為義大利文。

再次開啟 App,同一行文字已變成義大利文:

值得强调的是,這中間 Java 程式碼一個字都沒改。這正是把「文字」抽成資源的意義:介面翻譯交給翻譯人員處理,邏輯交給工程師,兩邊互不干擾。

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