Skip to content

Commit cb235a2

Browse files
authored
Merge pull request element-hq#5613 from pafcu/simplify-translation
Refactor translations
2 parents 53a793b + 1e2e439 commit cb235a2

5 files changed

Lines changed: 40 additions & 39 deletions

File tree

‎docs/translating-dev.md‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ function getColorName(hex) {
3636
## Adding variables inside a string.
3737

3838
1. Extend your ``_t()`` call. Instead of ``_t(STRING)`` use ``_t(STRING, {})``
39-
2. Decide how to name it. Please think about if the person who has to translate it can understand what it does.
40-
3. Add it to the array in ``_t`` for example ``_t(STRING, {variable: this.variable})``
41-
4. Add the variable inside the string. The syntax for variables is ``%(variable)s``. Please note the s at the end. The name of the variable has to match the previous used name.
39+
1. Decide how to name it. Please think about if the person who has to translate it can understand what it does. E.g. using the name 'recipient' is bad, because a translator does not know if it is the name of a person, an email address, a user ID, etc. Rather use e.g. recipientEmailAddress.
40+
1. Add it to the array in ``_t`` for example ``_t(STRING, {variable: this.variable})``
41+
1. Add the variable inside the string. The syntax for variables is ``%(variable)s``. Please note the _s_ at the end. The name of the variable has to match the previous used name.
42+
43+
- You can use the special ``count`` variable to choose between multiple versions of the same string, in order to get the correct pluralization. E.g. ``_t('You have %(count)s new messages', { count: 2 })`` would show 'You have 2 new messages', while ``_t('You have %(count)s new messages', { count: 1 })`` would show 'You have one new message' (assuming a singular version of the string has been added to the translation file. See above). Passing in ``count`` is much prefered over having an if-statement choose the correct string to use, because some languages have much more complicated plural rules than english (e.g. they might need a completely different form if there are three things rather than two).
44+
- If you want to translate text that includes e.g. hyperlinks or other HTML you have to also use tag substitution, e.g. ``_t('<a>Click here!</a>', {}, { 'a': (sub) => <a>{sub}</a> })``. If you don't do the tag substitution you will end up showing literally '<a>' rather than making a hyperlink.
45+
- You can also use React components with normal variable substitution if you want to insert HTML markup, e.g. ``_t('Your email address is %(emailAddress)s', { emailAddress: <i>{userEmailAddress}</i> })``.
4246

4347
## Things to know/Style Guides
4448

@@ -47,3 +51,5 @@ function getColorName(hex) {
4751
- 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.
4852
- 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.
4953
- 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.
54+
- Translation 'smell test': If you have a string that does not begin with a capital letter (is not the start of a sentence) or it ends with e.g. ':' or a preposition (e.g. 'to') you should recheck that you are not trying to translate a partial sentence.
55+
- If you have multiple strings, that are almost identical, except some part (e.g. a word or two) it is still better to translate the full sentence multiple times. It may seem like inefficient repetion, but unlike programming where you try to minimize repetition, translation is much faster if you have many, full, clear, sentences to work with, rather than fewer, but incomplete sentence fragments.

‎src/components/structures/CompatibilityPage.js‎

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
'use strict';
1818

1919
var React = require('react');
20-
import { _t, _tJsx } from 'matrix-react-sdk/lib/languageHandler';
20+
import { _t } from 'matrix-react-sdk/lib/languageHandler';
2121

2222
module.exports = React.createClass({
2323
displayName: 'CompatibilityPage',
@@ -40,30 +40,24 @@ module.exports = React.createClass({
4040
return (
4141
<div className="mx_CompatibilityPage">
4242
<div className="mx_CompatibilityPage_box">
43-
<p>{ _tJsx("Sorry, your browser is <b>not</b> able to run Riot.", /<b>(.*?)<\/b>/, (sub) => <b>{sub}</b>) } </p>
43+
<p>{ _t("Sorry, your browser is <b>not</b> able to run Riot.", {}, { 'b': (sub) => <b>{sub}</b> }) } </p>
4444
<p>
4545
{ _t("Riot uses many advanced browser features, some of which are not available or experimental in your current browser.") }
4646
</p>
4747
<p>
48-
{ _tJsx('Please install <a href="https://www.google.com/chrome">Chrome</a> or <a href="https://getfirefox.com">Firefox</a> for the best experience.',
49-
[
50-
/<a href="https:\/\/www.google.com\/chrome">(.*?)<\/a>/,
51-
/<a href="https:\/\/getfirefox.com">(.*?)<\/a>/,
52-
],
53-
[
54-
(sub) => <a href="https://www.google.com/chrome">{sub}</a>,
55-
(sub) => <a href="https://getfirefox.com">{sub}</a>,
56-
]
48+
{ _t('Please install <chromeLink>Chrome</chromeLink> or <firefoxLink>Firefox</firefoxLink> for the best experience.',
49+
{},
50+
{
51+
'chromeLink': (sub) => <a href="https://www.google.com/chrome">{sub}</a>,
52+
'firefoxLink': (sub) => <a href="https://getfirefox.com">{sub}</a>,
53+
},
5754
)}
58-
{ _tJsx('<a href="http://apple.com/safari">Safari</a> and <a href="http://opera.com">Opera</a> work too.',
59-
[
60-
/<a href="http:\/\/apple\.com\/safari">(.*?)<\/a>/,
61-
/<a href="http:\/\/opera\.com">(.*?)<\/a>/,
62-
],
63-
[
64-
(sub) => <a href="http://apple.com/safari">{sub}</a>,
65-
(sub) => <a href="http://opera.com">{sub}</a>,
66-
]
55+
{ _t('<safariLink>Safari</safariLink> and <operaLink>Opera</operaLink> work too.',
56+
{},
57+
{
58+
'safariLink': (sub) => <a href="http://apple.com/safari">{sub}</a>,
59+
'operaLink': (sub) => <a href="http://opera.com">{sub}</a>,
60+
},
6761
)}
6862
</p>
6963
<p>

‎src/components/views/globals/PasswordNagBar.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import React from 'react';
2020
import sdk from 'matrix-react-sdk';
2121
import Modal from 'matrix-react-sdk/lib/Modal';
2222
import dis from 'matrix-react-sdk/lib/dispatcher';
23-
import { _t, _tJsx } from 'matrix-react-sdk/lib/languageHandler';
23+
import { _t } from 'matrix-react-sdk/lib/languageHandler';
2424

2525
export default React.createClass({
2626
onUpdateClicked: function() {
@@ -49,10 +49,10 @@ export default React.createClass({
4949
alt="Warning"
5050
/>
5151
<div className="mx_MatrixToolbar_content">
52-
{ _tJsx(
52+
{ _t(
5353
"To return to your account in future you need to <u>set a password</u>",
54-
/<u>(.*?)<\/u>/,
55-
(sub) => { return <u>{ sub }</u>; },
54+
{},
55+
{ 'u': (sub) => <u>{ sub }</u> },
5656
) }
5757
</div>
5858
<button className="mx_MatrixToolbar_action">

‎src/components/views/settings/Notifications.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
import React from 'react';
1818
import Promise from 'bluebird';
1919
import sdk from 'matrix-react-sdk';
20-
import { _t, _tJsx } from 'matrix-react-sdk/lib/languageHandler';
20+
import { _t } from 'matrix-react-sdk/lib/languageHandler';
2121
import MatrixClientPeg from 'matrix-react-sdk/lib/MatrixClientPeg';
2222
import UserSettingsStore from 'matrix-react-sdk/lib/UserSettingsStore';
2323
import SettingsStore, {SettingLevel} from "matrix-react-sdk/lib/settings/SettingsStore";
@@ -564,10 +564,11 @@ module.exports = React.createClass({
564564
"vectorRuleId": "_keywords",
565565
"description" : (
566566
<span>
567-
{ _tJsx('Messages containing <span>keywords</span>',
568-
/<span>(.*?)<\/span>/,
569-
(sub) =>
567+
{ _t('Messages containing <span>keywords</span>',
568+
{},
569+
{ 'span': (sub) =>
570570
<span className="mx_UserNotifSettings_keywords" onClick={ self.onKeywordsClicked }>{sub}</span>
571+
},
571572
)}
572573
</span>
573574
),

‎src/i18n/strings/en_EN.json‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
"Away": "Away",
7373
"A new version of Riot is available.": "A new version of Riot is available.",
7474
"To return to your account in future you need to <u>set a password</u>": "To return to your account in future you need to <u>set a password</u>",
75-
"Toolbox": "Toolbox",
7675
"Set Password": "Set Password",
7776
"Error encountered (%(errorDetail)s).": "Error encountered (%(errorDetail)s).",
7877
"Checking for an update...": "Checking for an update...",
@@ -100,18 +99,19 @@
10099
"Unavailable": "Unavailable",
101100
"Changelog": "Changelog",
102101
"Back": "Back",
102+
"Send Custom Event": "Send Custom Event",
103103
"You must specify an event type!": "You must specify an event type!",
104104
"Event sent!": "Event sent!",
105105
"Failed to send custom event.": "Failed to send custom event.",
106106
"Event Type": "Event Type",
107-
"Event Content": "Event Content",
108107
"State Key": "State Key",
109-
"Edit": "Edit",
110-
"Filter results": "Filter results",
111-
"Send Custom Event": "Send Custom Event",
108+
"Event Content": "Event Content",
112109
"Send Account Data": "Send Account Data",
113-
"Explore Account Data": "Explore Account Data",
110+
"Filter results": "Filter results",
114111
"Explore Room State": "Explore Room State",
112+
"Edit": "Edit",
113+
"Explore Account Data": "Explore Account Data",
114+
"Toolbox": "Toolbox",
115115
"Developer Tools": "Developer Tools",
116116
"You have successfully set a password!": "You have successfully set a password!",
117117
"You have successfully set a password and an email address!": "You have successfully set a password and an email address!",
@@ -149,8 +149,8 @@
149149
"Direct Chat": "Direct Chat",
150150
"Sorry, your browser is <b>not</b> able to run Riot.": "Sorry, your browser is <b>not</b> able to run Riot.",
151151
"Riot uses many advanced browser features, some of which are not available or experimental in your current browser.": "Riot uses many advanced browser features, some of which are not available or experimental in your current browser.",
152-
"Please install <a href=\"https://www.google.com/chrome\">Chrome</a> or <a href=\"https://getfirefox.com\">Firefox</a> for the best experience.": "Please install <a href=\"https://www.google.com/chrome\">Chrome</a> or <a href=\"https://getfirefox.com\">Firefox</a> for the best experience.",
153-
"<a href=\"http://apple.com/safari\">Safari</a> and <a href=\"http://opera.com\">Opera</a> work too.": "<a href=\"http://apple.com/safari\">Safari</a> and <a href=\"http://opera.com\">Opera</a> work too.",
152+
"Please install <chromeLink>Chrome</chromeLink> or <firefoxLink>Firefox</firefoxLink> for the best experience.": "Please install <chromeLink>Chrome</chromeLink> or <firefoxLink>Firefox</firefoxLink> for the best experience.",
153+
"<safariLink>Safari</safariLink> and <operaLink>Opera</operaLink> work too.": "<safariLink>Safari</safariLink> and <operaLink>Opera</operaLink> work too.",
154154
"With your current browser, the look and feel of the application may be completely incorrect, and some or all features may not function. If you want to try it anyway you can continue, but you are on your own in terms of any issues you may encounter!": "With your current browser, the look and feel of the application may be completely incorrect, and some or all features may not function. If you want to try it anyway you can continue, but you are on your own in terms of any issues you may encounter!",
155155
"I understand the risks and wish to continue": "I understand the risks and wish to continue",
156156
"Couldn't load home page": "Couldn't load home page",

0 commit comments

Comments
 (0)