Zum Inhalt springen

Android Training/Supporting Different Languages: Unterschied zwischen den Versionen

Diese Seite wurde zum Übersetzen freigegeben
(Vorbereitung für Übersetzung)
(Diese Seite wurde zum Übersetzen freigegeben)
Zeile 1: Zeile 1:
<languages />
<languages />
<translate>
<translate>
<!--T:1-->
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.


<!--T:2-->
If you created your project using the Android SDK Tools (readCreating an Android Project), the tools create a <code>res/</code> directory in the top level of the project. Within this <code>res/</code> directory are subdirectories for various resource types. There are also a few default files such as <code>res/values/strings.xml</code>, which holds your string values.
If you created your project using the Android SDK Tools (readCreating an Android Project), the tools create a <code>res/</code> directory in the top level of the project. Within this <code>res/</code> directory are subdirectories for various resource types. There are also a few default files such as <code>res/values/strings.xml</code>, which holds your string values.


== Create Locale Directories and String Files ==
== Create Locale Directories and String Files == <!--T:3-->
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.


<!--T:4-->
Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:</translate>
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">
Zeile 20: Zeile 23:
</syntaxhighlight>
</syntaxhighlight>


<translate>Add the string values for each locale into the appropriate file.
<translate><!--T:5-->
Add the string values for each locale into the appropriate file.


<!--T:6-->
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.


<!--T:7-->
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.


<!--T:8-->
English (default locale),</translate> <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"?>
Zeile 34: Zeile 41:
</syntaxhighlight>
</syntaxhighlight>


<translate>Spanish</translate>, <code>/values-es/strings.xml</code>:<syntaxhighlight lang="xml">
<translate><!--T:9-->
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 42: Zeile 50:
</syntaxhighlight>
</syntaxhighlight>


<translate>French</translate>, <code>/values-fr/strings.xml</code>:<syntaxhighlight lang="xml">
<translate><!--T:10-->
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 50: Zeile 59:
</syntaxhighlight>
</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].
<translate><!--T:11-->
'''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 == <!--T:12-->
You can reference your string resources in your source code and other XML files using the resource name defined by the <code><string></code> element's <code>name</code> attribute.
You can reference your string resources in your source code and other XML files using the resource name defined by the <code><string></code> element's <code>name</code> attribute.


<!--T:13-->
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.


<!--T:14-->
For example:</translate>
For example:</translate>
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
Zeile 67: Zeile 79:
</syntaxhighlight>
</syntaxhighlight>


<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.
<translate><!--T:15-->
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.


<!--T:16-->
For example:</translate>
For example:</translate>
<syntaxhighlight lang="xml">
<syntaxhighlight lang="xml">
11.008

Bearbeitungen

Cookies helfen uns bei der Bereitstellung von Android Wiki. Durch die Nutzung von Android Wiki erklärst du dich damit einverstanden, dass wir Cookies speichern.