-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.jsx
163 lines (133 loc) · 4.57 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* External dependencies
*/
import React, { Component } from 'react';
import i18n, { localize } from 'i18n-calypso';
import debugModule from 'debug';
import { find, isEmpty } from 'lodash';
/**
* Internal dependencies
*/
import Translatable from './translatable';
import { languages } from 'languages';
import userSettings from 'lib/user-settings';
import { isCommunityTranslatorEnabled } from 'components/community-translator/utils';
/**
* Style dependencies
*/
import './style.scss';
/**
* Local variables
*/
const debug = debugModule( 'calypso:community-translator' );
class CommunityTranslator extends Component {
languageJson = null;
currentLocale = null;
initialized = false;
componentDidMount() {
this.setLanguage();
// wrap translations from i18n
i18n.registerTranslateHook( ( translation, options ) =>
this.wrapTranslation( options.original, translation, options )
);
// callback when translated component changes.
// the callback is overwritten by the translator on load/unload, so we're returning it within an anonymous function.
i18n.registerComponentUpdateHook( () => {} );
i18n.on( 'change', this.refresh );
userSettings.on( 'change', this.refresh );
}
componentWillUnmount() {
i18n.off( 'change', this.refresh );
userSettings.removeListener( 'change', this.refresh );
}
setLanguage() {
this.languageJson = i18n.getLocale() || { '': {} };
// The '' here is a Jed convention used for storing configuration data
// alongside translations in the same dictionary (because '' will never
// be a legitimately translatable string)
// See https://messageformat.github.io/Jed/
const { localeSlug, localeVariant } = this.languageJson[ '' ];
this.localeCode = localeVariant || localeSlug;
this.currentLocale = find( languages, lang => lang.langSlug === this.localeCode );
}
refresh = () => {
if ( this.initialized ) {
return;
}
if ( ! userSettings.getSettings() ) {
debug( 'initialization failed because userSettings are not ready' );
return;
}
if ( ! isCommunityTranslatorEnabled() ) {
debug( 'not initializing, not enabled' );
return;
}
this.setLanguage();
if ( ! this.localeCode || ! this.languageJson ) {
debug( 'trying to initialize translator without loaded language' );
return;
}
debug( 'Successfully initialized' );
this.initialized = true;
};
/**
* Wraps translation in a DOM object and attaches `toString()` method in case in can't be rendered
* @param { String } originalFromPage - original string
* @param { String } displayedTranslationFromPage - translated string
* @param { Object } optionsFromPage - i18n.translate options
* @returns {Object} DOM object
*/
wrapTranslation( originalFromPage, displayedTranslationFromPage, optionsFromPage ) {
if ( ! isCommunityTranslatorEnabled() ) {
return displayedTranslationFromPage;
}
if ( 'object' !== typeof optionsFromPage ) {
optionsFromPage = {};
}
if ( 'string' !== typeof originalFromPage ) {
debug( 'unknown original format' );
return displayedTranslationFromPage;
}
if ( optionsFromPage.textOnly ) {
debug( `respecting textOnly for string "${ originalFromPage }"` );
return displayedTranslationFromPage;
}
const props = {
singular: originalFromPage,
locale: this.currentLocale,
};
let key = originalFromPage;
// Has Context
if ( 'string' === typeof optionsFromPage.context ) {
props.context = optionsFromPage.context;
// see how Jed defines \u0004 as the delimiter here: https://messageformat.github.io/Jed/
key = `${ optionsFromPage.context }\u0004${ originalFromPage }`;
}
// Has Plural
if ( 'string' === typeof optionsFromPage.plural ) {
props.plural = optionsFromPage.plural;
}
// Has no translation in current locale
// Must be a string to be a valid DOM attribute value
if ( isEmpty( this.languageJson[ key ] ) ) {
props.untranslated = 'true';
}
// <Translatable> returns a frozen object, therefore we make a copy so that we can modify it below
const translatableElement = Object.assign(
{},
<Translatable { ...props }>{ displayedTranslationFromPage }</Translatable>
);
// now we can override the toString function which would otherwise return [object Object]
translatableElement.toString = () => {
// here we can store the strings that cannot be rendered to the page
return displayedTranslationFromPage;
};
// freeze the object again to certify the same behavior as the original ReactElement object
Object.freeze( translatableElement );
return translatableElement;
}
render() {
return null;
}
}
export default localize( CommunityTranslator );