Create an Unlocalized Application

The first version of the Hello, L10N application will use only the default resource directories (res/drawable, res/layout, andres/values). These resources are not localized — they are the graphics, layout, and strings that we expect the application to use most often. When a user runs the application in the default locale, or in a locale that the application does not specifically support, the application will load resources from these default directories.

The application consists of a simple user interface that displays two TextView objects and a Button image with a background image of a national flag. When clicked, the button displays an AlertDialog object that shows additional text.

Create the Project and Layout

For this application, the default language will be British English and the default location the United Kingdom.

1. Start a new project and Activity called "HelloL10N." If you are using Eclipse, fill out these values in the New Android Project wizard:

o Project name: HelloL10N

o Application name: Hello, L10N

o Package name: com.example.hellol10n (or your own private namespace)

o Create Activity: HelloL10N

o Min SDK Version: 3

The basic project contains a res / directory with subdirectories for the three most common types of resources: graphics (res/drawable/), layouts (res/layout/) and strings (res/values/). Most of the localization work you do later in this tutorial will involve adding more subdirectories to the res / directory.

ti Package Explorer ¿1 HellcL LON

Hier;

gen [Generated Java Files]

E) & drawable 0layout K +] & values 1 Cl AndroidManifest.xml Hi default, properties

Open the res/layout/main.xml file and replace it with the following code:

<?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>

The LinearLayout has two TextView objects that will display localized text and one Button that shows a flag.

The LinearLayout has two TextView objects that will display localized text and one Button that shows a flag.

Create Default Resources

The layout refers to resources that need to be defined.

1. Create default text strings. To do this, open the res/values/strings.xml file and replace it with the following code:

<?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= For every locale,

"dialog text">This dialog box"'"s strings are not localised. the text here will come from values/strings.xml.</string>

</resources>

This code provides British English text for each string that the application will use. When we localize this application, we will provide alternate text in German, French, and Japanese for some of the strings.

2. Add a default flag graphic to the res/drawable folder by saving flag.png as res/drawable/flag.png. When the application is not localized, it will show a British flag.

3. Open HelloL10N.java (in the src/ directory) and add the following code inside the onCreate() method (after setContentView).

This code provides British English text for each string that the application will use. When we localize this application, we will provide alternate text in German, French, and Japanese for some of the strings.

2. Add a default flag graphic to the res/drawable folder by saving flag.png as res/drawable/flag.png. When the application is not localized, it will show a British flag.

3. Open HelloL10N.java (in the src/ directory) and add the following code inside the onCreate() method (after setContentView).

// assign flag.png to the button, loading correct flag image for current locale Button b; (b =

(Button)findViewByld(R.id.flag button)).setBackgroundDrawable(this.getResources( ).getDrawable(R.drawable.flag));

// build dialog box to display when user clicks the flag AlertDialog.Builder builder = new AlertDialog.Builder(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) {

final AlertDialog alert = builder.create(); // set click listener on the flag to show the dialog box b.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {

Tip: In Eclipse, use Ctrl-Shift-O (Cmd-Shift-O, on Mac) to find and add missing import packages to your project, then save the HelloL10N.java file.

The code that you added does the following:

o It assigns the correct flag icon to the button. For now, no resources are defined other than the default, so this code will always assign the contents of res/drawable/flag.png (the British flag) as the flag icon, no matter what the locale. Once we add more flags for different locales, this code will sometimes assign a different flag.

o It creates an AlertDialog object and sets a click listener so that when the user clicks the button, the AlertDialog will display. We will not localize the dialog text; the AlertDialog will always display the dialog text that is located within res/values/strings.xml. The project structure now looks like this:

Tip: If you will want to run the application on a device and not just on an emulator, open AndroidManifest.xml and add android :debuggable="true" inside the <application>element. For information about setting up the device itself so it can run applications from your system, see Developing on a Device.

0 0

Post a comment