Scott Main | 60e5e43 | 2012-04-02 13:11:23 -0700 | [diff] [blame] | 1 | page.title=Supporting Different Languages |
| 2 | parent.title=Supporting Different Devices |
Joe Fernandez | 33baa5a | 2013-11-14 11:41:19 -0800 | [diff] [blame] | 3 | page.tags=localizing,localization,resources,formats,l10n |
Scott Main | 60e5e43 | 2012-04-02 13:11:23 -0700 | [diff] [blame] | 4 | parent.link=index.html |
| 5 | |
| 6 | trainingnavtop=true |
| 7 | next.title=Supporting Different Screens |
| 8 | next.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> |
Dirk Dougherty | afc0280 | 2013-05-08 18:37:11 -0700 | [diff] [blame] | 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> |
Scott Main | 60e5e43 | 2012-04-02 13:11:23 -0700 | [diff] [blame] | 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 |
| 29 | in an external file. Android makes this easy with a resources directory in each Android |
| 30 | project.</p> |
| 31 | |
| 32 | <p>If you created your project using the Android SDK |
| 33 | Tools (read <a href="{@docRoot}training/basics/firstapp/creating-project.html">Creating an |
| 34 | Android Project</a>), the tools create a <code>res/</code> directory in the top level of |
| 35 | the project. Within this <code>res/</code> directory are subdirectories for various resource |
| 36 | types. There are also a few default files such as <code>res/values/strings.xml</code>, which holds |
| 37 | your 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 |
| 44 | directory name. For example, <code>values-es/</code> is the directory containing simple |
| 45 | resourcess for the Locales with the language code "es". Android loads the appropriate resources |
| 46 | according 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 |
| 49 | string resource files. For example:</p> |
| 50 | |
| 51 | <pre class="classic no-pretty-print"> |
| 52 | MyProject/ |
| 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 |
| 65 | locale 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> |
Scott Main | fd51698 | 2012-07-03 16:25:52 -0700 | [diff] [blame] | 97 | <string name="title">Mon Application</string> |
| 98 | <string name="hello_world">Bonjour le monde !</string> |
Scott Main | 60e5e43 | 2012-04-02 13:11:23 -0700 | [diff] [blame] | 99 | </resources> |
| 100 | </pre> |
| 101 | |
Scott Main | 50e990c | 2012-06-21 17:14:39 -0700 | [diff] [blame] | 102 | <p class="note"><strong>Note:</strong> You can use the locale qualifier (or any |
| 103 | configuration qualifer) on any resource type, such as if you want to provide |
| 104 | localized versions of your bitmap drawable. For more information, see <a |
| 105 | href="{@docRoot}guide/topics/resources/localization.html">Localization</a>. |
Scott Main | 60e5e43 | 2012-04-02 13:11:23 -0700 | [diff] [blame] | 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 |
| 110 | resource 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 |
| 113 | R.string.<string_name>}. There are a variety of methods that accept a string resource this |
| 114 | way.</p> |
| 115 | |
| 116 | <p>For example:</p> |
| 117 | |
| 118 | <pre> |
| 119 | // Get a string resource from your app's {@link android.content.res.Resources} |
| 120 | String 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 |
| 123 | TextView textView = new TextView(this); |
| 124 | textView.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 | @string/<string_name>} 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 | |