Home | History | Annotate | Download | only in supporting-devices
      1 page.title=Supporting Different Languages
      2 parent.title=Supporting Different Devices
      3 page.tags=strings,localizing,localization,resources,formats,l10n
      4 helpoutsWidget=true
      5 
      6 trainingnavtop=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     </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 language 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. For more information, see
     45  <a href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">Providing Alternative Resources</a>.</p>
     46 
     47 <p>Once youve decided on the languages you will support, create the resource subdirectories and
     48 string resource files. For example:</p>
     49 
     50 <pre class="classic no-pretty-print">
     51 MyProject/
     52     res/
     53        values/
     54            strings.xml
     55        values-es/
     56            strings.xml
     57        values-fr/
     58            strings.xml
     59 </pre>
     60 
     61 <p>Add the string values for each locale into the appropriate file.</p>
     62 
     63 <p>At runtime, the Android system uses the appropriate set of string resources based on the
     64 locale currently set for the user's device.</p>
     65   
     66 <p>For example, the following are some different string resource files for different languages.</p>
     67 
     68 
     69 <p>English (default locale), <code>/values/strings.xml</code>:</p>
     70 
     71 <pre>
     72 &lt;?xml version="1.0" encoding="utf-8"?>
     73 &lt;resources>
     74     &lt;string name="title">My Application&lt;/string>
     75     &lt;string name="hello_world">Hello World!&lt;/string>
     76 &lt;/resources>
     77 </pre>
     78 
     79 
     80 <p>Spanish, <code>/values-es/strings.xml</code>:</p>
     81 
     82 <pre>
     83 &lt;?xml version="1.0" encoding="utf-8"?>
     84 &lt;resources>
     85     &lt;string name="title">Mi Aplicacin&lt;/string>
     86     &lt;string name="hello_world">Hola Mundo!&lt;/string>
     87 &lt;/resources>
     88 </pre>
     89 
     90 
     91 <p>French, <code>/values-fr/strings.xml</code>:</p>
     92 
     93 <pre>
     94 &lt;?xml version="1.0" encoding="utf-8"?>
     95 &lt;resources>
     96     &lt;string name="title">Mon Application&lt;/string>
     97     &lt;string name="hello_world">Bonjour le monde !&lt;/string>
     98 &lt;/resources>
     99 </pre>
    100 
    101 <p class="note"><strong>Note:</strong> You can use the locale qualifier (or any
    102 configuration qualifer) on any resource type, such as if you want to provide
    103 localized versions of your bitmap drawable. For more information, see <a
    104 href="{@docRoot}guide/topics/resources/localization.html">Localization</a>.
    105 
    106 <h2 id="UseString">Use the String Resources</h2>
    107 
    108 <p>You can reference your string resources in your source code and other XML files using the
    109 resource name defined by the {@code &lt;string>} element's {@code name} attribute.</p>
    110 
    111 <p>In your source code, you can refer to a string resource with the syntax {@code
    112 R.string.&lt;string_name>}. There are a variety of methods that accept a string resource this
    113 way.</p>
    114   
    115 <p>For example:</p>
    116 
    117 <pre>
    118 // Get a string resource from your app's {@link android.content.res.Resources}
    119 String hello = {@link android.content.Context#getResources()}.getString(R.string.hello_world);
    120 
    121 // Or supply a string resource to a method that requires a string
    122 TextView textView = new TextView(this);
    123 textView.setText(R.string.hello_world);
    124 </pre>
    125 
    126 <p>In other XML files, you can refer to a string resource with the syntax {@code
    127 &#64;string/&lt;string_name>} whenever the XML attribute accepts a string value.</p>
    128 
    129 <p>For example:</p>
    130 
    131 <pre>
    132 &lt;TextView
    133     android:layout_width="wrap_content"
    134     android:layout_height="wrap_content"
    135     android:text="@string/hello_world" />
    136 </pre>
    137 
    138 
    139 
    140