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

Android 本地化實作演練:從英文預設值到義大利文翻譯

編輯:Android 開發實例

把寫死的中文或英文字串丟進 App,短期很快,長期很痛。Android 早就提供了答案:所有顯示文字都放進資源檔,再依語言準備對應版本,系統會依裝置設定自動挑選。本章從建立資源目錄開始,做出一個能隨系統語言切換的 App。

建立語言資源資料夾

規則很單純:在 res 底下開一個 values- 加語言代碼的資料夾。以義大利為例,就是 values-it:

Anroid Localization TutZ喎?/kf/ware/vc/vcmlhbA==" data-ke-="" src="/uploadfile/2016/0203/20160203105145609.jpg" />

接著把預設的 strings.xml 複製一份進去,只替換需要翻譯的文字,key 名稱維持不變。

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

其他常用語言的代碼與資料夾名稱:

語言代碼/資料夾
Afrikaansaf/values-af
Arabicar/values-ar
Bengalibn/values-bn
Czechcs/values-cs
Chinesezh/values-zh
Germande/values-de
Frenchfr/values-fr
Japaneseja/values-ja

動手做一個雙語 App

步驟要做的事
1用 Android Studio 建立專案 Locals,Target SDK 選新版。
2修改 MainActivity.java(本例無額外邏輯)。
3修改 activity_main.xml 加入一個 TextView。
4修改 res/values/string.xml 加入英文預設字串。
5新增 res/values-it/string.xml 加入義大利文翻譯。
6執行並切換系統語言驗證。

MainActivity.java:

 package com.example.locals; import android.os.Bundle; import android.app.Activity; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }

activity_main.xml:

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textV1" android:layout_centerHorizontal="true" android:text="@string/hello_world" android:textAppearance="?android:attr/textAppearanceLarge" /> </RelativeLayout>

兩份字串資源:

 <!-- res/values/string.xml --> <resources> <string name="app_name">Locals</string> <string name="hello_world">Hello world!</string> </resources>

<!-- res/values-it/string.xml --> <resources> <string name="app_name">Locals</string> <string name="hello_world">Ciao mondo!</string> </resources>

AndroidManifest.xml:

 <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.yiibai.locals"> <application android:label="@string/app_name"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

切換語言看結果

英文環境下啟動 App:

到系統設定把語言改為義大利文,再回來開啟 App,同一行字已經換成當地的問候語:

想把 App 內的語言強制切換(不受系統設定影響),可以在建立 Locale 物件後呼叫 updateConfiguration(),並記得重建 Activity 讓版面重新載入資源。不過多數情況下,遵循系統設定才是對使用者友善的做法。

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