Android 简明教程

Android - Localization

安卓应用程序可以在许多不同地区的许多设备上运行。为了使你的应用程序更具交互性,你的应用程序应以适合你的应用程序所用区域的方式处理文本、数字、文件等。

An android application can run on many devices in many different regions. In order to make your application more interactive, your application should handle text,numbers,files e.t.c in ways appropriate to the locales where your application will be used.

在本章中,我们将解释如何根据不同的区域(等)本地化你的应用程序。我们将本地化应用程序中使用的字符串,并以相同的方式本地化其他内容。

In this chapter we will explain , how you can localize your application according to different regions e.t.c. We will localize the strings used in the application, and in the same way other things can be localized.

Localizing Strings

要本地化你的应用程序中使用的字符串,请在 res 下新建一个名为 values-local 的文件夹,其中local将替换为区域。

In order to localize the strings used in your application , make a new folder under res with name of values-local where local would be the replaced with the region.

例如,对于意大利语, values-it 文件夹应在res下创建。图像中显示为:

For example, in the case of italy, the values-it folder would be made under res. It is shown in the image below −

string

创建该文件夹后,将*strings.xml*从默认文件夹复制到已创建的文件夹中。并更改其内容。例如,我更改了hello_world字符串的值。

Once that folder is made, copy the *strings.xml*from default folder to the folder you have created. And change its contents. For example, i have changed the value of hello_world string.

Italy, res/values-it/strings.xml

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

Spanish, res/values-it/strings.xml

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

French, res/values-it/strings.xml

<;?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="hello_world">Bonjour le monde !</string>
</resources>

除了这些语言之外,其他语言的区域代码如下表所示:

Apart from these languages, the region code of other languages have been given in the table below −

Sr.No

Language & code

1

Afrikanns Code: af. Folder name: values-af

2

Arabic Code: ar. Folder name: values-ar

3

Bengali Code: bn. Folder name: values-bn

4

Czech Code: cs. Folder name: values-cs

5

Chinese Code: zh. Folder name: values-zh

6

German Code: de. Folder name: values-de

7

French Code: fr. Folder name: values-fr

8

Japanese Code: ja. Folder name: values-ja

Example

要使用此示例,你可以在实际设备或模拟器上运行此示例。

To experiment with this example , you can run this on an actual device or in an emulator.

Steps

Description

1

You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication.

2

Modify the res/layout/activity_main to add respective XML components

3

Modify the res/values/string.xml to add necessary string components

4

Run the application and choose a running android device and install the application on it and verify the results

以下是修改后的 xml res/layout/activity_main.xml 内容。

Following is the modified content of the 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 的内容。

Following is the content of the 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 运行该应用程序,请打开一个项目的活动文件,并单击工具栏中的运行图标。Android Studio 会将该应用程序安装在你的 AVD 上并启动它,如果你的设置和应用程序一切正常,它将显示以下模拟器窗口−

Let’s try to run our application we just modified. I assume you had created your AVD while doing environment setup. To run the app from Android studio, open one of your project’s activity files and click Run icon from the toolbar. Android studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window −

localization