-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
index.js
167 lines (153 loc) · 5.04 KB
/
index.js
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
164
165
166
167
/**
* External dependencies
*/
import Jed from 'jed';
import memoize from 'memize';
let i18n;
/**
* Log to console, once per message; or more precisely, per referentially equal
* argument set. Because Jed throws errors, we log these to the console instead
* to avoid crashing the application.
*
* @param {...*} args Arguments to pass to `console.error`
*/
const logErrorOnce = memoize( console.error ); // eslint-disable-line no-console
/**
* Merges locale data into the Jed instance by domain. Creates a new Jed
* instance if one has not yet been assigned.
*
* @see http://messageformat.github.io/Jed/
*
* @param {?Object} localeData Locale data configuration.
* @param {?string} domain Domain for which configuration applies.
*/
export function setLocaleData( localeData = { '': {} }, domain = 'default' ) {
if ( ! i18n ) {
i18n = new Jed( {
domain: 'default',
locale_data: {
default: {},
},
} );
}
i18n.options.locale_data[ domain ] = Object.assign(
{},
i18n.options.locale_data[ domain ],
localeData
);
}
/**
* Returns the current Jed instance, initializing with a default configuration
* if not already assigned.
*
* @return {Jed} Jed instance.
*/
export function getI18n() {
if ( ! i18n ) {
setLocaleData();
}
return i18n;
}
/**
* Wrapper for Jed's `dcnpgettext`, its most qualified function. Absorbs errors
* which are thrown as the result of invalid translation.
*
* @param {?string} domain Domain to retrieve the translated text.
* @param {?string} context Context information for the translators.
* @param {string} single Text to translate if non-plural. Used as fallback
* return value on a caught error.
* @param {?string} plural The text to be used if the number is plural.
* @param {?number} number The number to compare against to use either the
* singular or plural form.
*
* @return {string} The translated string.
*/
export const dcnpgettext = memoize( ( domain = 'default', context, single, plural, number ) => {
try {
return getI18n().dcnpgettext( domain, context, single, plural, number );
} catch ( error ) {
logErrorOnce( 'Jed localization error: \n\n' + error.toString() );
return single;
}
} );
/**
* Retrieve the translation of text.
*
* @see https://developer.wordpress.org/reference/functions/__/
*
* @param {string} text Text to translate.
* @param {?string} domain Domain to retrieve the translated text.
*
* @return {string} Translated text.
*/
export function __( text, domain ) {
return dcnpgettext( domain, undefined, text );
}
/**
* Retrieve translated string with gettext context.
*
* @see https://developer.wordpress.org/reference/functions/_x/
*
* @param {string} text Text to translate.
* @param {string} context Context information for the translators.
* @param {?string} domain Domain to retrieve the translated text.
*
* @return {string} Translated context string without pipe.
*/
export function _x( text, context, domain ) {
return dcnpgettext( domain, context, text );
}
/**
* Translates and retrieves the singular or plural form based on the supplied
* number.
*
* @see https://developer.wordpress.org/reference/functions/_n/
*
* @param {string} single The text to be used if the number is singular.
* @param {string} plural The text to be used if the number is plural.
* @param {number} number The number to compare against to use either the
* singular or plural form.
* @param {?string} domain Domain to retrieve the translated text.
*
* @return {string} The translated singular or plural form.
*/
export function _n( single, plural, number, domain ) {
return dcnpgettext( domain, undefined, single, plural, number );
}
/**
* Translates and retrieves the singular or plural form based on the supplied
* number, with gettext context.
*
* @see https://developer.wordpress.org/reference/functions/_nx/
*
* @param {string} single The text to be used if the number is singular.
* @param {string} plural The text to be used if the number is plural.
* @param {number} number The number to compare against to use either the
* singular or plural form.
* @param {string} context Context information for the translators.
* @param {?string} domain Domain to retrieve the translated text.
*
* @return {string} The translated singular or plural form.
*/
export function _nx( single, plural, number, context, domain ) {
return dcnpgettext( domain, context, single, plural, number );
}
/**
* Returns a formatted string. If an error occurs in applying the format, the
* original format string is returned.
*
* @param {string} format The format of the string to generate.
* @param {string[]} ...args Arguments to apply to the format.
*
* @see http://www.diveintojavascript.com/projects/javascript-sprintf
*
* @return {string} The formatted string.
*/
export function sprintf( format, ...args ) {
try {
return Jed.sprintf( format, ...args );
} catch ( error ) {
logErrorOnce( 'Jed sprintf error: \n\n' + error.toString() );
return format;
}
}