1 <h1>Formats: Locale-Specific Messages
</h1>
5 Each internationalized extension or app has at least one
6 file named
<code>messages.json
</code>
7 that provides locale-specific strings.
8 This page describes the format of
<code>messages.json
</code> files.
9 For information on how to internationalize and localize,
10 see the
<a href=
"i18n">Internationalization
</a> page.
13 <h2 id=
"overview"> Field summary
</h2>
16 The following code shows the supported fields for
17 <code>messages.json
</code>.
18 Only the
"<em>name</em>" and
"message" fields are required.
21 <pre data-filename=
"manifest.json">
23 "<a href="#name
"><em>name</em></a>": {
24 "<a href="#message
">message</a>":
"<em>Message text, with optional placeholders.</em>",
25 "<a href="#description
">description</a>":
"<em>Translator-aimed description of the message.</em>",
26 "<a href="#placeholders
">placeholders</a>": {
27 "<em>placeholder_name</em>": {
28 "content":
"<em>A string to be placed within the message.</em>",
29 "example":
"<em>Translator-aimed example of the placeholder string.</em>"
38 <h2 id=
"example"> Example
</h2>
41 Here's a
<code>messages.json
</code> file
42 that defines three messages
43 named
"prompt_for_name",
"hello", and
"bye":
46 <pre data-filename=
"messages.json">
49 "message":
"What's your name?",
50 "description":
"Ask for the user's name"
53 "message":
"Hello, $USER$",
54 "description":
"Greet the user",
63 "message":
"Goodbye, $USER$. Come back to $OUR_SITE$ soon!",
64 "description":
"Say goodbye to the user",
67 "content":
"Example.com",
79 <h2 id=
"field_details">Field details
</h2>
82 This section describes each field
83 that can appear in a
<code>messages.json
</code> file.
84 For details on how the messages file is used
—
85 for example, what happens when a locale doesn't define
86 all the messages
—
87 see
<a href=
"i18n">Internationalization
</a>.
91 <h3 id=
"name">name
</h3>
94 Actually, there's no field called
"name".
95 This field's name is the name of the message
—
96 the same
<em>name
</em> that you see in
97 <code>__MSG_
<em>name
</em>__
</code>
98 or
<code>getMessage(
"<em>name</em>")
</code>.
102 The name is a case-insensitive key
103 that lets you retrieve the localized message text.
104 The name can include the following characters:
111 <li> _ (underscore)
</li>
117 Don't define names that begin with
"@@".
118 Those names are reserved for
119 <a href=
"i18n#overview-predefined">predefined messages
</a>.
123 Here are three examples of names,
124 taken from the
<a href=
"#example">Example
</a> section:
140 For more examples of using names, see the
141 <a href=
"i18n">Internationalization
</a> page.
145 <h3 id=
"message">message
</h3>
148 The translated message,
149 in the form of a string that can contain
150 <a href=
"#placeholders">placeholders
</a>.
151 Use
<code>$
<em>placeholder_name
</em>$
</code>
153 to refer to a particular placeholder.
154 For example, you can refer to a placeholder named
"our_site" as
155 <code>$our_site$
</code>,
<code>$OUR_SITE$
</code>, or
<code>$oUR_sITe$
</code>.
159 Here are three examples of messages,
160 taken from the
<a href=
"#example">Example
</a> section:
164 "message":
"What's your name?"
166 "message":
"Hello, $USER$"
168 "message":
"Goodbye, $USER$. Come back to $OUR_SITE$ soon!"
172 To put a dollar sign (
<code>$
</code>) into the string,
174 For example, use the following code to specify the message
175 <b>Amount (in $)
</b>:
178 "message":
"Amount (in $$)"
182 Although placeholders such as
<code>$USER$
</code> are
183 the preferred way of referring to
<em>substitution strings
</em>
184 (strings specified using the
<em>substitutions
</em> parameter of
185 $(ref:i18n.getMessage))
186 you can also refer to substitution strings directly
188 For example, the following message
189 refers to the first three substitution strings passed into
190 <code>getMessage()
</code>:
194 "message":
"Params: $1, $2, $3"
198 Despite that example,
199 we recommend that you stick to using placeholders
200 instead of
<code>$
<em>n
</em></code> strings
201 within your messages.
202 Think of placeholders as good variable names.
203 A week after you write your code,
204 you'll probably forget what
<code>$
1</code> refers to,
205 but you'll know what your placeholders refer to.
206 For more information on placeholders and substitution strings, see
207 the
<a href=
"#placeholders">placeholders
</a> section.
210 <h3 id=
"description">description
</h3>
214 A description of the message,
215 intended to give context
216 or details to help the translator
217 make the best possible translation.
221 Here are three examples of descriptions,
222 taken from the
<a href=
"#example">Example
</a> section:
226 "description":
"Ask for the user's name"
228 "description":
"Greet the user"
230 "description":
"Say goodbye to the user"
233 <h3 id=
"placeholders">placeholders
</h3>
237 Defines one or more substrings
238 to be used within the message.
239 Here are two reasons you might want to use a placeholder:
245 for a part of your message
246 that shouldn't be translated.
247 Examples: HTML code, trademarked names, formatting specifiers.
250 To refer to a substitution string passed into
251 <code>getMessage()
</code>.
252 Example:
<code>$
1</code>.
257 Each placeholder has a name,
259 and an optional
"example" item.
260 A placeholder's name is case-insensitive
261 and can contain the same characters
262 as a
<a href=
"#name">message name
</a>.
266 The
"content" item's value is a string
267 that can refer to substitution strings, which are
269 $(ref:i18n.getMessage) method's
270 <em>substitutions
</em> parameter.
271 The value of a
"content" item is typically something like
272 "Example.com" or
"$1".
274 a substitution string that doesn't exist,
275 you get an empty string.
276 The following table shows how
277 <code>$
<em>n
</em></code> strings correspond to
278 strings specified by the
<em>substitutions
</em> parameter.
281 <table class=
"simple">
283 <th> <em>substitutions
</em> parameter
</th>
284 <th> Value of $
1</th>
285 <th> Value of $
2</th>
286 <th> Value of $
3</th>
289 <td> <code>userName
</code> </td>
290 <td> value of
<code>userName
</code> </td>
291 <td> <code>""</code> </td>
292 <td> <code>""</code> </td>
295 <td> <code>[
"Cira",
"Kathy"]
</code> </td>
296 <td> <code>"Cira"</code> </td>
297 <td> <code>"Kathy"</code> </td>
298 <td> <code>""</code> </td>
304 (optional, but highly recommended)
305 helps translators by showing how the content appears to the end user.
306 For example, a placeholder
308 should have an example like
<code>"$23.45"</code>.
312 The following snippet,
313 taken from the
<a href=
"#example">Example
</a> section,
314 shows a
"placeholders" item that contains two placeholders
315 named
"our_site" and
"user".
316 The
"our_site" placeholder has no
"example" item
317 because its value is obvious from the
"content" field.
323 "content":
"Example.com",