Skip to content

Commit ef794f3

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored andcommitted
[cleanup] Fix some formatting in localization guide
Using formatter that understand Markdown these break as anything inside `` is expected to be in a single line Bug: none Change-Id: Iaf343b86372e647ee6f8af91e36b3911ca5522a6 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6725902 Auto-Submit: Nikolay Vitkov <[email protected]> Reviewed-by: Alina Varkki <[email protected]> Commit-Queue: Alina Varkki <[email protected]>
1 parent d924cd4 commit ef794f3

File tree

1 file changed

+86
-79
lines changed

1 file changed

+86
-79
lines changed

docs/cookbook/localization.md

Lines changed: 86 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,21 @@ import * as i18n from '../i18n/i18n.js';
2121
// at the top of example.js file, after import statements
2222
const UIStrings = {
2323
/**
24-
* @description A string that is already added
25-
*/
26-
alreadyAddedString: 'Someone already created a "UIStrings = {}" and added this string',
24+
* @description A string that is already added
25+
*/
26+
alreadyAddedString:
27+
'Someone already created a "UIStrings = {}" and added this string',
2728
/**
28-
* @description This is an example description for my new string
29-
*/
29+
* @description This is an example description for my new string
30+
*/
3031
addThisString: 'The new string I want to add',
3132
/**
32-
* @description This is an example description for my new string with placeholder
33-
* @example {example for placeholder} PH1
34-
*/
33+
* @description This is an example description for my new
34+
* string with placeholder
35+
* @example {example for placeholder} PH1
36+
*/
3537
addAnotherString: 'Another new string I want to add, with {PH1}',
36-
};
38+
} as const;
3739
const str_ = i18n.i18n.registerUIStrings('example.js', UIStrings);
3840
const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
3941

@@ -44,39 +46,44 @@ const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
4446
const message1 = i18nString(UIStrings.addThisString);
4547
console.log(message1); // The new string I want to add
4648

47-
const message2 = i18nString(UIStrings.addAnotherString, {PH1: 'a placeholder'});
48-
console.log(message2); // Another new string I want to add, with a placeholder
49+
const message2 = i18nString(UIStrings.addAnotherString, {
50+
PH1: 'a placeholder',
51+
});
52+
// Another new string I want to add, with a placeholder
53+
console.log(message2);
4954
```
5055

5156
1. If there is already `UIStrings = {}` declared in the file, add your string
5257
to it. If there isn't `UIStrings = {}` in the file, create one and add your
5358
string, also register the new UIStrings into the `en-US.json` by adding:
5459

55-
1. `const str_ = i18n.i18n.registerUIStrings({the current fileName.js,
56-
relative to front_end}, UIStrings);`
57-
1. `const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);`
60+
```js
61+
// Filename should be relative to front_end folder
62+
const str_ = i18n.i18n.registerUIStrings('<filename>', UIStrings);
63+
const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
64+
```
5865

59-
2. Add description and examples for placeholder(if any):
66+
2. Add description and examples for placeholder (if any):
6067

61-
1. To specify the description, use `@description …` `@description This is
62-
an example description for my new string`
63-
2. To specify an example for placeholder, use `@example {…} …` `@example
64-
{example for placeholder} PH1`
68+
1. To specify the description, use `@description …` `@description This is an example description for my new string`
69+
2. To specify an example for placeholder, use `@example {…} …` `@example {example for placeholder} PH1`
6570
3. To distinguish messages with the same content, use `@meaning …`
66-
`@meaning This is an example meaning to differentiate this message from
67-
the other message with same content`
71+
`@meaning This is an example meaning to differentiate this message from the other message with same content`
6872

6973
3. Make sure your string is localizable:
7074

71-
1. Do not assume word order by using concatenation. Use the whole string. ❌
72-
`javascript 'Add' + 'breakpoint'` ✔️ `javascript 'Add breakpoint'` or ❌
73-
`javascript let description = 'first part' if (condition) description +=
74-
' second part'` ✔️ `javascript let description if (condition)
75-
description = 'first part second part' else description = 'first part'`
75+
1. Do not assume word order by using concatenation. Use the whole string.
76+
77+
-`javascript 'Add' + 'breakpoint'`
78+
- ✔️ `javascript 'Add breakpoint'` or
79+
-`javascript let description = 'first part' if (condition) description +=' second part'`
80+
- ✔️ `javascript let description if (condition) description = 'first part second part' else description = 'first part'`
81+
7682
2. Use placeholder over concatenation. This is so that the translators can
7783
adjust variable order based on what works in another language. For
78-
example: ❌ `javascript 'Check ' + title + ' for more information.'` ✔️
79-
`javascript 'Check {PH1} for more information.', {PH1: title}`
84+
example:
85+
-`javascript 'Check ' + title + ' for more information.'`
86+
- ✔️ `javascript 'Check {PH1} for more information.', {PH1: title}`
8087
3. If your string contains <b>leading or trailing white space</b>, it's
8188
usually an indication that it's half of a sentence. This decreases
8289
localizability as it's essentially concatenating. Modify it so that it
@@ -88,38 +95,35 @@ console.log(message2); // Another new string I want to add, with a placeholder
8895

8996
❌ Not localized
9097

91-
- Numbers: 1, 1.23, 1.2e3, etc.
92-
- Application data: error codes, enums, database names, rgba, urls,
93-
etc.
98+
- Numbers: 1, 1.23, 1.2e3, etc.
99+
- Application data: error codes, enums, database names, rgba, urls,
100+
etc.
94101

95102
✔️ Can be localized
96103

97-
- Words and sentences
98-
- Punctuation
99-
- Units of measurement: kb/s, mph, etc.
104+
- Words and sentences
105+
- Punctuation
106+
- Units of measurement: kb/s, mph, etc.
100107

101108
4. The following commands would add the new strings to `en-US.json`:
102109

103-
- `git cl presubmit --upload`, or
104-
- `node third_party/i18n/collect-strings.js` under the DevTools src folder
110+
- `git cl presubmit --upload`, or
111+
- `node third_party/i18n/collect-strings.js` under the DevTools src folder
105112

106113
5. Strings containing possible plurals have a special format in ICU. This is
107114
because plurals work quite differently in other languages, e.g. special
108115
forms for two or three items.
109116

110-
❌ `javascript if (count === 1) { str = '1 breakpoint'; } else { str = '{n}
111-
breakpoints', {n: count}; }`
117+
`javascript if (count === 1) { str = '1 breakpoint'; } else { str = '{n} breakpoints', {n: count}; }`
112118

113-
✔️ `javascript '{n, plural, =1 {# breakpoint} other {# breakpoints}}', {n:
114-
count};`
119+
✔️ `javascript '{n, plural, =1 {# breakpoint} other {# breakpoints}}', {n:count};`
115120

116-
- '#' is replaced with the value of `n`
117-
- 'n' is a naming convention, but any name can be used
118-
- Nesting placeholders inside of plurals is allowed
119-
- Put the entire string within the plural switch, e.g. `{# breakpoints
120-
were found}`, not `{# breakpoints} were found`
121-
- Always provide the `=1` and the `other` case, even if they are the same
122-
for English.
121+
- `#` is replaced with the value of `n`
122+
- `n` is a naming convention, but any name can be used
123+
- Nesting placeholders inside of plurals is allowed
124+
- Put the entire string within the plural switch, e.g. `{# breakpoints were found}`, not `{# breakpoints} were found`
125+
- Always provide the `=1` and the `other` case, even if they are the same
126+
for English.
123127
124128
### Modifying a string
125129
@@ -146,14 +150,17 @@ object for placeholders (if any)
146150

147151
const UIStrings = {
148152
/**
149-
* @description This is an example description for my new string with placeholder
150-
* @example {example for placeholder} PH1
151-
* @example {example 2 for placeholder 2} PH2
152-
*/
153+
* @description This is an example description for my new string with placeholder
154+
* @example {example for placeholder} PH1
155+
* @example {example 2 for placeholder 2} PH2
156+
*/
153157
addAnotherString: 'Another new string I want to add, with {PH1} and {PH2}',
154-
};
158+
} as const;
155159

156-
message = i18nString(UIStrings.addAnotherString, {PH1: 'a placeholder', PH2: 'another placeholder'});
160+
message = i18nString(UIStrings.addAnotherString, {
161+
PH1: 'a placeholder',
162+
PH2: 'another placeholder',
163+
});
157164
```
158165

159166
### i18nLazyString
@@ -221,7 +228,7 @@ This call is a named cast. Use it in places where a localized string is expected
221228
but the term you want to use does not require translation. Instead of locking
222229
the whole phrase or using a placeholder-only phrase, use `lockedString`.
223230
224-
```javascript
231+
```js
225232
someFunctionRequiringALocalizedString(i18n.i18n.lockedString('HTTP'));
226233
```
227234
@@ -239,7 +246,7 @@ const UIStrings = {
239246
* @description Tooltip text that appears when hovering over the 'Focusable' attribute name under the Computed Properties section in the Accessibility pane of the Elements pane.
240247
*/
241248
computedPropertyTooltip: 'If true, this element can receive focus.',
242-
};
249+
} as const;
243250
```
244251
245252
**Bad description**:
@@ -250,39 +257,39 @@ const UIStrings = {
250257
* @description Elements pane 'Focusable' tooltip.
251258
*/
252259
computedPropertyTooltip: 'If true, this element can receive focus.',
253-
};
260+
} as const;
254261
```
255262
256263
### What information should I provide in the message description?
257264
258-
- The type of UI element where the text is displayed. Is it regular text, a
259-
label, button text, a tooltip, a link, or an accessible label? Button text
260-
is often imperative i.e. a command to do something, which is important to
261-
know in some languages.
262-
- *When*: What triggers the string and/or what is the result? What page or
263-
text comes before and after? e.g. "Status text while waiting for X", "Shown
264-
when the audit is finished and X error was encountered".
265-
- What do the placeholders stand for? Placeholder examples are sent to
266-
translators, but extra information in the description will help too. e.g.
267-
"Total time in ms that the profile took to complete", "The CSS property name
268-
that is being edited"
269-
- Is this a verb or a noun? Many words in English can be both, e.g. 'request',
270-
'address', 'change', 'display', 'increase'. Particularly if the string is
271-
short, this can be hard to guess. If it's an adjective, what does it refer
272-
to? This is important for inflection in some languages, where the ending of
273-
the adjective must change for gender or case.
274-
- Explain or name any complex terms, e.g. "Trust Tokens are a web API -
275-
https://web.dev/trust-tokens/"
276-
- Where is the text located? e.g. A table header in the Sources panel, a
277-
context-menu item in the Network panel. Many strings in the code base have
278-
*only* the location, which is not the most important context.
265+
- The type of UI element where the text is displayed. Is it regular text, a
266+
label, button text, a tooltip, a link, or an accessible label? Button text
267+
is often imperative i.e. a command to do something, which is important to
268+
know in some languages.
269+
- _When_: What triggers the string and/or what is the result? What page or
270+
text comes before and after? e.g. "Status text while waiting for X", "Shown
271+
when the audit is finished and X error was encountered".
272+
- What do the placeholders stand for? Placeholder examples are sent to
273+
translators, but extra information in the description will help too. e.g.
274+
"Total time in ms that the profile took to complete", "The CSS property name
275+
that is being edited"
276+
- Is this a verb or a noun? Many words in English can be both, e.g. 'request',
277+
'address', 'change', 'display', 'increase'. Particularly if the string is
278+
short, this can be hard to guess. If it's an adjective, what does it refer
279+
to? This is important for inflection in some languages, where the ending of
280+
the adjective must change for gender or case.
281+
- Explain or name any complex terms, e.g. "Trust Tokens are a web API -
282+
https://web.dev/trust-tokens/"
283+
- Where is the text located? e.g. A table header in the Sources panel, a
284+
context-menu item in the Network panel. Many strings in the code base have
285+
_only_ the location, which is not the most important context.
279286
280287
## How to prevent a term being localized
281288
282289
Any text within the backticks will not be translated. For example, if the
283290
'robots.txt' in string 'Requesting for robots.txt …' should not be translated:
284291
285-
```javascript
292+
```js
286293
// in example.js file
287294

288295
import * as i18n from '../i18n/i18n.js';
@@ -291,7 +298,7 @@ const UIStrings = {
291298
* @description Example description. Note: "robots.txt" is a canonical filename and should not be translated.
292299
*/
293300
requestMessage: 'Requesting for `robots.txt` …',
294-
};
301+
} as const;
295302
const str_ = i18n.i18n.registerUIStrings('example.js', UIStrings);
296303

297304
const message = i18nString(UIStrings.requestMessage);

0 commit comments

Comments
 (0)