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 Localization

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

除了上述幾種語言,其他常見語言的區域代碼與對應資料夾名稱整理如下:

序號 語言與代碼 1 南非荷蘭語
代碼:af 資料夾名稱:values-af 2 阿拉伯語
代碼:ar 資料夾名稱:values-ar 3 孟加拉語
代碼:bn 資料夾名稱:values-bn 4 捷克語
代碼:cs 資料夾名稱:values-cs 5 中文
代碼:zh 資料夾名稱:values-zh 6 德語
代碼:de 資料夾名稱:values-de 7 法語
代碼:fr 資料夾名稱:values-fr 8 日語
代碼:ja 資料夾名稱:values-ja

實作範例

以下範例示範如何運用字串本地化。我們會打造一個簡易應用,讓它能依美國與義大利兩種地區顯示不同的內容。

你可以選擇在實體裝置或模擬器上執行這個範例來驗證效果。

步驟 說明 1 使用 Android Studio 建立專案,命名為 Locals。建立時請確認 Target SDK 與 Compile With 採用最新版本的 Android SDK,以便使用較高階的 API 2 修改 src/MainActivity.java,加入所需程式碼 3 修改 res/layout/activity_main,加入對應的 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/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>

接著我們來執行這個完成本地化的應用。先安裝並啟動 AVD,若一切設定正確且程式沒有問題,就會看到如下的模擬器視窗:

現在透過「設定/系統/語言」把裝置語言切換為義大利語。

再次開啟應用,你會看到 Hello World 已變成義大利文的顯示。結果如下圖所示:


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