1 page.title=Localizing with Resources 2 parent.title=Application Resources 3 page.tags="localizing","localization","resources", "formats", "l10n" 4 parent.link=index.html 5 @jd:body 6 7 <div id="qv-wrapper"> 8 <div id="qv"> 9 10 <h2>Quickview</h2> 11 12 <ul> 13 <li>Use resource sets to create a localized app.</li> 14 <li>Android loads the correct resource set for the user's language and locale.</li> 15 <li>If localized resources are not available, Android loads your default resources.</li> 16 </ul> 17 18 <h2>In this document</h2> 19 <ol> 20 <li><a href="#resource-switching">Overview: Resource-Switching in Android</a></li> 21 <li><a href="#using-framework">Using Resources for Localization</a></li> 22 <li><a href="#strategies">Localization Tips</a></li> 23 <li><a href="#testing">Testing Localized Applications</a></li> 24 </ol> 25 26 <h2>See also</h2> 27 <ol> 28 <li><a href="{@docRoot}distribute/tools/localization-checklist.html"> 29 Localization Checklist</a></li> 30 <li><a href="{@docRoot}guide/topics/resources/providing-resources.html"> 31 Providing Resources</a></li> 32 <li><a href="{@docRoot}guide/topics/ui/declaring-layout.html"> 33 Layouts</a></li> 34 <li><a href="{@docRoot}reference/android/app/Activity.html#ActivityLifecycle"> 35 Activity Lifecycle</a></li> 36 <li><a href="https://support.google.com/l10n/answer/6359997">App Translation Service</a></li> 37 </ol> 38 </div> 39 </div> 40 41 <p>Android will run on many devices in many regions. To reach the most users, 42 your application should handle text, audio files, numbers, currency, and 43 graphics in ways appropriate to the locales where your application will be used. 44 </p> 45 46 <p>This document describes best practices for localizing Android 47 applications.</p> 48 49 <p>You should already have a working knowledge of Java and be familiar with 50 Android resource loading, the declaration of user interface elements in XML, 51 development considerations such as Activity lifecycle, and general principles of 52 internationalization and localization. </p> 53 54 <p>It is good practice to use the Android resource framework to separate the 55 localized aspects of your application as much as possible from the core Java 56 functionality:</p> 57 58 <ul> 59 <li>You can put most or all of the <em>contents</em> of your application's 60 user interface into resource files, as described in this document and in <a 61 href="{@docRoot}guide/topics/resources/providing-resources.html"> 62 Providing Resources</a>.</li> 63 <li>The <em>behavior</em> of the user interface, on the other hand, is driven 64 by your Java code. 65 For example, if users input data that needs to be formatted or sorted 66 differently depending on locale, then you would use Java to handle the data 67 programmatically. This document does not cover how to localize your Java code. 68 </li> 69 </ul> 70 71 <p>For a short guide to localizing strings in your app, see the training lesson, 72 <a href="{@docRoot}training/basics/supporting-devices/languages.html"> 73 Supporting Different Languages</a>. </p> 74 75 76 <h2 id="resource-switching">Overview: Resource-Switching in Android</h2> 77 78 <p>Resources are text strings, layouts, sounds, graphics, and any other static 79 data that your Android application needs. An application can include multiple 80 sets of resources, each customized for a different device configuration. When a 81 user runs the application, Android automatically selects and loads the 82 resources that best match the device.</p> 83 84 <p>(This document focuses on localization and locale. For a complete description 85 of resource-switching and all the types of configurations that you can 86 specify — screen orientation, touchscreen type, and so on — 87 see <a href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources"> 88 Providing Alternative Resources</a>.)</p> 89 90 <table border="0" cellspacing="0" cellpadding="0"> 91 <tr border="0"> 92 <td width="180" style="border: 0pt none ;"><p class="special-note"> 93 <strong>When you write your application:</strong> 94 <br><br> 95 You create a set of default resources, plus alternatives to be used in 96 different locales.</p></td> 97 <td style="border: 0pt none; padding:0"> 98 <p style="border:0; padding:0"> 99 <img src="../../../images/resources/right-arrow.png" alt="right-arrow" 100 width="51" height="17"></p></td> 101 <td width="180" style="border: 0pt none ;"><p class="special-note"> 102 <strong>When a user runs your application:</strong> 103 <br><br>The Android system selects which resources to load, based on the 104 device's locale.</p></td> 105 </tr> 106 </table> 107 108 <p>When you write your application, you create default and alternative resources 109 for your application to use. To create resources, you place files within 110 specially named subdirectories of the project's <code>res/</code> directory. 111 </p> 112 113 114 115 <h3 id="defaults-r-important">Why Default Resources Are Important</h3> 116 117 <p>Whenever the application runs in a locale for which you have not provided 118 locale-specific text, Android will load the default strings from 119 <code>res/values/strings.xml</code>. If this default file is absent, or if it 120 is missing a string that your application needs, then your application will not run 121 and will show an error. 122 The example below illustrates what can happen when the default text file is 123 incomplete. </p> 124 125 <p><em>Example:</em> 126 <p>An application's Java code refers to just two strings, <code>text_a</code> and 127 <code>text_b</code>. This application includes a localized resource file 128 (<code>res/values-en/strings.xml</code>) that defines <code>text_a</code> and 129 <code>text_b</code> in English. This application also includes a default 130 resource file (<code>res/values/strings.xml</code>) that includes a 131 definition for <code>text_a</code>, but not for <code>text_b</code>: 132 <ul> 133 <li>When this application is launched on a device with locale set to English, 134 the application might run without a problem, because 135 <code>res/values-en/strings.xml</code> contains both of the needed text 136 strings.</li> 137 <li>However, <strong>the user will see an error message and a Force Close 138 button</strong> when this application is launched on a device set to a 139 language other than English. The application will not load.</li> 140 </ul> 141 142 143 <p>To prevent this situation, make sure that a <code>res/values/strings.xml</code> 144 file exists and that it defines every needed string. The situation applies to 145 all types of resources, not just strings: You 146 need to create a set of default resource files containing all 147 the resources that your application calls upon — layouts, drawables, 148 animations, etc. For information about testing, see <a href="#test-for-default"> 149 Testing for Default Resources</a>.</p> 150 151 <h2 id="using-framework">Using Resources for Localization</h2> 152 153 <h3 id="creating-defaults">How to Create Default Resources</h3> 154 155 <p>Put the application's default text in 156 a file with the following location and name:</p> 157 <p><code> res/values/strings.xml</code> (required directory)</p> 158 159 <p>The text strings in <code>res/values/strings.xml</code> should use the 160 default language, which is the language that you expect most of your application's users to 161 speak. </p> 162 163 <p>The default resource set must also include any default drawables and layouts, 164 and can include other types of resources such as animations. 165 <br> 166 <code> res/drawable/</code>(required directory holding at least 167 one graphic file, for the application's icon on Google Play)<br> 168 <code> res/layout/</code> (required directory holding an XML 169 file that defines the default layout)<br> 170 <code> res/anim/</code> (required if you have any 171 <code>res/anim-<em><qualifiers></em></code> folders)<br> 172 <code> res/xml/</code> (required if you have any 173 <code>res/xml-<em><qualifiers></em></code> folders)<br> 174 <code> res/raw/</code> (required if you have any 175 <code>res/raw-<em><qualifiers></em></code> folders) 176 </p> 177 178 <p class="note"><strong>Tip:</strong> In your code, examine each reference to 179 an Android resource. Make sure that a default resource is defined for each 180 one. Also make sure that the default string file is complete: A <em> 181 localized</em> string file can contain a subset of the strings, but the 182 <em>default</em> string file must contain them all. 183 </p> 184 185 <h3 id="creating-alternatives">How to Create Alternative Resources</h3> 186 187 <p>A large part of localizing an application is providing alternative text for 188 different languages. In some cases you will also provide alternative graphics, 189 sounds, layouts, and other locale-specific resources. </p> 190 191 <p>An application can specify many <code>res/<em><qualifiers></em>/</code> 192 directories, each with different qualifiers. To create an alternative resource for 193 a different locale, you use a qualifier that specifies a language or a 194 language-region combination. (The name of a resource directory must conform 195 to the naming scheme described in 196 <a href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">Providing 197 Alternative Resources</a>, 198 or else it will not compile.)</p> 199 200 <p><em>Example:</em></p> 201 202 <p>Suppose that your application's default language is English. Suppose also 203 that you want to localize all the text in your application to French, and most 204 of the text in your application (everything except the application's title) to 205 Japanese. In this case, you could create three alternative <code>strings.xml</code> 206 files, each stored in a locale-specific resource directory:</p> 207 208 <ol> 209 <li><code>res/values/strings.xml</code><br> 210 Contains English text for all the strings that the application uses, 211 including text for a string named <code>title</code>.</li> 212 <li><code>res/values-fr/strings.xml</code><br> 213 Contain French text for all the strings, including <code>title</code>.</li> 214 <li><code>res/values-ja/strings.xml</code><br> 215 Contain Japanese text for all the strings <em>except</em> 216 <code>title</code>.<br> 217 <code></code></li> 218 </ol> 219 220 <p>If your Java code refers to <code>R.string.title</code>, here is what will 221 happen at runtime:</p> 222 223 <ul> 224 <li>If the device is set to any language other than French, Android will load 225 <code>title</code> from the <code>res/values/strings.xml</code> file.</li> 226 <li>If the device is set to French, Android will load <code>title</code> from 227 the <code>res/values-fr/strings.xml</code> file.</li> 228 </ul> 229 230 <p>Notice that if the device is set to Japanese, Android will look for 231 <code>title</code> in the <code>res/values-ja/strings.xml</code> file. But 232 because no such string is included in that file, Android will fall back to the 233 default, and will load <code>title</code> in English from the 234 <code>res/values/strings.xml</code> file. </p> 235 236 <h3 id="resource-precedence">Which Resources Take Precedence?</h3> 237 238 <p> If multiple resource files match a device's configuration, Android follows a 239 set of rules in deciding which file to use. Among the qualifiers that can be 240 specified in a resource directory name, <strong>locale almost always takes 241 precedence</strong>. </p> 242 <p><em>Example:</em></p> 243 244 <p>Assume that an application includes a default set of graphics and two other 245 sets of graphics, each optimized for a different device setup:</p> 246 247 <ul> 248 <li><code>res/drawable/</code><br> 249 Contains 250 default graphics.</li> 251 <li><code>res/drawable-small-land-stylus/</code><br> 252 Contains graphics optimized for use with a device that expects input from a 253 stylus and has a QVGA low-density screen in landscape orientation.</li> 254 <li><code>res/drawable-ja/</code> <br> 255 Contains graphics optimized for use with Japanese.</li> 256 </ul> 257 258 <p>If the application runs on a device that is configured to use Japanese, 259 Android will load graphics from <code>res/drawable-ja/</code>, even if the 260 device happens to be one that expects input from a stylus and has a QVGA 261 low-density screen in landscape orientation.</p> 262 263 <p class="note"><strong>Exception:</strong> The only qualifiers that take 264 precedence over locale in the selection process are MCC and MNC (mobile country 265 code and mobile network code). </p> 266 267 <p><em>Example:</em></p> 268 269 <p>Assume that you have the following situation:</p> 270 271 <ul> 272 <li>The application code calls for <code>R.string.text_a</code></li> 273 <li>Two relevant resource files are available: 274 <ul> 275 <li><code>res/values-mcc404/strings.xml</code>, which includes 276 <code>text_a</code> in the application's default language, in this case 277 English.</li> 278 <li><code>res/values-hi/strings.xml</code>, which includes 279 <code>text_a</code> in Hindi.</li> 280 </ul> 281 </li> 282 <li>The application is running on a device that has the following 283 configuration: 284 <ul> 285 <li>The SIM card is connected to a mobile network in India (MCC 404).</li> 286 <li>The language is set to Hindi (<code>hi</code>).</li> 287 </ul> 288 </li> 289 </ul> 290 291 <p>Android will load <code>text_a</code> from 292 <code>res/values-mcc404/strings.xml</code> (in English), even if the device is 293 configured for Hindi. That is because in the resource-selection process, Android 294 will prefer an MCC match over a language match. </p> 295 296 <p>The selection process is not always as straightforward as these examples 297 suggest. Please read <a 298 href="{@docRoot}guide/topics/resources/providing-resources.html#BestMatch">How Android Finds 299 the Best-matching Resource</a> for a more nuanced description of the 300 process. All the qualifiers are described and listed in order of 301 precedence in <a 302 href="{@docRoot}guide/topics/resources/providing-resources.html#table2">Table 2 of Providing 303 Alternative Resources</a>.</p> 304 305 <h3 id="referring-to-resources">Referring to Resources in Java</h3> 306 307 <p>In your application's Java code, you refer to resources using the syntax 308 <code>R.<em>resource_type</em>.<em>resource_name</em></code> or 309 <code>android.R.<em>resource_type</em>.<em>resource_name</em></code><em>.</em> 310 For more about this, see <a 311 href="{@docRoot}guide/topics/resources/accessing-resources.html">Accessing Resources</a>.</p> 312 313 <h2 id="checklist">Localization Checklist</h2> 314 315 <p>For a complete overview of the process of localizing and distributing an Android application, 316 see the <a href="{@docRoot}distribute/tools/localization-checklist.html">Localization 317 Checklist</a> document.</p> 318 319 <h2 id="strategies">Localization Tips</h2> 320 321 <h4 id="failing2">Design your application to work in any locale</h4> 322 323 <p>You cannot assume anything about the device on which a user will 324 run your application. The device might have hardware that you were not 325 anticipating, or it might be set to a locale that you did not plan for or that 326 you cannot test. Design your application so that it will function normally or fail gracefully no 327 matter what device it runs on.</p> 328 329 <p class="note"><strong>Important:</strong> Make sure that your application 330 includes a full set of default resources.</p> <p>Make sure to include 331 <code>res/drawable/</code> and a <code>res/values/</code> folders (without any 332 additional modifiers in the folder names) that contain all the images and text 333 that your application will need. </p> 334 335 <p>If an application is missing even one default resource, it will not run on a 336 device that is set to an unsupported locale. For example, the 337 <code>res/values/strings.xml</code> default file might lack one string that 338 the application needs: When the application runs in an unsupported locale and 339 attempts to load <code>res/values/strings.xml</code>, the user will see an 340 error message and a Force Close button.</p> 341 342 <p>For more information, see <a href="#test-for-default">Testing for Default Resources</a>.</p> 343 344 <h4>Design a flexible layout</h4> 345 346 <p> If you need to rearrange your layout to fit a certain language (for example 347 German with its long words), you can create an alternative layout for that 348 language (for example <code>res/layout-de/main.xml</code>). However, doing this 349 can make your application harder to maintain. It is better to create a single 350 layout that is more flexible.</p> 351 352 <p>Another typical situation is a language that requires something different in 353 its layout. For example, you might have a contact form that should include two 354 name fields when the application runs in Japanese, but three name fields when 355 the application runs in some other language. You could handle this in either of 356 two ways:</p> 357 358 <ul> 359 <li>Create one layout with a field that you can programmatically enable or 360 disable, based on the language, or</li> 361 <li>Have the main layout include another layout that includes the changeable 362 field. The second layout can have different configurations for different 363 languages.</li> 364 </ul> 365 366 <h4>Avoid creating more resource files and text strings than you need</h4> 367 368 <p>You probably do not need to create a locale-specific 369 alternative for every resource in your application. For example, the layout 370 defined in the <code>res/layout/main.xml</code> file might work in any locale, 371 in which case there would be no need to create any alternative layout files. 372 </p> 373 374 <p>Also, you might not need to create alternative text for every 375 string. For example, assume the following:</p> 376 377 <ul> 378 <li>Your application's default language is American 379 English. Every string that the application uses is defined, using American 380 English spellings, in <code>res/values/strings.xml</code>. </li> 381 382 <li>For a few important phrases, you want to provide 383 British English spelling. You want these alternative strings to be used when your 384 application runs on a device in the United Kingdom. </li> 385 </ul> 386 387 <p>To do this, you could create a small file called 388 <code>res/values-en-rGB/strings.xml</code> that includes only the strings that 389 should be different when the application runs in the U.K. For all the rest of 390 the strings, the application will fall back to the defaults and use what is 391 defined in <code>res/values/strings.xml</code>.</p> 392 393 <h4>Use the Android Context object for manual locale lookup</h4> 394 395 <p>You can look up the locale using the {@link android.content.Context} object 396 that Android makes available:</p> 397 398 <pre>String locale = context.getResources().getConfiguration().locale.getDisplayName();</pre> 399 <h4> 400 Use the App Translation Service 401 </h4> 402 403 <p> 404 The <a href="https://support.google.com/l10n/answer/6359997">App Translation 405 Service</a> is integrated into the <a href= 406 "https://support.google.com/l10n/answer/6341304">Developer Console</a>, and 407 it is also accessible from <a href= 408 "https://support.google.com/l10n/answer/6341928">Android Studio</a>. It is a 409 quick and simple way to get an instant quote and place an order with a 410 translation company. You can order translations into one or more languages 411 for app UI strings, Play Store Listing text, IAP names, and ad campaign text. 412 </p> 413 414 <h2 id="testing">Testing Localized Applications</h2> 415 416 <h3 id="device">Testing on a Device</h3> 417 <p>Keep in mind that the device you are testing may be significantly different from 418 the devices available to consumers in other geographies. The locales available 419 on your device may differ from those available on other devices. Also, the 420 resolution and density of the device screen may differ, which could affect 421 the display of strings and drawables in your UI.</p> 422 423 <p>To change the locale or language on a device, use the Settings application.</p> 424 425 <h3 id="emulator">Testing on an Emulator</h3> 426 427 <p>For details about using the emulator, see See <a 428 href="{@docRoot}tools/help/emulator.html">Android Emulator</a>.</p> 429 <h4>Creating and using a custom locale</h4> 430 431 <p>A "custom" locale is a language/region combination that the Android 432 system image does not explicitly support. You can test 433 how your application will run in a custom locale by creating a custom locale in 434 the emulator. There are two ways to do this:</p> 435 436 <ul> 437 <li>Use the Custom Locale application, which is accessible from the 438 Application tab. (After you create a custom locale, switch to it by 439 pressing and holding the locale name.)</li> 440 <li>Change to a custom locale from the adb shell, as described below.</li> 441 </ul> 442 443 <p>When you set the emulator to a locale that is not available in the Android 444 system image, the system itself will display in its default language. Your 445 application, however, should localize properly.</p> 446 447 <h4>Changing the emulator locale from the adb shell</h4> 448 449 <p>To change the locale in the emulator by using the adb shell. </p> 450 451 <ol> 452 <li>Pick the locale you want to test and determine its BCP-47 language tag, for 453 example, Canadian French would be <code>fr-CA</code>.<br> 454 </li> 455 <li>Launch an emulator.</li> 456 <li>From a command-line shell on the host computer, run the following 457 command:<br> 458 <code>adb shell</code><br> 459 or if you have a device attached, specify that you want the emulator by adding 460 the <code>-e</code> option:<br> 461 <code>adb -e shell</code></li> 462 <li>At the adb shell prompt (<code>#</code>), run this command: <br> 463 <code>setprop persist.sys.locale [<em>BCP-47 language tag</em>];stop;sleep 5;start <br> 464 </code>Replace bracketed sections with the appropriate codes from Step 465 1.</li> 466 </ol> 467 468 <p>For instance, to test in Canadian French:</p> 469 470 <p><code>setprop persist.sys.locale fr-CA;stop;sleep 5;start </code></p> 471 472 <p>This will cause the emulator to restart. (It will look like a full reboot, 473 but it is not.) Once the Home screen appears again, re-launch your application, 474 and the application launches with the new locale. </p> 475 476 <h3 id="test-for-default">Testing for Default Resources</h3> 477 <p>Here's how to test whether an application includes every string 478 resource that it needs: </p> 479 <ol><li>Set the emulator or device to a language that your application does not 480 support. For example, if the application has French strings in 481 <code>res/values-fr/</code> but does not have any Spanish strings in 482 <code>res/values-es/</code>, then set the emulator's locale to Spanish. 483 (You can use the Custom Locale application to set the emulator to an 484 unsupported locale.)</li> 485 <li>Run the application.</li> 486 <li>If the application shows an error message and a Force Close button, it might 487 be looking for a string that is not available. Make sure that your 488 <code>res/values/strings.xml</code> file includes a definition for 489 every string that the application uses.</li> 490 </ol> 491 </p> 492 493 <p>If the test is successful, repeat it for other types of 494 configurations. For example, if the application has a layout file called 495 <code>res/layout-land/main.xml</code> but does not contain a file called 496 <code>res/layout-port/main.xml</code>, then set the emulator or device to 497 portrait orientation and see if the application will run. 498