• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=Supporting Different Languages
2parent.title=Supporting Different Devices
3page.tags="localizing","localization","resources", "formats", "l10n"
4parent.link=index.html
5
6trainingnavtop=true
7next.title=Supporting Different Screens
8next.link=screens.html
9
10@jd:body
11
12
13<div id="tb-wrapper">
14  <div id="tb">
15    <h2>This class teaches you to</h2>
16    <ol>
17      <li><a href="#CreateDirs">Create Locale Directories and String Files</a></li>
18      <li><a href="#UseString">Use the String Resources</a></li>
19    </ol>
20    <h2>You should also read</h2>
21    <ul>
22    <li><a href="{@docRoot}distribute/googleplay/publish/localizing.html">Localization Checklist</a></li>
23      <li><a href="{@docRoot}guide/topics/resources/localization.html">Localization with Resources</a></li>
24    </ul>
25  </div>
26</div>
27
28<p>It’s always a good practice to extract UI strings from your app code and keep them
29in an external file.  Android makes this easy with a resources directory in each Android
30project.</p>
31
32<p>If you created your project using the Android SDK
33Tools (read <a href="{@docRoot}training/basics/firstapp/creating-project.html">Creating an
34Android Project</a>), the tools create a <code>res/</code> directory in the top level of
35the project. Within this <code>res/</code> directory are subdirectories for various resource
36types. There are also a few default files such as <code>res/values/strings.xml</code>, which holds
37your string values.</p>
38
39
40<h2 id="CreateDirs">Create Locale Directories and String Files</h2>
41
42<p>To add support for more languages, create additional <code>values</code> directories inside
43<code>res/</code> that include a hyphen and the ISO country code at the end of the
44directory name. For example, <code>values-es/</code> is the directory containing simple
45resourcess for the Locales with the language code "es".  Android loads the appropriate resources
46according to the locale settings of the device at run time.</p>
47
48<p>Once you’ve decided on the languages you will support, create the resource subdirectories and
49string resource files. For example:</p>
50
51<pre class="classic no-pretty-print">
52MyProject/
53    res/
54       values/
55           strings.xml
56       values-es/
57           strings.xml
58       values-fr/
59           strings.xml
60</pre>
61
62<p>Add the string values for each locale into the appropriate file.</p>
63
64<p>At runtime, the Android system uses the appropriate set of string resources based on the
65locale currently set for the user's device.</p>
66
67<p>For example, the following are some different string resource files for different languages.</p>
68
69
70<p>English (default locale), <code>/values/strings.xml</code>:</p>
71
72<pre>
73&lt;?xml version="1.0" encoding="utf-8"?>
74&lt;resources>
75    &lt;string name="title">My Application&lt;/string>
76    &lt;string name="hello_world">Hello World!&lt;/string>
77&lt;/resources>
78</pre>
79
80
81<p>Spanish, <code>/values-es/strings.xml</code>:</p>
82
83<pre>
84&lt;?xml version="1.0" encoding="utf-8"?>
85&lt;resources>
86    &lt;string name="title">Mi Aplicación&lt;/string>
87    &lt;string name="hello_world">Hola Mundo!&lt;/string>
88&lt;/resources>
89</pre>
90
91
92<p>French, <code>/values-fr/strings.xml</code>:</p>
93
94<pre>
95&lt;?xml version="1.0" encoding="utf-8"?>
96&lt;resources>
97    &lt;string name="title">Mon Application&lt;/string>
98    &lt;string name="hello_world">Bonjour le monde !&lt;/string>
99&lt;/resources>
100</pre>
101
102<p class="note"><strong>Note:</strong> You can use the locale qualifier (or any
103configuration qualifer) on any resource type, such as if you want to provide
104localized versions of your bitmap drawable. For more information, see <a
105href="{@docRoot}guide/topics/resources/localization.html">Localization</a>.
106
107<h2 id="UseString">Use the String Resources</h2>
108
109<p>You can reference your string resources in your source code and other XML files using the
110resource name defined by the {@code &lt;string>} element's {@code name} attribute.</p>
111
112<p>In your source code, you can refer to a string resource with the syntax {@code
113R.string.&lt;string_name>}. There are a variety of methods that accept a string resource this
114way.</p>
115
116<p>For example:</p>
117
118<pre>
119// Get a string resource from your app's {@link android.content.res.Resources}
120String hello = {@link android.content.Context#getResources()}.getString(R.string.hello_world);
121
122// Or supply a string resource to a method that requires a string
123TextView textView = new TextView(this);
124textView.setText(R.string.hello_world);
125</pre>
126
127<p>In other XML files, you can refer to a string resource with the syntax {@code
128&#64;string/&lt;string_name>} whenever the XML attribute accepts a string value.</p>
129
130<p>For example:</p>
131
132<pre>
133&lt;TextView
134    android:layout_width="wrap_content"
135    android:layout_height="wrap_content"
136    android:text="@string/hello_world" />
137</pre>
138
139
140
141