Android Training/Supporting Different Languages/de: Unterschied zwischen den Versionen

Aus Android Wiki
(Die Seite wurde neu angelegt: „== Locale-Verzeichnis und Zeichenketten-Dateien erstellen == Um mehr Sprachen zu unterstützen, musst du zusätzliche <code>values</code> Ordner unter <code>re…“)
(Übernehme Bearbeitung einer neuen Version der Quellseite)
Zeile 4: Zeile 4:
Wenn du ein Projekt mit den Android SDK Tools erstellt hast (lese [[Special:MyLanguage/Android_Training/Creating_an_Android_Project|Erstellen eines Android Projektes]]), haben die Tools ein <code>res/</code> Ordner im Stammverzeichnis des Projektes erstellt. Innerhalb dieses <code>res/</code> Ordner gibt es verschiedene Unterordner für die verschiedenen Ressourcen-Typen. Außerdem liegen dort einige Standard-Dateien, wie zum Beispiel <code>res/values/strings.xml</code>, welche deine Zeichenketten beinhalten.
Wenn du ein Projekt mit den Android SDK Tools erstellt hast (lese [[Special:MyLanguage/Android_Training/Creating_an_Android_Project|Erstellen eines Android Projektes]]), haben die Tools ein <code>res/</code> Ordner im Stammverzeichnis des Projektes erstellt. Innerhalb dieses <code>res/</code> Ordner gibt es verschiedene Unterordner für die verschiedenen Ressourcen-Typen. Außerdem liegen dort einige Standard-Dateien, wie zum Beispiel <code>res/values/strings.xml</code>, welche deine Zeichenketten beinhalten.


== Locale-Verzeichnis und Zeichenketten-Dateien erstellen ==
== Create Locale Directories and String Files ==
Um mehr Sprachen zu unterstützen, musst du zusätzliche <code>values</code> Ordner unter <code>res/</code> erstellen, welche einen Bindestrich gefolgt vom ISO Sprach-Code am Ende des Ordner-Namens haben. Als Beispiel: <code>values-es/</code> ist der Ordner, welcher einfache Ressourcen für die Lokalisierung für den Sprachcode "es" beinhaltet. Android lädt die korrekten Ressourcen während der Laufzeit entsprechend der Sprach-Einstellungen des Gerätes. Für mehr Informationen, siehe [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 Resources].


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:

Version vom 24. Dezember 2015, 19:20 Uhr

Sprachen:

Es ist immer eine gute Praxis, Zeichenketten der Benutzeroberfläche vom eigentlichen App-Code zu trennen und in einer externen Datei zu belassen. Android macht die Umsetzung einfach, durch ein ressources-Ordner in jedem Android Projekt.

Wenn du ein Projekt mit den Android SDK Tools erstellt hast (lese Erstellen eines Android Projektes), haben die Tools ein res/ Ordner im Stammverzeichnis des Projektes erstellt. Innerhalb dieses res/ Ordner gibt es verschiedene Unterordner für die verschiedenen Ressourcen-Typen. Außerdem liegen dort einige Standard-Dateien, wie zum Beispiel res/values/strings.xml, welche deine Zeichenketten beinhalten.

Create Locale Directories and String Files

To add support for more languages, create additional values directories inside res/ that include a hyphen and the ISO language code at the end of the directory name. For example, values-es/ 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 Providing Alternative Resources.

Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:

MyProject/
    res/
        values/
            strings.xml
        values-es/
            strings.xml
        values-fr/
            strings.xml

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.

For example, the following are some different string resource files for different languages.

English (default locale), /values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">My Application</string>
    <string name="hello_world">Hello World!</string>
</resources>

Spanish, /values-es/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Mi Aplicación</string>
    <string name="hello_world">Hola Mundo!</string>
</resources>

French, /values-fr/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Mon Application</string>
    <string name="hello_world">Bonjour le monde !</string>
</resources>

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 Localization.

Use the String Resources

You can reference your string resources in your source code and other XML files using the resource name defined by the <string> element's name attribute.

In your source code, you can refer to a string resource with the syntax R.string.<string_name>. There are a variety of methods that accept a string resource this way.

For example:

// Get a string resource from your app's Resources
String hello = getResources().getString(R.string.hello_world);

// Or supply a string resource to a method that requires a string
TextView textView = new TextView(this);
textView.setText(R.string.hello_world);

In other XML files, you can refer to a string resource with the syntax @string/<string_name> whenever the XML attribute accepts a string value.

For example:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />