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