2017年6月2日 星期五

Android :多國語系

Android 應用程式可以在不同語言的環境中顯示不同介面的語系,當使用者的手機使用英文環境時,應用程式介面就以英文顯示;當使用者的手機使用中文環境時,應用程式介面就以中文顯示。


步驟:
1.建立版面中的文字的字串資源(String resource)
2.建立語系字串資源檔,也就是翻譯資料

實作如下:

1.建立版面字串資源(String resource)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="tw.com.hjchen.myapplication.MainActivity"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/one"/>  <!-- 字串資源-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/two"/>  <!-- 字串資源-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/three"/>  <!-- 字串資源-->
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="bottom|center">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/_123" />  <!-- 字串資源-->
    </LinearLayout>
    
</LinearLayout>

在res\valuse\strings.xml底下可以更改設定

2.建立語系資源檔(Ex:繁體中文)

在xml的預覽畫面上方的工具列的地球圖示(Language),選擇Edit Traslations

選擇Edit Traslations後會出現Translations Editor畫面,如下:

點選左上的小地球圖示,選擇Chinese(zh)


選擇Chinese後,Translations Editor畫面會多出各個資源可以讓你輸入中文翻譯的區塊,如下:


完成後,會自動產生國旗圖示的strings.xml(zh)資源檔,如下:

PS:要執行前,請先把手機或模擬器的語系設定為繁體中文

1 則留言: