summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/docs/static/i18n-messages.html
blob: 2a4b9b9b04926b3ee599e2ca917c8f61efa56a69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<div id="pageData-name" class="pageData">Formats: Locale-Specific Messages</div>
<div id="pageData-showTOC" class="pageData">true</div>

<p>
Each internationalized extension has at least one
file named <code>messages.json</code>
that provides locale-specific strings for the extension.
This page describes the format of <code>messages.json</code> files.
For information on how to internationalize and localize your extension,
see the <a href="i18n.html">Internationalization</a> page.
</p>

<h2 id="overview"> Field summary </h2>

<p>
The following code shows the supported fields for
<code>messages.json</code>.
Only the "<em>name</em>" and "message" fields are required.
</p>

<pre>
{
  "<a href="#name"><em>name</em></a>": {
    "<a href="#message">message</a>": "<em>Message text, with optional placeholders.</em>",
    "<a href="#description">description</a>": "<em>Translator-aimed description of the message.</em>",
    "<a href="#placeholders">placeholders</a>": {
      "<em>placeholder_name</em>": {
        "content": "<em>A string to be placed within the message.</em>",
        "example": "<em>Translator-aimed example of the placeholder string.</em>"
      },
      ...
    }
  },
  ...
}
</pre>

<h2 id="example"> Example </h2>

<p>
Here's a <code>messages.json</code> file
that defines three messages
named "prompt_for_name", "hello", and "bye":
</p>

<pre>
{
  "prompt_for_name": {
    "message": "What's your name?",
    "description": "Ask for the user's name"
  },
  "hello": {
    "message": "Hello, $USER$",
    "description": "Greet the user",
    "placeholders": {
      "user": {
        "content": "$1",
        "example": "Cira"
      }
    }
  },
  "bye": {
    "message": "Goodbye, $USER$. Come back to $OUR_SITE$ soon!",
    "description": "Say goodbye to the user",
    "placeholders": {
      "our_site": {
        "content": "Example.com",
      },
      "user": {
        "content": "$1",
        "example": "Cira"
      }
    }
  }
}
</pre>


<h2>Field details</h2>

<p>
This section describes each field
that can appear in a <code>messages.json</code> file.
For details on how the messages file is used &mdash;
for example, what happens when a locale doesn't define
all the messages &mdash;
see <a href="i18n.html">Internationalization</a>.
</p>


<h3 id="name">name</h3>

<p>
Actually, there's no field called "name".
This field's name is the name of the message &mdash;
the same <em>name</em> that you see in
<code>__MSG_<em>name</em>__</code>
or <code>getMessage("<em>name</em>")</code>.
</p>

<p>
The name is a case-insensitive key
that lets you retrieve the localized message text.
The name can include the following characters:
</p>

<ul>
  <li> A-Z </li>
  <li> a-z </li>
  <li> 0-9 </li>
  <li> _ (underscore) </li>
  <li> @ </li>
</ul>

<p class="note">
<b>Note:</b>
Don't define names that begin with "@@".
Those names are reserved for
<a href="i18n.html#overview-predefined">predefined messages</a>.
</p>

<p>
Here are three examples of names,
taken from the <a href="#example">Example</a> section:
</p>

<pre>
"prompt_for_name": {
  ...
},
"hello": {
  ...
},
"bye": {
  ...
}
</pre>

<p>
For more examples of using names, see the
<a href="i18n.html">Internationalization</a> page.
</p>


<h3 id="message">message</h3>

<p>
The translated message,
in the form of a string that can contain
<a href="#placeholders">placeholders</a>.
Use <code>$<em>placeholder_name</em>$</code>
(case insensitive)
to refer to a particular placeholder.
For example, you can refer to a placeholder named "our_site" as
<code>$our_site$</code>, <code>$OUR_SITE$</code>, or <code>$oUR_sITe$</code>.
</p>

<p>
Here are three examples of messages,
taken from the <a href="#example">Example</a> section:
</p>

<pre>
"message": "What's your name?"
...
"message": "Hello, $USER$"
...
"message": "Goodbye, $USER$. Come back to $OUR_SITE$ soon!"
</pre>

<p>
To put a dollar sign (<code>$</code>) into the string,
use <code>$$</code>.
For example, use the following code to specify the message
<b>Amount (in $)</b>:

<pre>
"message": "Amount (in $$)"
</pre>

<p>
Although placeholders such as <code>$USER$</code> are
the preferred way of referring to <em>substitution strings</em>
(strings specified using the <em>substitutions</em> parameter of
<a href="i18n.html#method-getMessage"><code>getMessage()</code></a>)
you can also refer to substitution strings directly
within the message.
For example, the following message
refers to the first three substitution strings passed into
<code>getMessage()</code>:
</p>

<pre>
"message": "Params: $1, $2, $3"
</pre>

<p>
Despite that example,
we recommend that you stick to using placeholders
instead of <code>$<em>n</em></code> strings
within your messages.
Think of placeholders as good variable names.
A week after you write your code,
you'll probably forget what <code>$1</code> refers to,
but you'll know what your placeholders refer to.
For more information on placeholders and substitution strings, see
the <a href="#placeholders">placeholders</a> section.
</p>

<h3 id="description">description</h3>

<p>
<em>Optional.</em>
A description of the message,
intended to give context
or details to help the translator
make the best possible translation.
</p>

<p>
Here are three examples of descriptions,
taken from the <a href="#example">Example</a> section:
</p>

<pre>
"description": "Ask for the user's name"
...
"description": "Greet the user"
...
"description": "Say goodbye to the user"
</pre>

<h3 id="placeholders">placeholders</h3>

<p>
<em>Optional.</em>
Defines one or more substrings
to be used within the message.
Here are two reasons you might want to use a placeholder:
</p>

<ul>
  <li>
    To define the text
    for a part of your message
    that shouldn't be translated.
    Examples: HTML code, trademarked names, formatting specifiers.
  </li>
  <li>
    To refer to a substitution string passed into
    <code>getMessage()</code>.
    Example: <code>$1</code>.
  </li>
</ul>

<p>
Each placeholder has a name,
a "content" item,
and an optional "example" item.
A placeholder's name is case-insensitive
and can contain the same characters
as a <a href="#name">message name</a>.
</p>

<p>
The "content" item's value is a string
that can refer to substitution strings, which are
specified using the
<a href="i18n.html#method-getMessage"><code>getMessage()</code></a> method's
<em>substitutions</em> parameter.
The value of a "content" item is typically something like
"Example.com" or "$1".
If you refer to
a substitution string that doesn't exist,
you get an empty string.
The following table shows how
<code>$<em>n</em></code> strings correspond to
strings specified by the <em>substitutions</em> parameter.
</p>

<table>
<tr>
<th> <em>substitutions</em> parameter </th>
<th> Value of $1</th>
<th> Value of $2</th>
<th> Value of $3</th>
</tr>
<tr>
  <td> <code>userName</code> </td>
  <td> value of <code>userName</code> </td>
  <td> <code>""</code> </td>
  <td> <code>""</code> </td>
</tr>
<tr>
  <td> <code>["Cira", "Kathy"]</code> </td>
  <td> <code>"Cira"</code> </td>
  <td> <code>"Kathy"</code> </td>
  <td> <code>""</code> </td>
</tr>
</table>

<p>
The "example" item
(optional, but highly recommended)
helps translators by showing how the content appears to the end user.
For example, a placeholder
for a dollar amount
should have an example like <code>"$23.45"</code>.
</p>

<p>
The following snippet,
taken from the <a href="#example">Example</a> section,
shows a "placeholders" item that contains two placeholders
named "our_site" and "user".
The "our_site" placeholder has no "example" item
because its value is obvious from the "content" field.
</p>

<pre>
"placeholders": {
  "our_site": {
    "content": "Example.com",
  },
  "user": {
    "content": "$1",
    "example": "Cira"
  }
}
</pre>