blob: 098b556314da172a9a4910c03f82e7c5a3f3a517 [file] [log] [blame]
page.title=Supporting Different Languages
parent.title=Supporting Different Devices
<div id="tb-wrapper">
<div id="tb">
<h2>This class teaches you to</h2>
<li><a href="#CreateDirs">Create Locale Directories and String Files</a></li>
<li><a href="#UseString">Use the String Resources</a></li>
<h2>You should also read</h2>
<li><a href="{@docRoot}distribute/tools/localization-checklist.html">Localization Checklist</a></li>
<li><a href="{@docRoot}guide/topics/resources/localization.html">Localization with Resources</a></li>
<p>Its 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
<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 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
<a href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">Providing Alternative Resources</a>.</p>
<p>Once youve decided on the languages you will support, create the resource subdirectories and
string resource files. For example:</p>
<pre class="classic no-pretty-print">
<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>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;string name="title">My Application&lt;/string>
&lt;string name="hello_world">Hello World!&lt;/string>
<p>Spanish, <code>/values-es/strings.xml</code>:</p>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;string name="title">Mi Aplicación&lt;/string>
&lt;string name="hello_world">Hola Mundo!&lt;/string>
<p>French, <code>/values-fr/strings.xml</code>:</p>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;string name="title">Mon Application&lt;/string>
&lt;string name="hello_world">Bonjour le monde !&lt;/string>
<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
<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 &lt;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.&lt;string_name>}. There are a variety of methods that accept a string resource this
<p>For example:</p>
// 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);
<p>In other XML files, you can refer to a string resource with the syntax {@code
&#64;string/&lt;string_name>} whenever the XML attribute accepts a string value.</p>
<p>For example:</p>
android:text="@string/hello_world" />