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

Android 應用在地化:字串資源與語言代碼對照

編輯:Android 開發實例

一支上架的 App,使用者可能來自任何語言環境。要讓各地使用者都讀得懂畫面上的字,就得把文字從程式碼中抽出來,按語言分別準備。Android 對這件事有完整的資源機制支援,本章說明字串本地化的做法,其他資源的概念也一樣。

字串本地化

做法是在 res 底下建立一個新的資源目錄,名稱為 values-local 的形式,其中 local 換成對應的區域代碼。Android 會依裝置語言自動選用。

以義大利為例,目錄名稱就是 values-it,放在 res 之下:

res 目錄下的 values-it 資料夾

目錄建立好之後,把預設資料夾裡的 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>

其他語言的代碼與對應目錄整理如下:

語言代碼目錄名稱
Afrikaans(南非荷蘭語)afvalues-af
Arabic(阿拉伯語)arvalues-ar
Bengali(孟加拉語)bnvalues-bn
Czech(捷克語)csvalues-cs
Chinese(中文)zhvalues-zh
German(德語)devalues-de
French(法語)frvalues-fr
Japanese(日語)javalues-ja

範例:英文與義大利文版本並存

以下範例示範字串本地化的實際操作,做出一個能依美國與義大利地區設定顯示不同語言的 App。

你可以在實機或模擬器上執行驗證。

步驟要做的事
1用 Android Studio 建立專案,命名為 Locals。Target SDK 與 Compile With 設為最新版本或較高 API 等級。
2修改 src/MainActivity.java。
3修改 res/layout/activity_main.xml。
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 裝到 AVD 啟動,畫面顯示預設語言的文字:

App 以預設語言啟動

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

重新開啟 App,問候語已換成義大利文:

切換為義大利文後的畫面
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved