1<div id="pageData-name" class="pageData">Formats: Locale-Specific Messages</div> 2<div id="pageData-showTOC" class="pageData">true</div> 3 4<p> 5Each internationalized extension has at least one 6file named <code>messages.json</code> 7that provides locale-specific strings for the extension. 8This page describes the format of <code>messages.json</code> files. 9For information on how to internationalize and localize your extension, 10see the <a href="i18n.html">Internationalization</a> page. 11</p> 12 13<h2 id="overview"> Field summary </h2> 14 15<p> 16The following code shows the supported fields for 17<code>messages.json</code>. 18Only the "<em>name</em>" and "message" fields are required. 19</p> 20 21<pre> 22{ 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>" 30 }, 31 ... 32 } 33 }, 34 ... 35} 36</pre> 37 38<h2 id="example"> Example </h2> 39 40<p> 41Here's a <code>messages.json</code> file 42that defines three messages 43named "prompt_for_name", "hello", and "bye": 44</p> 45 46<pre> 47{ 48 "prompt_for_name": { 49 "message": "What's your name?", 50 "description": "Ask for the user's name" 51 }, 52 "hello": { 53 "message": "Hello, $USER$", 54 "description": "Greet the user", 55 "placeholders": { 56 "user": { 57 "content": "$1", 58 "example": "Cira" 59 } 60 } 61 }, 62 "bye": { 63 "message": "Goodbye, $USER$. Come back to $OUR_SITE$ soon!", 64 "description": "Say goodbye to the user", 65 "placeholders": { 66 "our_site": { 67 "content": "Example.com", 68 }, 69 "user": { 70 "content": "$1", 71 "example": "Cira" 72 } 73 } 74 } 75} 76</pre> 77 78 79<h2>Field details</h2> 80 81<p> 82This section describes each field 83that can appear in a <code>messages.json</code> file. 84For details on how the messages file is used — 85for example, what happens when a locale doesn't define 86all the messages — 87see <a href="i18n.html">Internationalization</a>. 88</p> 89 90 91<h3 id="name">name</h3> 92 93<p> 94Actually, there's no field called "name". 95This field's name is the name of the message — 96the same <em>name</em> that you see in 97<code>__MSG_<em>name</em>__</code> 98or <code>getMessage("<em>name</em>")</code>. 99</p> 100 101<p> 102The name is a case-insensitive key 103that lets you retrieve the localized message text. 104The name can include the following characters: 105</p> 106 107<ul> 108 <li> A-Z </li> 109 <li> a-z </li> 110 <li> 0-9 </li> 111 <li> _ (underscore) </li> 112 <li> @ </li> 113</ul> 114 115<p class="note"> 116<b>Note:</b> 117Don't define names that begin with "@@". 118Those names are reserved for 119<a href="i18n.html#overview-predefined">predefined messages</a>. 120</p> 121 122<p> 123Here are three examples of names, 124taken from the <a href="#example">Example</a> section: 125</p> 126 127<pre> 128"prompt_for_name": { 129 ... 130}, 131"hello": { 132 ... 133}, 134"bye": { 135 ... 136} 137</pre> 138 139<p> 140For more examples of using names, see the 141<a href="i18n.html">Internationalization</a> page. 142</p> 143 144 145<h3 id="message">message</h3> 146 147<p> 148The translated message, 149in the form of a string that can contain 150<a href="#placeholders">placeholders</a>. 151Use <code>$<em>placeholder_name</em>$</code> 152(case insensitive) 153to refer to a particular placeholder. 154For 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>. 156</p> 157 158<p> 159Here are three examples of messages, 160taken from the <a href="#example">Example</a> section: 161</p> 162 163<pre> 164"message": "What's your name?" 165... 166"message": "Hello, $USER$" 167... 168"message": "Goodbye, $USER$. Come back to $OUR_SITE$ soon!" 169</pre> 170 171<p> 172To put a dollar sign (<code>$</code>) into the string, 173use <code>$$</code>. 174For example, use the following code to specify the message 175<b>Amount (in $)</b>: 176 177<pre> 178"message": "Amount (in $$)" 179</pre> 180 181<p> 182Although placeholders such as <code>$USER$</code> are 183the preferred way of referring to <em>substitution strings</em> 184(strings specified using the <em>substitutions</em> parameter of 185<a href="i18n.html#method-getMessage"><code>getMessage()</code></a>) 186you can also refer to substitution strings directly 187within the message. 188For example, the following message 189refers to the first three substitution strings passed into 190<code>getMessage()</code>: 191</p> 192 193<pre> 194"message": "Params: $1, $2, $3" 195</pre> 196 197<p> 198Despite that example, 199we recommend that you stick to using placeholders 200instead of <code>$<em>n</em></code> strings 201within your messages. 202Think of placeholders as good variable names. 203A week after you write your code, 204you'll probably forget what <code>$1</code> refers to, 205but you'll know what your placeholders refer to. 206For more information on placeholders and substitution strings, see 207the <a href="#placeholders">placeholders</a> section. 208</p> 209 210<h3 id="description">description</h3> 211 212<p> 213<em>Optional.</em> 214A description of the message, 215intended to give context 216or details to help the translator 217make the best possible translation. 218</p> 219 220<p> 221Here are three examples of descriptions, 222taken from the <a href="#example">Example</a> section: 223</p> 224 225<pre> 226"description": "Ask for the user's name" 227... 228"description": "Greet the user" 229... 230"description": "Say goodbye to the user" 231</pre> 232 233<h3 id="placeholders">placeholders</h3> 234 235<p> 236<em>Optional.</em> 237Defines one or more substrings 238to be used within the message. 239Here are two reasons you might want to use a placeholder: 240</p> 241 242<ul> 243 <li> 244 To define the text 245 for a part of your message 246 that shouldn't be translated. 247 Examples: HTML code, trademarked names, formatting specifiers. 248 </li> 249 <li> 250 To refer to a substitution string passed into 251 <code>getMessage()</code>. 252 Example: <code>$1</code>. 253 </li> 254</ul> 255 256<p> 257Each placeholder has a name, 258a "content" item, 259and an optional "example" item. 260A placeholder's name is case-insensitive 261and can contain the same characters 262as a <a href="#name">message name</a>. 263</p> 264 265<p> 266The "content" item's value is a string 267that can refer to substitution strings, which are 268specified using the 269<a href="i18n.html#method-getMessage"><code>getMessage()</code></a> method's 270<em>substitutions</em> parameter. 271The value of a "content" item is typically something like 272"Example.com" or "$1". 273If you refer to 274a substitution string that doesn't exist, 275you get an empty string. 276The following table shows how 277<code>$<em>n</em></code> strings correspond to 278strings specified by the <em>substitutions</em> parameter. 279</p> 280 281<table> 282<tr> 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> 287</tr> 288<tr> 289 <td> <code>userName</code> </td> 290 <td> value of <code>userName</code> </td> 291 <td> <code>""</code> </td> 292 <td> <code>""</code> </td> 293</tr> 294<tr> 295 <td> <code>["Cira", "Kathy"]</code> </td> 296 <td> <code>"Cira"</code> </td> 297 <td> <code>"Kathy"</code> </td> 298 <td> <code>""</code> </td> 299</tr> 300</table> 301 302<p> 303The "example" item 304(optional, but highly recommended) 305helps translators by showing how the content appears to the end user. 306For example, a placeholder 307for a dollar amount 308should have an example like <code>"$23.45"</code>. 309</p> 310 311<p> 312The following snippet, 313taken from the <a href="#example">Example</a> section, 314shows a "placeholders" item that contains two placeholders 315named "our_site" and "user". 316The "our_site" placeholder has no "example" item 317because its value is obvious from the "content" field. 318</p> 319 320<pre> 321"placeholders": { 322 "our_site": { 323 "content": "Example.com", 324 }, 325 "user": { 326 "content": "$1", 327 "example": "Cira" 328 } 329} 330</pre> 331 332 333 334