1 page.title=Supporting Different Languages 2 parent.title=Supporting Different Devices 3 parent.link=index.html 4 5 trainingnavtop=true 6 next.title=Supporting Different Screens 7 next.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>Its always a good practice to extract UI strings from your app code and keep them 27 in an external file. Android makes this easy with a resources directory in each Android 28 project.</p> 29 30 <p>If you created your project using the Android SDK 31 Tools (read <a href="{@docRoot}training/basics/firstapp/creating-project.html">Creating an 32 Android Project</a>), the tools create a <code>res/</code> directory in the top level of 33 the project. Within this <code>res/</code> directory are subdirectories for various resource 34 types. There are also a few default files such as <code>res/values/strings.xml</code>, which holds 35 your 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 42 directory name. For example, <code>values-es/</code> is the directory containing simple 43 resourcess for the Locales with the language code "es". Android loads the appropriate resources 44 according to the locale settings of the device at run time.</p> 45 46 <p>Once youve decided on the languages you will support, create the resource subdirectories and 47 string resource files. For example:</p> 48 49 <pre class="classic no-pretty-print"> 50 MyProject/ 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 63 locale 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 Aplicacin</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">Mon Application</string> 96 <string name="hello_world">Bonjour le monde !</string> 97 </resources> 98 </pre> 99 100 <p class="note"><strong>Note:</strong> You can use the locale qualifier (or any 101 configuration qualifer) on any resource type, such as if you want to provide 102 localized versions of your bitmap drawable. For more information, see <a 103 href="{@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 108 resource 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 111 R.string.<string_name>}. There are a variety of methods that accept a string resource this 112 way.</p> 113 114 <p>For example:</p> 115 116 <pre> 117 // Get a string resource from your app's {@link android.content.res.Resources} 118 String 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 121 TextView textView = new TextView(this); 122 textView.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