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).
Basically, whenever a translatable string is introduced, you should call either _t() immediately OR _td() and later _t().
Example:
// Module-level constant
const COLORS = {
'#f8481c': _td('reddish orange'), // Can't call _t() here yet
'#fc2647': _td('pinky red') // Use _td() instead so the text is picked up for translation anyway
}
// Function that is called some time after i18n has been loaded
function getColorName(hex) {
return _t(COLORS[hex]); // Perform actual translation here
}
import { _t } from 'matrix-react-sdk/lib/languageHandler'; is present. If not add it to the other import statements. Also import _td if needed._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.yarn i18n to update src/i18n/strings/en_EN.jsonsrc/i18n/strings/en_EN.json (remeber to edit the one in the same project as the source file containing your new translation)._t() and _td() in the JSX files.yarn i18n to update src/i18n/strings/en_EN.json. (Be sure to run this in the same project as the JSX files you just edited.)yarn prunei18n to remove the old string from src/i18n/strings/*.json._t() call. Instead of _t(STRING) use _t(STRING, {})_t for example _t(STRING, {variable: this.variable})%(variable)s. Please note the s at the end. The name of the variable has to match the previous used name.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)._t('<a>Click here!</a>', {}, { 'a': (sub) => <a>{sub}</a> }). If you don't do the tag substitution you will end up showing literally '' rather than making a hyperlink._t('Your email address is %(emailAddress)s', { emailAddress: <i>{userEmailAddress}</i> })._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._td() instead and perform the actual translation later.