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

Android 本地化:讓應用程式適配不同語言環境

編輯:Android 開發實例

Android 應用程式可以在許多不同地區的許多裝置上執行。為了讓應用程式更具互動性,它應該能處理文字、數字、檔案等內容,使其適配應用程式即將使用的語言環境。

本章將說明如何依據不同區域,把應用程式中使用的字串做本地化;以同樣的方式,其他內容也能夠本地化。

本地化字串

要對應用程式中使用的字串做本地化,需在 res 底下建立一個新的資料夾,命名為 values-local,並把 local 替換成對應的語言代碼。

例如在義大利的情況下,就在 res 底下建立 values-it 資料夾。如下圖所示:

Android 本地化教學

這個資料夾建立完成後,把預設資料夾中的 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>

除了這些語言,其他語言的區域代碼已在表中給出:

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 建立 Android 應用程式,命名為 Locals。建立專案時,Target SDK 請編譯於最新版本的 Android SDK 或採用更高等級的 API。
2. 修改 src/MainActivity.java,加入必要程式碼。
3. 修改 res/layout/activity_main.xml,加入對應的 XML 元件。
4. 修改 res/values/string.xml,加入必要字串。
5. 建立 res/values-it/string.xml,加入必要字串。
6. 執行應用程式,選擇一部 Android 裝置安裝並驗證結果。

修改後的主活動檔案 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/text1" 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>

接著來執行修改後的本地化應用程式。把程式安裝到 AVD 並啟動,如果一切設定與程式都沒有問題,會顯示下面的模擬器畫面:

現在從選單/系統設定/語言,把裝置語言改成義大利文。

現在再次開啟應用程式,這時會看到 Hello World 以義大利文顯示,如下圖所示:

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