-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathintl.store.js
59 lines (58 loc) · 1.78 KB
/
intl.store.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
import { Store } from "lux.js";
import IntlMessageFormat from "intl-messageformat";
import merge from "lodash/merge";
import cloneDeep from "lodash/cloneDeep";
const DEFAULT_LOCALE = "en-US";
export default new Store( {
namespace: "i18n",
state: {
defaultLocale: DEFAULT_LOCALE,
currentLocale: DEFAULT_LOCALE,
translations: {}
},
handlers: {
changeLocale( currentLocale ) {
this.setState( { currentLocale } );
},
/*
Incoming translation arg should be structured like this:
{ locale : { key: translation } }
for example:
{
"en-US": {
"home.recentBoards": "Recent Boards"
}
}
*/
receiveTranslation( translations ) {
const existing = this.getState().translations;
this.setState( { translations: merge( cloneDeep( existing ), translations ) } );
}
},
getCurrentTranslations() {
const { defaultLocale, currentLocale, translations } = this.getState();
const actualLocale = currentLocale || defaultLocale;
const language = actualLocale.split( "-" )[ 0 ];
const lang = {
locale: actualLocale,
messages: translations[ actualLocale ] || translations[ language ] || {}
};
return lang;
},
getFormattedMessage( key, data, defaultValue ) {
const current = this.getCurrentTranslations();
if ( current.messages[ key ] || typeof defaultValue !== "undefined" ) {
const msg = new IntlMessageFormat( current.messages[ key ] || defaultValue, current.locale );
return msg.format( data );
}
return key;
},
getFormattedNumber( data, options ) {
const { currentLocale } = this.getState();
return new window.Intl.NumberFormat( currentLocale, options ).format( data );
},
getFormattedDate( data, options ) {
const { currentLocale } = this.getState();
return new window.Intl.DateTimeFormat( currentLocale, options ).format( data );
}
} );