Android - 概述

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>

除了这些语言之外,其他语言的区域代码已在下表中给出 :

Sr.No语言&代码
1

Afrikanns

代码: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包下创建Android应用程序. sairamkrishna.myapplication.
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工作室运行应用程序,请打开项目的一个活动文件,然后单击运行Eclipse Run Icon icon从工具栏中. Android工作室在您的AVD上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示以下模拟器窗口 :

Anroid本地化教程