Chapter 3 Hello, L10N (Localization)
In this chapter we will create a Hello, L10N applications, it will selectively loading some resources in accordance with the Android framework. Then we will add some resources to the res / directory to be localized by a process our application.
3.1 to create a non-localized applications
In the first version of Hello, L10N we only applicable in default resource directory (res / drawable, res / layout, res / values). These resources are not localized - which we often use graphics, layout, string, and so on. When a user in the default locale, the default directory will load these resources. The application contains a simple user interface, displaying a two TextView objects and image button. When the button is clicked, it displays AlertDialog object used to display additional text.
3.1.1 Creating a project and layout
For this program we use the simulator, the default language is English, the default area for England.
1. Create a "HelloL10N" project, and fill in the following fields
Project name: HelloL10N
Application name: Hello, L10N
Package name: com.example.hellol10n
Create Activity: HelloL10N
Min SDK Version: 4
At present the author's eclipse will automatically create the following directory structure, please note that not all are like Eclipse created, it may vary depending on the version you ADT plugin. We also need to back yourself to add some folder. Shown in Figure 3-1:
Figure 3-1 Directory Structure diagram created after the Eclipse project
2. Open res / layout / main.xml modify the code such as "3-1 Listing" below:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/text_a" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/text_b" /> <Button android:id="@+id/flag_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </LinearLayout>
Listing 3-1
3.1.2 Create a default resource
1. Open the res / values / strings.xml add code such as "3-2 Listing" below:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Hello, L10N</string> <string name="text_a">Shall I compare thee to a summer"'"s day?</string> <string name="text_b">Thou art more lovely and more temperate.</string> <string name="dialog_title">No Localisation</string> <string name="dialog_text">This dialog box"'"s strings are not localised. For every locale, the text here will come from values/strings.xml.</string> </resources>
Listing 3-2
As our simulator default setting is English, so we use here in English, we will add a language later in fact a number of other countries.
2. Add picture to the flag under res / drawable, without this we create a directory, not localized program will use this image
(flag.png)
Add "Listing 3-3" code 3. Open Open HelloL10N.java setContentView () in the onCreate ()
// according to the current locale to load the correct flag.png (of course, we are currently only for the default, so no matter what language environment is that the British flag) Button b; (b = (Button)findViewById(R.id.flag_button)).setBackgroundDrawable(this.getResources().getDrawable(R.drawable.flag)); // generate a dialog box when the user clicks the flag when this dialog box will pop up AlertDialog.Builder Builder = new new AlertDialog.Builder ( the this ); builder.setMessage(R.string.dialog_text) .setCancelable(false) .setTitle(R.string.dialog_title) .setPositiveButton("Done", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); final AlertDialog alert = builder.create(); // When a flag button is clicked, a dialog box we created above b.setOnClickListener ( new new View.OnClickListener () { public void onClick(View v) { alert.show(); } });
Listing 3-3
3.2 The Executive is not localized applications
We saved code, and then run the application, the run should result shown in Figure 3-2:
Effect after operation |
Click the flag after effects |
Figure 3-2 run no screenshots localization projects
3.3 Localized planning
The first step in the localization of the application is planned for our application which needs to render several regions. In this application, the default locale will be the United Kingdom. We will add some specific information, China, Germany, France region. Table 3-1 shows the areas where we need to plan.
Region / Language |
United Kingdom |
Germany |
France |
China |
Other countries |
|
English, the default British flag |
- |
- |
|
English, the default British flag |
German |
- |
German flag and the German text, modify |
- |
- |
|
French |
- |
- |
French flag and German text, modify |
|
|
China |
- |
- |
- |
- Chinese national flag and Chinese characters, modify |
|
other languages |
|
|
|
|
English, the default British flag |
Table 3-1 regional localization application programming
According to the above plan, in addition to the UK, we also need to add strings.xml three countries. Please note that a language could apply to many areas, such as English. The following table 3-2 corresponding extension of the country, for more details please refer to the correspondence referred to ISO639-1 (language), ISO3166-1-alpha-2 (area)
Native code |
Language / country |
Local strings.xml |
Local flag.png |
default |
English / UK |
res/values/ |
res/drawable/ |
de-RDE |
German / Germany |
res / values-in / |
res / drawable-de-RDE / |
fr-rFR |
French / France |
res/values-fr/ |
res/drawable-fr-rFR/ |
zh-RCN |
Chinese / Chinese |
res/values-zh/ |
res / drawable-zh-RCN / |
Table 3-2 countries referred to
At runtime, the system will be automatically loads the appropriate images and text according to the local device to the selected region and language. If there is no corresponding countries and regions will display the default British flag and English.
3.4 localized application
3.4.1 localized strings
We need to create three additional strings.xml file, corresponding to German, French, Chinese, because of Eclipse, it makes our job a lot easier
1. Select File> New> Android XML File
2. Select Our L10N project, and then select File field, enter the following Language in strings.xml click -> insert button to the right. Shown in Figure 3-3:
Figure 3-3
3. Enter here to automatically create de res / values-de / strings.xml. Figure 3-4:
Figure 3-4
3. Like the rest of the creation method, and then click Create:
RES / values-fr /
RES / values-zh /
4. Add the following text to the respective strings.xml, as shown in Table 3-3:
file |
Code: |
|
<?xml version="1.0" encoding="utf-8"?> <resources> < String name = "app_name" > Hello, localization </ string > < String name = "TEXT_A" > Shall I compare thee to a summer, </ string > < String name = "text_b" > Thou much sweeter and softer you? </ String > </resources> |
|
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Bonjour, Localisation</string> < String name = "text_a" > Shall I compare thee to a summer day? </ String > <string name="text_b">Tu es plus tendre et bien plus tempéré.</string> </resources> |
|
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">你好,本地化</string> <string name="text_a">夏天</string> < String name = "text_b" > you are more gentle and more lovely </ String > </resources> |
Table 3-3 national language
3.4.2 Localization Pictures
(Res / drawable-de-rDE / flag.png) Germany
(res/drawable-fr-rFR/flag.png)法国
(res/drawable-zh-rCN/flag.png)中国
Implementation and testing of localized applications 3.5
Once you've added localized string and image resources, ready to run and test the application. So we need to change the locale of the device or emulator, some devices may not be set regional and language. But simulator, there is a picture on the desktop to help us set up, shown in Figure 3-5:
Figure 3-5
When selecting a language, we need to press a list, shown in Figure 3-6:
Figure 3-6
After running the program, you will see the results we want.
Reproduced in: https: //www.cnblogs.com/Codenewbie/articles/2973176.html