File size: 1,760 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import i18n, { I18N, translate } from 'i18n-calypso';

let defaultUntranslatedPlacehoder = translate( "I don't understand" );

// keep `defaultUntranslatedPlacehoder` in sync with i18n changes
i18n.subscribe( () => {
	defaultUntranslatedPlacehoder = translate( "I don't understand" );
} );

function encodeUntranslatedString( originalString, placeholder = defaultUntranslatedPlacehoder ) {
	let output = placeholder;

	while ( output.length < originalString.length ) {
		output += ' ' + placeholder;
	}

	return output.substr( 0, originalString.length );
}

let isActive = false;
let isInitialized = false;

export function toggleLanguageEmpathyMode( state ) {
	isActive = typeof state === 'boolean' ? state : ! isActive;

	if ( ! isInitialized && isActive ) {
		initLanguageEmpathyMode();
	}

	i18n.reRenderTranslations();
}

export function getLanguageEmpathyModeActive() {
	return isActive;
}

export function initLanguageEmpathyMode() {
	const i18nEmpathy = new I18N();
	const i18nEmpathyTranslate = i18nEmpathy.translate.bind( i18nEmpathy );
	const i18nEmpathyRegisterHook = i18nEmpathy.registerTranslateHook.bind( i18nEmpathy );
	const availableEmpathyTranslations = [ defaultUntranslatedPlacehoder ];

	i18n.translateHooks.forEach( i18nEmpathyRegisterHook );

	// wrap translations from i18n
	i18n.registerTranslateHook( ( translation, options ) => {
		const locale = i18n.getLocaleSlug();
		if (
			! isActive ||
			locale === i18n.defaultLocaleSlug ||
			availableEmpathyTranslations.includes( options.original )
		) {
			return translation;
		}

		if ( i18n.hasTranslation( options ) ) {
			return i18nEmpathyTranslate( options );
		}
		return '👉 ' + encodeUntranslatedString( options.original );
	} );

	isInitialized = true;
	isActive = true;
}