summaryrefslogtreecommitdiffstats
path: root/docs/html/training/basics/supporting-devices/languages.jd
blob: 739c282e4618e8d24989ba4cb07454d34a23a777 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
page.title=Supporting Different Languages
parent.title=Supporting Different Devices
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}guide/topics/resources/localization.html">Localization</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>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
    &lt;string name="title">My Application&lt;/string>
    &lt;string name="hello_world">Hello World!&lt;/string>
&lt;/resources>
</pre>


<p>Spanish, <code>/values-es/strings.xml</code>:</p>

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


<p>French, <code>/values-fr/strings.xml</code>:</p>

<pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;resources>
    &lt;string name="title">Mon Application&lt;/string>
    &lt;string name="hello_world">Bonjour le monde !&lt;/string>
&lt;/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 &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
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
&#64;string/&lt;string_name>} whenever the XML attribute accepts a string value.</p>

<p>For example:</p>

<pre>
&lt;TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
</pre>