1
- /**
2
- * The i18n module relies on react-intl and re-exports all of that package's exports.
3
- *
4
- * @module I18n
5
- *
6
- *
7
- */
8
-
9
1
import PropTypes from 'prop-types' ;
10
2
import { addLocaleData } from 'react-intl' ;
11
3
import arLocale from 'react-intl/locale-data/ar' ;
@@ -72,6 +64,7 @@ let messages = null;
72
64
73
65
/**
74
66
*
67
+ * @ignore
75
68
* @returns {LoggingService }
76
69
*
77
70
*/
@@ -83,13 +76,13 @@ export const getLoggingService = () => loggingService;
83
76
export const LOCALE_TOPIC = 'LOCALE' ;
84
77
85
78
/**
86
- * @memberof I18n
79
+ * @memberof module: I18n
87
80
*/
88
81
export const LOCALE_CHANGED = `${ LOCALE_TOPIC } .CHANGED` ;
89
82
90
83
/**
91
84
*
92
- * @memberof I18n
85
+ * @memberof module: I18n
93
86
* @returns {Cookies }
94
87
*/
95
88
export function getCookies ( ) {
@@ -119,9 +112,11 @@ addLocaleData([
119
112
* may be 2 or more characters.
120
113
*
121
114
* @param {string } code
122
- * @memberof I18n
115
+ * @memberof module: I18n
123
116
*/
124
- export const getPrimaryLanguageSubtag = code => code . split ( '-' ) [ 0 ] ;
117
+ export function getPrimaryLanguageSubtag ( code ) {
118
+ return code . split ( '-' ) [ 0 ] ;
119
+ }
125
120
126
121
/**
127
122
* Finds the closest supported locale to the one provided. This is done in three steps:
@@ -133,9 +128,9 @@ export const getPrimaryLanguageSubtag = code => code.split('-')[0];
133
128
*
134
129
* @param {string } locale
135
130
* @returns {string }
136
- * @memberof I18n
131
+ * @memberof module: I18n
137
132
*/
138
- export const findSupportedLocale = ( locale ) => {
133
+ export function findSupportedLocale ( locale ) {
139
134
if ( messages [ locale ] !== undefined ) {
140
135
return locale ;
141
136
}
@@ -145,7 +140,7 @@ export const findSupportedLocale = (locale) => {
145
140
}
146
141
147
142
return 'en' ;
148
- } ;
143
+ }
149
144
150
145
/**
151
146
* Get the locale from the cookie or, failing that, the browser setting.
@@ -155,9 +150,9 @@ export const findSupportedLocale = (locale) => {
155
150
* @param {string } locale If a locale is provided, returns the closest supported locale. Optional.
156
151
* @throws An error if i18n has not yet been configured.
157
152
* @returns {string }
158
- * @memberof I18n
153
+ * @memberof module: I18n
159
154
*/
160
- export const getLocale = ( locale ) => {
155
+ export function getLocale ( locale ) {
161
156
if ( messages === null ) {
162
157
throw new Error ( 'getLocale called before configuring i18n. Call configure with messages first.' ) ;
163
158
}
@@ -176,38 +171,42 @@ export const getLocale = (locale) => {
176
171
// Thus the toLowerCase, for consistency.
177
172
// https://developer.mozilla.org/en-US/docs/Web/API/NavigatorLanguage/language
178
173
return findSupportedLocale ( global . navigator . language . toLowerCase ( ) ) ;
179
- } ;
174
+ }
180
175
181
176
/**
182
177
* Returns messages for the provided locale, or the user's preferred locale if no argument is
183
178
* provided.
184
179
*
185
180
* @param {string } [locale=getLocale()]
186
- * @memberof I18n
181
+ * @memberof module: I18n
187
182
*/
188
- export const getMessages = ( locale = getLocale ( ) ) => messages [ locale ] ;
183
+ export function getMessages ( locale = getLocale ( ) ) {
184
+ return messages [ locale ] ;
185
+ }
189
186
190
187
/**
191
188
* Determines if the provided locale is a right-to-left language.
192
189
*
193
190
* @param {string } locale
194
- * @memberof I18n
191
+ * @memberof module: I18n
195
192
*/
196
- export const isRtl = locale => rtlLocales . includes ( locale ) ;
193
+ export function isRtl ( locale ) {
194
+ return rtlLocales . includes ( locale ) ;
195
+ }
197
196
198
197
/**
199
198
* Handles applying the RTL stylesheet and "dir=rtl" attribute to the html tag if the current locale
200
199
* is a RTL language.
201
200
*
202
- * @memberof I18n
201
+ * @memberof module: I18n
203
202
*/
204
- export const handleRtl = ( ) => {
203
+ export function handleRtl ( ) {
205
204
if ( isRtl ( getLocale ( ) ) ) {
206
205
global . document . getElementsByTagName ( 'html' ) [ 0 ] . setAttribute ( 'dir' , 'rtl' ) ;
207
206
} else {
208
207
global . document . getElementsByTagName ( 'html' ) [ 0 ] . setAttribute ( 'dir' , 'ltr' ) ;
209
208
}
210
- } ;
209
+ }
211
210
212
211
const messagesShape = {
213
212
ar : PropTypes . objectOf ( PropTypes . string ) , // Arabic
@@ -242,7 +241,7 @@ const optionsShape = {
242
241
*
243
242
* @param {Array } [messagesArray=[]]
244
243
* @returns {Object }
245
- * @memberof I18n
244
+ * @memberof module: I18n
246
245
*/
247
246
export function mergeMessages ( messagesArray = [ ] ) {
248
247
return Array . isArray ( messagesArray ) ? merge ( { } , ...messagesArray ) : { } ;
@@ -258,9 +257,9 @@ export function mergeMessages(messagesArray = []) {
258
257
* @param {LoggingService } options.loggingService
259
258
* @param {Object } options.config
260
259
* @param {Object } options.messages
261
- * @memberof I18n
260
+ * @memberof module: I18n
262
261
*/
263
- export const configure = ( options ) => {
262
+ export function configure ( options ) {
264
263
PropTypes . checkPropTypes ( optionsShape , options , 'property' , 'i18n' ) ;
265
264
// eslint-disable-next-line prefer-destructuring
266
265
loggingService = options . loggingService ;
@@ -283,4 +282,4 @@ export const configure = (options) => {
283
282
}
284
283
285
284
handleRtl ( ) ;
286
- } ;
285
+ }
0 commit comments