You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 5db44bd
Browse filesBrowse the repository at this point in the historyBrowse files
Luke Barnard
committed
Merge branch 'develop' into TheLastProject-reverse-video
Copy file name to clipboardExpand all lines: docs/translating-dev.md
+28-5Lines changed: 28 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,11 +6,32 @@
6
6
- Be able to understand English
7
7
- Be able to understand the language you want to translate riot-web into
8
8
9
+
## Translating strings vs. marking strings for translation
10
+
11
+
Translating strings are done with the `_t()` function found in matrix-react-sdk/lib/languageHandler.js. It is recommended to call this function wherever you introduce a string constant which should be translated. However, translating can not be performed until after the translation system has been initialized. Thus, sometimes translation must be performed at a different location in the source code than where the string is introduced. This breaks some tooling and makes it difficult to find translatable strings. Therefore, there is the alternative `_td()` function which is used to mark strings for translation, without actually performing the translation (which must still be performed separately, and after the translation system has been initialized).
12
+
13
+
Basically, whenever a translatable string is introduced, you should call either `_t()` immediately OR `_td()` and later `_t()`.
14
+
15
+
Example:
16
+
```
17
+
// Module-level constant
18
+
const COLORS = {
19
+
'#f8481c': _td('reddish orange'), // Can't call _t() here yet
20
+
'#fc2647': _td('pinky red') // Use _td() instead so the text is picked up for translation anyway
21
+
}
22
+
23
+
// Function that is called some time after i18n has been loaded
24
+
function getColorName(hex) {
25
+
return _t(COLORS[hex]); // Perform actual translation here
26
+
}
27
+
```
28
+
9
29
## Adding new strings
10
30
11
-
1. Check if the import ``import { _t } from 'matrix-react-sdk/lib/languageHandler';`` is present. If not add it to the other import statements.
12
-
2. Add ``_t()`` to your string. (Don't forget curly braces when you assign an expression to JSX attributes in the render method)
13
-
3. Add the String to the ``en_EN.json`` file in ``src/i18n/strings`` (respect which repository you are on).
31
+
1. Check if the import ``import { _t } from 'matrix-react-sdk/lib/languageHandler';`` is present. If not add it to the other import statements. Also import `_td` if needed.
32
+
1. Add ``_t()`` to your string. (Don't forget curly braces when you assign an expression to JSX attributes in the render method). If the string is introduced at a point before the translation system has not yet been initialized, use `_td()` instead, and call `_t()` at the appropriate time.
33
+
1. Run `npm run i18n` to update ``src/i18n/strings/en_EN.json`` (if it fails because it can't find the script, your dev environment predates the script, so reinstall/link react-sdk with `npm link ../matrix-react-sdk`). If it segfaults, you may be on Node 6, so try a newer version of node.
34
+
1. If you added a string with a plural, you can add other English plural variants to ``src/i18n/strings/en_EN.json`` (remeber to edit the one in the same project as the source file containing your new translation).
14
35
15
36
## Adding variables inside a string.
16
37
@@ -21,6 +42,8 @@
21
42
22
43
## Things to know/Style Guides
23
44
24
-
- Do not use it inside ``getDefaultProps`` at the point where ``getDefaultProps`` is initialized the translations aren't loaded yet and it causes missing translations.
25
-
- If using translated strings as constants, translated strings can't be in constants loaded at class-load time since the translations won't be loaded.
45
+
- Do not use `_t()` inside ``getDefaultProps``: the translations aren't loaded when `getDefaultProps` is called, leading to missing translations. Use `_td()` to indicate that `_t()` will be called on the string later.
46
+
- If using translated strings as constants, translated strings can't be in constants loaded at class-load time since the translations won't be loaded. Mark the strings using `_td()` instead and perform the actual translation later.
26
47
- If a string is presented in the UI with punctuation like a full stop, include this in the translation strings, since punctuation varies between languages too.
48
+
- Avoid "translation in parts", i.e. concatenating translated strings or using translated strings in variable substitutions. Context is important for translations, and translating partial strings this way is simply not always possible.
49
+
- Concatenating strings often also introduces an implicit assumption about word order (e.g. that the subject of the sentence comes first), which is incorrect for many languages.
0 commit comments