Hello, L10N | Android Developers
http://developer.android.com/intl/ja/resources/tutorials/localization/index.html
ローカライズ(多言語対応)のチュートリアルです。HelloL10Nというプロジェクトを作成します。
res/layout/main.xmlを開き、以下のように変更します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "match_parent" android:layout_height = "match_parent" > < TextView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:gravity = "center_horizontal" android:text = "@string/text_a" /> < TextView android:layout_width = "match_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 > |
1 2 3 4 5 6 7 8 9 10 11 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > < string name = "hello" >Hello World, HelloL10NActivity!</ string > < string name = "app_name" >HelloL10N</ 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 > |
HelloL10NActivity.javaを開き、以下のように変更します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package jp.blogspot.logroid.helloL10N; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; public class HelloL10NActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); // assign flag.png to the button, loading correct flag image for current locale Button b; (b = (Button)findViewById(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) { dialog.dismiss(); } }); 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) { alert.show(); } }); } } |
この時点では、ローカライズされていない状態です。


res/values/を右クリックし、Android XMLファイルを追加します。
ファイル名をstrings.xmlとして、次へをクリックします。
Available QualifiersからLanguageを選択し、->ボタンをクリックします。
言語にdeを入力し、完了をクリックします。
同様に、fr、jaのstrings.xmlを作成します。
それぞれのファイル内容は以下の通り設定します。
res/values-de/strings.xml
1 2 3 4 5 6 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > < string name = "app_name" >Hallo, Lokalisierung</ string > < string name = "text_a" >Soll ich dich einem Sommertag vergleichen,</ string > < string name = "text_b" >Der du viel lieblicher und sanfter bist?</ string > </ resources > |
1 2 3 4 5 6 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > < string name = "app_name" >Bonjour, Localisation</ string > < string name = "text_a" >Irai-je te comparer au jour d\'été?</ string > < string name = "text_b" >Tu es plus tendre et bien plus tempéré.</ string > </ resources > |
1 2 3 4 5 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > < string name = "text_a" >あなたをなにかにたとえるとしたら夏の一日でしょうか?</ string > < string name = "text_b" >だがあなたはもっと美しく、もっとおだやかです。</ string > </ resources > |
実行します。

セットしたいロケールをロングタップし、言語を設定します。


0 件のコメント:
コメントを投稿