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

Android 多語系資源怎麼放?values 資料夾命名規則一次搞懂

編輯:Android 開發實例

當 App 的使用者來自不同國家,文字、數字、日期甚至閱讀方向都得跟著調整。Android 把這件事抽象成「資源覆寫」:預設放一份在 res/values,其他語言各自放一份在對應的資料夾,系統在執行時依裝置地區自動挑一份最合適的。

本章把命名規則、常見語言代碼與一個可驗證的範例都梳理一遍。

資料夾命名規則

格式是 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>

常見語言代碼對照

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

除了語言,還可以搭配限定符做更細的控制:-r 前綴代表地區(如 values-zh-rTW)、-land 代表橫向、-sw600dp 代表平板尺寸。多個限定符依固定順序串接,寫錯順序資源就會失效。

範例:英文與義大利文雙版本

步驟要做的事
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,同一個 TextView 就換成 Ciao mondo:

一個容易被忽略的細節:預設的 values 資料夾永遠不能拿掉。少了它,一旦使用者裝置的語言沒有對應翻譯,App 就會在載入字串時崩潰。

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