| page.title=Supporting Different Languages |
| parent.title=Supporting Different Devices |
| page.tags="localizing","localization","resources", "formats", "l10n" |
| parent.link=index.html |
| |
| trainingnavtop=true |
| next.title=Supporting Different Screens |
| next.link=screens.html |
| |
| @jd:body |
| |
| |
| <div id="tb-wrapper"> |
| <div id="tb"> |
| <h2>This class teaches you to</h2> |
| <ol> |
| <li><a href="#CreateDirs">Create Locale Directories and String Files</a></li> |
| <li><a href="#UseString">Use the String Resources</a></li> |
| </ol> |
| <h2>You should also read</h2> |
| <ul> |
| <li><a href="{@docRoot}distribute/googleplay/publish/localizing.html">Localization Checklist</a></li> |
| <li><a href="{@docRoot}guide/topics/resources/localization.html">Localization with Resources</a></li> |
| </ul> |
| </div> |
| </div> |
| |
| <p>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.</p> |
| |
| <p>If you created your project using the Android SDK |
| Tools (read <a href="{@docRoot}training/basics/firstapp/creating-project.html">Creating an |
| Android Project</a>), 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.</p> |
| |
| |
| <h2 id="CreateDirs">Create Locale Directories and String Files</h2> |
| |
| <p>To add support for more languages, create additional <code>values</code> directories inside |
| <code>res/</code> that include a hyphen and the ISO country 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.</p> |
| |
| <p>Once you’ve decided on the languages you will support, create the resource subdirectories and |
| string resource files. For example:</p> |
| |
| <pre class="classic no-pretty-print"> |
| MyProject/ |
| res/ |
| values/ |
| strings.xml |
| values-es/ |
| strings.xml |
| values-fr/ |
| strings.xml |
| </pre> |
| |
| <p>Add the string values for each locale into the appropriate file.</p> |
| |
| <p>At runtime, the Android system uses the appropriate set of string resources based on the |
| locale currently set for the user's device.</p> |
| |
| <p>For example, the following are some different string resource files for different languages.</p> |
| |
| |
| <p>English (default locale), <code>/values/strings.xml</code>:</p> |
| |
| <pre> |
| <?xml version="1.0" encoding="utf-8"?> |
| <resources> |
| <string name="title">My Application</string> |
| <string name="hello_world">Hello World!</string> |
| </resources> |
| </pre> |
| |
| |
| <p>Spanish, <code>/values-es/strings.xml</code>:</p> |
| |
| <pre> |
| <?xml version="1.0" encoding="utf-8"?> |
| <resources> |
| <string name="title">Mi Aplicación</string> |
| <string name="hello_world">Hola Mundo!</string> |
| </resources> |
| </pre> |
| |
| |
| <p>French, <code>/values-fr/strings.xml</code>:</p> |
| |
| <pre> |
| <?xml version="1.0" encoding="utf-8"?> |
| <resources> |
| <string name="title">Mon Application</string> |
| <string name="hello_world">Bonjour le monde !</string> |
| </resources> |
| </pre> |
| |
| <p class="note"><strong>Note:</strong> 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 <a |
| href="{@docRoot}guide/topics/resources/localization.html">Localization</a>. |
| |
| <h2 id="UseString">Use the String Resources</h2> |
| |
| <p>You can reference your string resources in your source code and other XML files using the |
| resource name defined by the {@code <string>} element's {@code name} attribute.</p> |
| |
| <p>In your source code, you can refer to a string resource with the syntax {@code |
| R.string.<string_name>}. There are a variety of methods that accept a string resource this |
| way.</p> |
| |
| <p>For example:</p> |
| |
| <pre> |
| // Get a string resource from your app's {@link android.content.res.Resources} |
| String hello = {@link android.content.Context#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); |
| </pre> |
| |
| <p>In other XML files, you can refer to a string resource with the syntax {@code |
| @string/<string_name>} whenever the XML attribute accepts a string value.</p> |
| |
| <p>For example:</p> |
| |
| <pre> |
| <TextView |
| android:layout_width="wrap_content" |
| android:layout_height="wrap_content" |
| android:text="@string/hello_world" /> |
| </pre> |
| |
| |
| |