• Android Video Tutorials

Android - 本地化



一個 Android 應用可以在許多不同地區的不同裝置上執行。為了使您的應用程式更具互動性,您的應用程式應以適合應用程式將要使用的地區的語言環境處理文字、數字、檔案等。

將字串更改為不同語言的方式稱為本地化。

在本章中,我們將解釋如何根據不同的地區等本地化您的應用程式。我們將本地化應用程式中使用的字串,並且以同樣的方式可以本地化其他內容。

本地化字串

為了本地化應用程式中使用的字串,請在 res 下建立一個名為 values-local 的新資料夾,其中 local 將被替換為區域。

例如,在義大利的情況下,將建立 values-it 資料夾在 res 下。它在下圖中顯示 -

Anroid Localization Tutorial

建立該資料夾後,將 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-it/strings.xml

<;?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="hello_world">Hola Mundo!</string>
</resources>

法語,res/values-it/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 在包 com.example.sairamkrishna.myapplication 下建立一個 Android 應用程式。
2 修改 res/layout/activity_main 以新增相應的 XML 元件
3 修改 res/values/string.xml 以新增必要的字串元件
4 執行應用程式並選擇一個正在執行的 Android 裝置並在其上安裝應用程式並驗證結果

以下是修改後的 xml res/layout/activity_main.xml 內容。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" 
   android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" 
   tools:context=".MainActivity">
   
   <TextView android:text="Wifi" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/hindi"
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="50dp"
      android:textColor="#ff59ff1a"
      android:textSize="30dp" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/marathi"
      android:id="@+id/textView3"
      android:textSize="30dp"
      android:textColor="#ff67ff1e"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/arabic"
      android:id="@+id/textView4"
      android:layout_below="@+id/textView3"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="42dp"
      android:textColor="#ff40ff08"
      android:textSize="30dp" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/chinese"
      android:id="@+id/textView5"
      android:layout_below="@+id/textView4"
      android:layout_alignLeft="@+id/textView3"
      android:layout_alignStart="@+id/textView3"
      android:layout_marginTop="42dp"
      android:textSize="30dp"
      android:textColor="#ff56ff12"
      android:layout_alignRight="@+id/textView3"
      android:layout_alignEnd="@+id/textView3" />

</RelativeLayout>

以下是 res/values/string.xml 的內容。

<resources>
   <string name="app_name">My Application</string>
   <string name="hello_world">Hello world!</string>
   <string name="action_settings">Settings</string>
   <string name="hindi">ట్యుటోరియల్స్ పాయింట్</string>
   <string name="marathi">शिकवण्या बिंदू</string>
   <string name="arabic">نقطة الدروس7</string>
   <string name="chinese">教程點</string>
</resources>

讓我們嘗試執行我們剛剛修改的應用程式。我假設您在進行環境設定時建立了您的 AVD。要從 Android Studio 執行應用程式,請開啟專案的某個 Activity 檔案並單擊工具欄中的執行 Eclipse Run Icon 圖示。Android Studio 將應用程式安裝到您的 AVD 上並啟動它,如果您的設定和應用程式一切正常,它將顯示以下模擬器視窗 -

Anroid Localization Tutorial
廣告