@@ -2,80 +2,51 @@ import i18next, { t as translate } from 'i18next';
2
2
3
3
export const i18nMixin = baseClass => class extends baseClass {
4
4
5
- /**
6
- * Add translation resources
7
- * @param {String } language - The language code.
8
- * @param {String } namespace - I18next namespace.
9
- * @param {Object } resources - i18next resources.
10
- */
11
- addResources ( language , namespace , resources ) {
12
- i18next . addResources ( language , namespace , resources ) ;
13
- }
14
-
15
- /**
16
- * Read in configuration files.
17
- * @param {Array } filenames - Configuration file names
18
- * @return {Object } Configuration objects associated with the files.
19
- */
20
- async readFiles ( filenames ) {
21
- let configs = [ ] ;
22
- for ( const filename of filenames ) {
23
-
24
- console . log ( 'Reading config file ' + filename ) ;
25
- const config = await import ( filename ) ;
26
- configs . push ( config ) ;
27
- }
28
- return configs ;
29
- }
30
-
31
- /**
32
- * Detect browser language settings.
33
- * @return {String } Language code used by browser.
34
- */
35
- detectBrowserLanguage ( ) {
36
-
37
- const language = navigator . language ;
38
- console . log ( 'Detected browser language ' + language ) ;
39
- return language ;
40
- }
41
-
42
5
/**
43
6
* Initialize i18nMixin.
44
7
*
45
8
* Call this function in the firstUpdated() { .. } callback to initialize the mixin.
46
9
* After that, the translation functionality will be applicable.
47
10
*
48
- * @param {Object } resources - Options to configure mixin .
11
+ * @param {Object } resources - I18next resources object. See https://www.i18next.com/overview/configuration-options .
49
12
*/
50
- async i18nInit ( resources ) {
51
-
52
- const language = this . detectBrowserLanguage ( ) ;
13
+ i18nInit ( resources ) {
53
14
54
15
if ( ! i18next . isInitialized ) {
16
+
17
+ console . log ( 'Initializing i18n' ) ;
55
18
i18next .
56
19
init ( {
57
- lng : language ,
20
+ lng : navigator . language ,
58
21
resources,
59
22
defaultNS : 'translations' ,
60
23
ns : [ 'translations' ] ,
61
- fallbackLng : 'en-US '
24
+ fallbackLng : 'en-CA '
62
25
} )
63
26
}
64
27
65
28
i18next . on ( 'initialized' , options => {
66
29
this . requestUpdate ( )
67
30
} )
68
31
i18next . on ( 'languageChanged' , lng => {
69
- const language = this . detectBrowserLanguage ( ) ;
70
- this . changeLanguage ( language ) ;
32
+ this . changeLanguage ( lng ) ;
71
33
this . requestUpdate ( ) ;
72
34
} )
73
35
}
74
36
37
+ /**
38
+ * Change the language of i18next.
39
+ * @param {String } lang - Language code to which to switch.
40
+ */
75
41
changeLanguage ( lang ) {
76
- i18next . changeLanguage ( lang )
42
+ return i18next . changeLanguage ( lang ) ;
77
43
}
78
44
45
+ /**
46
+ * Apply the translation of key.
47
+ * @param {String } key - i18next translation key
48
+ * @returns {String } - Translated result.
49
+ */
79
50
translate ( key ) {
80
51
return translate ( key )
81
52
}
0 commit comments