1page.title=Supporting Different Languages 2parent.title=Supporting Different Devices 3page.tags=strings,localizing,localization,resources,formats,l10n 4helpoutsWidget=true 5 6trainingnavtop=true 7 8@jd:body 9 10 11<div id="tb-wrapper"> 12 <div id="tb"> 13 <h2>This class teaches you to</h2> 14 <ol> 15 <li><a href="#CreateDirs">Create Locale Directories and String Files</a></li> 16 <li><a href="#UseString">Use the String Resources</a></li> 17 </ol> 18 <h2>You should also read</h2> 19 <ul> 20 <li><a href="{@docRoot}distribute/tools/localization-checklist.html">Localization Checklist</a></li> 21 <li><a href="{@docRoot}guide/topics/resources/localization.html">Localization with Resources</a></li> 22 <li><a href="https://support.google.com/l10n/answer/6359997">App Translation Service</a></li> 23 </ul> 24 </div> 25</div> 26 27<p>It’s always a good practice to extract UI strings from your app code and keep them 28in an external file. Android makes this easy with a resources directory in each Android 29project.</p> 30 31<p>If you created your project using the Android SDK 32Tools (read <a href="{@docRoot}training/basics/firstapp/creating-project.html">Creating an 33Android Project</a>), the tools create a <code>res/</code> directory in the top level of 34the project. Within this <code>res/</code> directory are subdirectories for various resource 35types. There are also a few default files such as <code>res/values/strings.xml</code>, which holds 36your string values.</p> 37 38 39<h2 id="CreateDirs">Create Locale Directories and String Files</h2> 40 41<p>To add support for more languages, create additional <code>values</code> directories inside 42<code>res/</code> that include a hyphen and the ISO language code at the end of the 43directory name. For example, <code>values-es/</code> is the directory containing simple 44resources for the Locales with the language code "es". Android loads the appropriate resources 45according to the locale settings of the device at run time. For more information, see 46 <a href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">Providing Alternative Resources</a>.</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<?xml version="1.0" encoding="utf-8"?> 74<resources> 75 <string name="title">My Application</string> 76 <string name="hello_world">Hello World!</string> 77</resources> 78</pre> 79 80 81<p>Spanish, <code>/values-es/strings.xml</code>:</p> 82 83<pre> 84<?xml version="1.0" encoding="utf-8"?> 85<resources> 86 <string name="title">Mi Aplicación</string> 87 <string name="hello_world">Hola Mundo!</string> 88</resources> 89</pre> 90 91 92<p>French, <code>/values-fr/strings.xml</code>:</p> 93 94<pre> 95<?xml version="1.0" encoding="utf-8"?> 96<resources> 97 <string name="title">Mon Application</string> 98 <string name="hello_world">Bonjour le monde !</string> 99</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 <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.<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 128<code>@string/<string_name></code> whenever the XML attribute accepts a string value.</p> 129 130<p>For example:</p> 131 132<pre> 133<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