Android Training/Supporting Different Languages: Unterschied zwischen den Versionen

Vorbereitung für Übersetzung
(Die Seite wurde neu angelegt: „It’s always a good practice to extract UI strings from your app code and keep them in an external file. Android makes this easy with a resources directory in…“)
 
(Vorbereitung für Übersetzung)
Zeile 1: Zeile 1:
<languages />
<translate>
It’s always a good practice to extract UI strings from your app code and keep them in an external file. Android makes this easy with a resources directory in each Android project.
It’s always a good practice to extract UI strings from your app code and keep them in an external file. Android makes this easy with a resources directory in each Android project.


Zeile 6: Zeile 8:
To add support for more languages, create additional <code>values</code> directories inside <code>res/</code> that include a hyphen and the ISO language code at the end of the directory name. For example, <code>values-es/</code> is the directory containing simple resourcess for the Locales with the language code "es". Android loads the appropriate resources according to the locale settings of the device at run time. For more information, see [http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources Providing Alternative Resource]s.
To add support for more languages, create additional <code>values</code> directories inside <code>res/</code> that include a hyphen and the ISO language code at the end of the directory name. For example, <code>values-es/</code> is the directory containing simple resourcess for the Locales with the language code "es". Android loads the appropriate resources according to the locale settings of the device at run time. For more information, see [http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources Providing Alternative Resource]s.


Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:
Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:</translate>
<syntaxhighlight lang="text">
<syntaxhighlight lang="text">
MyProject/
MyProject/
Zeile 18: Zeile 20:
</syntaxhighlight>
</syntaxhighlight>


Add the string values for each locale into the appropriate file.
<translate>Add the string values for each locale into the appropriate file.


At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.
Zeile 24: Zeile 26:
For example, the following are some different string resource files for different languages.
For example, the following are some different string resource files for different languages.


English (default locale), <code>/values/strings.xml</code>:<syntaxhighlight lang="xml">
English (default locale),</translate> <code>/values/strings.xml</code>:<syntaxhighlight lang="xml">
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<resources>
Zeile 30: Zeile 32:
     <string name="hello_world">Hello World!</string>
     <string name="hello_world">Hello World!</string>
</resources>
</resources>
</syntaxhighlight>Spanish, <code>/values-es/strings.xml</code>:<syntaxhighlight lang="xml">
</syntaxhighlight>
 
<translate>Spanish</translate>, <code>/values-es/strings.xml</code>:<syntaxhighlight lang="xml">
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<resources>
Zeile 36: Zeile 40:
     <string name="hello_world">Hola Mundo!</string>
     <string name="hello_world">Hola Mundo!</string>
</resources>
</resources>
</syntaxhighlight>French, <code>/values-fr/strings.xml</code>:<syntaxhighlight lang="xml">
</syntaxhighlight>
 
<translate>French</translate>, <code>/values-fr/strings.xml</code>:<syntaxhighlight lang="xml">
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<resources>
Zeile 42: Zeile 48:
     <string name="hello_world">Bonjour le monde !</string>
     <string name="hello_world">Bonjour le monde !</string>
</resources>
</resources>
</syntaxhighlight>'''Note:''' You can use the locale qualifier (or any configuration qualifer) on any resource type, such as if you want to provide localized versions of your bitmap drawable. For more information, see [http://developer.android.com/guide/topics/resources/localization.html Localization].
</syntaxhighlight>
 
<translate>'''Note:''' You can use the locale qualifier (or any configuration qualifer) on any resource type, such as if you want to provide localized versions of your bitmap drawable. For more information, see [http://developer.android.com/guide/topics/resources/localization.html Localization].


== Use the String Resources ==
== Use the String Resources ==
Zeile 49: Zeile 57:
In your source code, you can refer to a string resource with the syntax <code>R.string.<string_name></code>. There are a variety of methods that accept a string resource this way.
In your source code, you can refer to a string resource with the syntax <code>R.string.<string_name></code>. There are a variety of methods that accept a string resource this way.


For example:<syntaxhighlight lang="java">
For example:</translate>
<syntaxhighlight lang="java">
// Get a string resource from your app's Resources
// Get a string resource from your app's Resources
String hello = getResources().getString(R.string.hello_world);
String hello = getResources().getString(R.string.hello_world);
Zeile 56: Zeile 65:
TextView textView = new TextView(this);
TextView textView = new TextView(this);
textView.setText(R.string.hello_world);
textView.setText(R.string.hello_world);
</syntaxhighlight>In other XML files, you can refer to a string resource with the syntax <code>@string/<string_name></code> whenever the XML attribute accepts a string value.
</syntaxhighlight>


For example:<syntaxhighlight lang="xml">
<translate>In other XML files, you can refer to a string resource with the syntax <code>@string/<string_name></code> whenever the XML attribute accepts a string value.
 
For example:</translate>
<syntaxhighlight lang="xml">
<TextView
<TextView
     android:layout_width="wrap_content"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/hello_world" />
     android:text="@string/hello_world" />
</syntaxhighlight>{{TNT|Android Training/Attribution}}
</syntaxhighlight>
 
{{TNT|Android Training/Attribution}}
11.008

Bearbeitungen