i18n.html revision 731df977c0511bca2206b5f333555b1205ff1f43
1<div id="pageData-name" class="pageData">Internationalization (i18n)</div>
2
3<!--
4[NOTEs for editors:
5 * Try to be consistent about string vs. message (it's probably not yet).
6-->
7
8<!-- BEGIN AUTHORED CONTENT -->
9<p id="classSummary">
10An <em>internationalized</em> extension
11can be easily
12<em>localized</em> &mdash;
13adapted to languages and regions
14that it didn't originally support.
15</p>
16
17<p>
18To internationalize your extension,
19you need to put all of its user-visible strings into a file
20named <a href="i18n-messages.html"><code>messages.json</code></a>.
21Each time you localize your extension
22you add a messages file
23under a directory
24named <code>_locales/<em>localeCode</em></code>,
25where <em>localeCode</em> is a code such as
26<code>en</code> for English.
27</p>
28
29<p>
30Here's the file hierarchy
31for an internationalized extension that supports
32English (<code>en</code>),
33Spanish (<code>es</code>), and
34Korean (<code>ko</code>):
35</p>
36
37<img src="images/i18n-hierarchy.gif"
38 alt='In the extension directory: manifest.json, *.html, *.js, _locales directory. In the _locales directory: en, es, and ko directories, each with a messages.json file.'
39 width="385" height="77" />
40
41
42<h2 id="l10">How to support multiple languages</h2>
43
44<p>
45Say you have an extension
46with the files shown in the following figure:
47</p>
48
49<img src="images/i18n-before.gif"
50 alt='A manifest.json file and a file with JavaScript. The .json file has "name": "Hello World". The JavaScript file has title = "Hello World";'
51 width="323" height="148">
52
53<p>
54To internationalize this extension,
55you name each user-visible string
56and put it into a messages file.
57The extension's manifest,
58CSS files,
59and JavaScript code
60use each string's name to get its localized version.
61</p>
62
63<p>
64Here's what the extension looks like when it's internationalized
65(note that it still has only English strings):
66</p>
67
68<img src="images/i18n-after-1.gif"
69 alt='In the manifest.json file, "Hello World" has been changed to "__MSG_extName__", and a new "default_locale" item has the value "en". In the JavaScript file, "Hello World" has been changed to chrome.i18n.getMessage("extName"). A new file named _locales/en/messages.json defines "extName".'
70 width="782" height="228">
71
72<p class="note">
73<b>Important:</b>
74If an extension has a <code>_locales</code> directory,
75the <a href="manifest.html">manifest</a>
76<b>must</b> define "default_locale".
77</p>
78
79<p>
80Some notes about internationalizing extensions:
81</p>
82
83<ul>
84  <li><p>
85    You can use any of the <a href="#overview-locales">supported locales</a>.
86    If you use an unsupported locale,
87    Google Chrome ignores it.
88  </p></li>
89
90  <li>
91    In <code>manifest.json</code>
92    and CSS files,
93    refer to a string named <em>messagename</em> like this:
94    <pre>__MSG_<em>messagename</em>__</pre>
95  </li>
96
97  <li>
98    In your extension's JavaScript code,
99    refer to a string named <em>messagename</em>
100    like this:
101    <pre>chrome.i18n.getMessage("<em>messagename</em>")</pre>
102
103  <li> <p>
104    In each call to <code>getMessage()</code>,
105    you can supply up to 9 strings
106    to be included in the message.
107    See <a href="#examples-getMessage">Examples: getMessage</a>
108    for details.
109    </p>
110  </li>
111
112  <li><p>
113    Some messages, such as <code>@@bidi_dir</code> and <code>@@ui_locale</code>,
114    are provided by the internationalization system.
115    See the <a href="#overview-predefined">Predefined messages</a> section
116    for a full list of predefined message names.
117    </p>
118  </li>
119
120  <li>
121    In <code>messages.json</code>,
122    each user-visible string has a name, a "message" item,
123    and an optional "description" item.
124    The name is a key
125    such as "extName" or "search_string"
126    that identifies the string.
127    The "message" specifies
128    the value of the string in this locale.
129    The optional "description"
130    provides help to translators,
131    who might not be able to see how the string is used in your extension.
132    For example:
133<pre>
134{
135  "search_string": {
136    "message": "hello%20world",
137    "description": "The string we search for. Put %20 between words that go together."
138  },
139  ...
140}</pre>
141
142<p>
143For more information, see
144<a href="i18n-messages.html">Formats: Locale-Specific Messages</a>.
145</p>
146  </li>
147</ul>
148
149<p>
150Once an extension is internationalized,
151translating it is simple.
152You copy <code>messages.json</code>,
153translate it,
154and put the copy into a new directory under <code>_locales</code>.
155For example, to support Spanish,
156just put a translated copy of <code>messages.json</code>
157under <code>_locales/es</code>.
158The following figure shows the previous extension
159with a new Spanish translation.
160</p>
161
162<img src="images/i18n-after-2.gif"
163 alt='This looks the same as the previous figure, but with a new file at _locales/es/messages.json that contains a Spanish translation of the messages.'
164 width="782" height="358">
165
166
167<h2 id="overview-predefined">Predefined messages</h2>
168
169<p>
170The internationalization system provides a few predefined
171messages to help you localize your extension.
172These include <code>@@ui_locale</code>,
173so you can detect the current UI locale,
174and a few <code>@@bidi_...</code> messages
175that let you detect the text direction.
176The latter messages have similar names to constants in the
177<a href="http://code.google.com/apis/gadgets/docs/i18n.html#BIDI">
178gadgets BIDI (bi-directional) API</a>.
179</p>
180
181<p>
182The special message <code>@@extension_id</code>
183can be used in the CSS and JavaScript files of any extension,
184whether or not the extension is localized.
185This message doesn't work in manifest files.
186</p>
187
188<p class="note">
189<b>Note:</b>
190Content script CSS files can't use
191predefined messages such as <code>@@extension_id</code>.
192For details, see
193<a href="http://crbug.com/39899">bug 39899</a>.
194</p>
195
196<p>
197The following table describes each predefined message.
198</p>
199
200<table>
201<tr>
202  <th>Message name</th> <th>Description</th>
203</tr>
204<tr>
205  <td> <code>@@extension_id</code> </td>
206  <td>The extension ID;
207    you might use this string to construct URLs
208    for resources inside the extension.
209    Even unlocalized extensions can use this message.
210    <br>
211    <b>Note:</b> You can't use this message in a manifest file.
212    </td>
213</tr>
214<tr>
215  <td> <code>@@ui_locale</code> </td>
216  <td>The current locale;
217    you might use this string to construct locale-specific URLs. </td>
218</tr>
219<tr>
220  <td> <code>@@bidi_dir</code> </td>
221  <td> The text direction for the current locale,
222       either "ltr" for left-to-right languages such as English
223       or "rtl" for right-to-left languages such as Japanese. </td>
224</tr>
225<tr>
226  <td> <code>@@bidi_reversed_dir</code> </td>
227  <td> If the <code>@@bidi_dir</code> is "ltr", then this is "rtl";
228       otherwise, it's "ltr". </td>
229</tr>
230<tr>
231  <td> <code>@@bidi_start_edge</code> </td>
232  <td> If the <code>@@bidi_dir</code> is "ltr", then this is "left";
233       otherwise, it's "right". </td>
234</tr>
235<tr>
236  <td> <code>@@bidi_end_edge</code> </td>
237  <td> If the <code>@@bidi_dir</code> is "ltr", then this is "right";
238       otherwise, it's "left". </td>
239</tr>
240</table>
241
242<p>
243Here's an example of using <code>@@extension_id</code> in a CSS file
244to construct a URL:
245</p>
246
247<pre>
248body {
249  <b>background-image:url('chrome-extension://__MSG_@@extension_id__/background.png');</b>
250}
251</pre>
252
253<p>
254If the extension ID is abcdefghijklmnopqrstuvwxyzabcdef,
255then the bold line in the previous code snippet becomes:
256</p>
257
258<pre>
259background-image:url('chrome-extension://abcdefghijklmnopqrstuvwxyzabcdef/background.png');
260</pre>
261
262<p>
263Here's an example of using <code>@@bidi_*</code> messages in a CSS file:
264</p>
265
266<pre>
267body {
268  <b>dir: __MSG_@@bidi_dir__;</b>
269}
270
271div#header {
272  margin-bottom: 1.05em;
273  overflow: hidden;
274  padding-bottom: 1.5em;
275  <b>padding-__MSG_@@bidi_start_edge__: 0;</b>
276  <b>padding-__MSG_@@bidi_end_edge__: 1.5em;</b>
277  position: relative;
278}
279</pre>
280
281<p>
282For left-to-right languages such as English,
283the bold lines become:
284</p>
285
286<pre>
287dir: ltr;
288padding-left: 0;
289padding-right: 1.5em;
290</pre>
291
292
293<h2 id="overview-locales">Locales</h2>
294
295<p>
296Extensions can use all the locales that Google Chrome supports,
297plus a few (such as <code>en</code>)
298that let a single translation support multiple variations of a language
299(such as <code>en_GB</code> and <code>en_US</code>).
300</p>
301
302
303<h3 id="locales-supported">Supported locales</h3>
304
305<p>
306Your extension can use any of the following locales:
307</p>
308
309<p>
310<code>am  ar  bg  bn  ca  cs  da  de  el  en  en_GB  en_US  es  es_419  et  fi  fil  fr  gu  he  hi  hr  hu  id  it  ja  kn  ko  lt
311lv  ml  mr  nb  nl  or  pl  pt  pt_BR  pt_PT  ro  ru  sk  sl  sr  sv  sw  ta  te  th  tr  uk  vi  zh  zh_CN  zh_TW</code>
312</p>
313
314
315<h3 id="locales-usage">How extensions find strings</h3>
316
317<p>
318You don't have to define every string for every locale
319that your internationalized extension supports.
320As long as the default locale's <code>messages.json</code> file
321has a value for every string,
322your extension will run no matter how sparse a translation is.
323Here's how the extension system searches for a message:
324</p>
325
326<ol>
327  <li>
328     Search the messages file (if any)
329     for the user's preferred locale.
330     For example, when Google Chrome's locale is set to
331     British English (<code>en_GB</code>),
332     the system first looks for the message in
333     <code>_locales/en_GB/messages.json</code>.
334     If that file exists and the message is there,
335     the system looks no further.
336  </li>
337  <li>
338     If the user's preferred locale has a region
339     (that is, the locale has an underscore: _),
340     search the locale without that region.
341     For example, if the <code>en_GB</code> messages file
342     doesn't exist or doesn't contain the message,
343     the system looks in the <code>en</code> messages file.
344     If that file exists and the message is there,
345     the system looks no further.
346  </li>
347  <li>
348     Search the messages file for the extension's default locale.
349     For example, if the extension's "default_locale" is set to "es",
350     and neither <code>_locales/en_GB/messages.json</code>
351     nor <code>_locales/en/messages.json</code> contains the message,
352     the extension uses the message from
353     <code>_locales/es/messages.json</code>.
354  </li>
355</ol>
356
357<p>
358In the following figure,
359the message named "colores" is in all three locales
360that the extension supports,
361but "extName" is in only two of the locales.
362Wherever a user running Google Chrome in US English sees the label "Colors",
363a user of British English sees "Colours".
364Both US English and British English users
365see the extension name "Hello World".
366Because the default language is Spanish,
367users running Google Chrome in any non-English language
368see the label "Colores" and the extension name "Hola mundo".
369</p>
370
371<img src="images/i18n-strings.gif"
372 alt='Four files: manifest.json and three messages.json files (for es, en, and en_GB).  The es and en files show entries for messages named "extName" and "colores"; the en_GB file has just one entry (for "colores").'
373 width="493" height="488" />
374
375<h3 id="locales-testing">How to set your browser's locale</h3>
376
377<p>
378To test translations, you might want to set your browser's locale.
379This section tells you how to set the locale in
380<a href="#testing-win">Windows</a>,
381<a href="#testing-mac">Mac OS X</a>, and
382<a href="#testing-linux">Linux</a>.
383</p>
384
385<h4 id="testing-win">Windows</h4>
386
387<p>
388You can change the locale using either
389a locale-specific shortcut
390or the Google Chrome UI.
391The shortcut approach is quicker, once you've set it up,
392and it lets you use several languages at once.
393</p>
394
395<h5 id="win-shortcut">Using a locale-specific shortcut</h5>
396
397<p>
398To create and use a shortcut that launches Google Chrome
399with a particular locale:
400</p>
401
402<ol>
403  <li>
404    Make a copy of the Google Chrome shortcut
405    that's already on your desktop.
406  </li>
407  <li>
408    Rename the new shortcut to match the new locale.
409  </li>
410  <li>
411    Change the shortcut's properties
412    so that the Target field specifies the
413    <code>--lang</code> and
414    <code>--user-data-dir</code> flags.
415    The target should look something like this:
416
417<pre><em>path_to_chrome.exe</em> --lang=<em>locale</em> --user-data-dir=c:\<em>locale_profile_dir</em></pre>
418  </li>
419
420  <li>
421    Launch Google Chrome by double-clicking the shortcut.
422  </li>
423</ol>
424
425<p>
426For example, to create a shortcut
427that launches Google Chrome in Spanish (<code>es</code>),
428you might create a shortcut named <code>chrome-es</code>
429that has the following target:
430</p>
431
432<pre><em>path_to_chrome.exe</em> --lang=es --user-data-dir=c:\chrome-profile-es</pre>
433
434<p>
435You can create as many shortcuts as you like,
436making it easy to test your extension in multiple languages.
437For example:
438</p>
439
440<pre><em>path_to_chrome.exe</em> --lang=en --user-data-dir=c:\chrome-profile-en
441<em>path_to_chrome.exe</em> --lang=en_GB --user-data-dir=c:\chrome-profile-en_GB
442<em>path_to_chrome.exe</em> --lang=ko --user-data-dir=c:\chrome-profile-ko</pre>
443
444<p class="note">
445<b>Note:</b>
446Specifying <code>--user-data-dir</code> is optional but handy.
447Having one data directory per locale
448lets you run the browser
449in several languages at the same time.
450A disadvantage is that because the locales' data isn't shared,
451you have to install your extension multiple times &mdash; once per locale,
452which can be challenging when you don't speak the language.
453For more information, see
454<a href="http://www.chromium.org/developers/creating-and-using-profiles">Creating and Using Profiles</a>.
455</p>
456
457
458<h5 id="win-ui">Using the UI</h5>
459
460<p>
461Here's how to change the locale using the UI on Google Chrome for Windows:
462</p>
463
464<ol>
465  <li> Tools menu (wrench) > <b>Options</b> </li>
466  <li> Choose the <b>Under the Hood</b> tab </li>
467  <li> Scroll down to <b>Web Content</b> </li>
468  <li> Click <b>Change font and language settings</b> </li>
469  <li> Choose the <b>Languages</b> tab </li>
470  <li> Use the drop down to set the <b>Google Chrome language</b> </li>
471  <li> Restart Chrome </li>
472</ol>
473
474
475<h4 id="testing-mac">Mac OS X</h4>
476
477<p>
478To change the locale on Mac,
479you use the system preferences.
480</p>
481
482<ol>
483  <li> From the Apple menu, choose <b>System Preferences</b> </li>
484  <li> Under the <b>Personal</b> section, choose <b>International</b> </li>
485  <li> Choose your language and location </li>
486  <li> Restart Chrome </li>
487</ol>
488
489
490<h4 id="testing-linux">Linux</h4>
491
492<p>
493To change the locale on Linux,
494first quit Google Chrome.
495Then, all in one line,
496set the LANGUAGE environment variable
497and launch Google Chrome.
498For example:
499</p>
500
501<pre>
502LANGUAGE=es /chrome
503</pre>
504
505
506<h2 id="overview-examples">Examples</h2>
507
508<p>
509You can find simple examples of internationalization in the
510<a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/i18n/">examples/api/i18n</a>
511directory.
512For a more complete example, see
513<a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/extensions/news_i18n/">examples/extensions/news_i18n</a>
514(compare it to
515<a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/extensions/news/">examples/extensions/news</a>).
516For other examples and for help in viewing the source code, see
517<a href="samples.html">Samples</a>.
518</p>
519
520
521<h3 id="examples-getMessage">Examples: getMessage</h3>
522
523<!--
524[PENDING: improve this section. it should probably start with a
525one-variable example that includes the messages.json code.]
526-->
527
528<p>
529The following code gets a localized message from the browser
530and displays it as a string.
531It replaces two placeholders within the message with the strings
532"string1" and "string2".
533</p>
534
535<pre>
536function getMessage() {
537  var message = chrome.i18n.getMessage("click_here", ["string1", "string2"]);
538  document.getElementById("languageSpan").innerHTML = message;
539}
540</pre>
541
542<p>
543Here's how you'd supply and use a single string:
544</p>
545
546<pre>
547<em>// In JavaScript code</em>
548status.innerText = chrome.i18n.getMessage("error", errorDetails);
549
550<em>// In messages.json</em>
551"error": {
552  "message": "Error: $details$",
553  "description": "Generic error template. Expects error parameter to be passed in.",
554  "placeholders": {
555    "details": {
556      "content": "$1",
557      "example": "Failed to fetch RSS feed."
558    }
559  }
560}
561</pre>
562
563<p>
564For more information about placeholders, see the
565<a href="i18n-messages.html">Locale-Specific Messages</a> page.
566For details on calling <code>getMessage()</code>, see the
567<a href="#method-getMessage">API reference</a>.
568</p>
569
570<h3 id="example-accept-languages">Example: getAcceptLanguages</h3>
571<p>
572The following code gets accept-languages from the browser and displays them as a
573string by separating each accept-language with ','.
574</p>
575
576<pre>
577function getAcceptLanguages() {
578  chrome.i18n.getAcceptLanguages(function(languageList) {
579    var languages = languageList.join(",");
580    document.getElementById("languageSpan").innerHTML = languages;
581  })
582}
583</pre>
584
585<p>
586For details on calling <code>getAcceptLanguages()</code>, see the
587<a href="#method-getAcceptLanguages">API reference</a>.
588</p>
589
590<!-- END AUTHORED CONTENT -->
591