@@ -15,7 +15,7 @@ export function findLocale() {
15
15
16
16
17
17
export class Localize {
18
- constructor ( localizedData = { ...translations , ...config . localization } ) {
18
+ constructor ( localizedData = { ...translations , ...config . localization } ) {
19
19
const localizedMap = this . processLocalized ( localizedData ) ;
20
20
const currentLocal = findLocale ( ) ;
21
21
const [ language ] = currentLocal . split ( '-' ) ;
@@ -30,15 +30,26 @@ export class Localize {
30
30
} ;
31
31
32
32
processLocalized = ( data = { } ) => {
33
- const locales = Object . keys ( data ) ;
33
+ // Lowercase top level object properties which are locale names
34
+ const [ locales , localizedData ] = Object . keys ( data ) . reduce ( ( [ locales , localeData ] , key ) => {
35
+ const locale = key . toLowerCase ( ) ;
36
+ return [
37
+ [ ...locales , locale ] ,
38
+ {
39
+ ...localeData ,
40
+ [ locale ] : data [ key ]
41
+ }
42
+ ] ;
43
+ } , [ [ ] , { } ] ) ;
44
+
34
45
return locales . reduce ( ( acc , locale ) => {
35
- const [ language ] = locale . toLowerCase ( ) . split ( '-' ) ;
46
+ const [ language ] = locale . split ( '-' ) ;
36
47
return {
37
48
...acc ,
38
49
[ locale ] : {
39
50
...acc [ locale ] ,
40
- ...this . flattenObject ( data [ language ] ) ,
41
- ...this . flattenObject ( data [ locale ] )
51
+ ...this . flattenObject ( localizedData [ language ] ) ,
52
+ ...this . flattenObject ( localizedData [ locale ] )
42
53
}
43
54
} ;
44
55
} , { } ) ;
@@ -48,16 +59,18 @@ export class Localize {
48
59
const flattened = { } ;
49
60
50
61
function flatten ( part , prefix ) {
51
- Object . keys ( part ) . forEach ( key => {
52
- const prop = prefix ? `${ prefix } .${ key } ` : key ;
53
- const val = part [ key ] ;
62
+ if ( part ) {
63
+ Object . keys ( part ) . forEach ( key => {
64
+ const prop = prefix ? `${ prefix } .${ key } ` : key ;
65
+ const val = part [ key ] ;
54
66
55
- if ( typeof val === 'object' ) {
56
- return flatten ( val , prop ) ;
57
- }
67
+ if ( typeof val === 'object' ) {
68
+ return flatten ( val , prop ) ;
69
+ }
58
70
59
- flattened [ prop ] = val ;
60
- } ) ;
71
+ flattened [ prop ] = val ;
72
+ } ) ;
73
+ }
61
74
}
62
75
63
76
flatten ( data ) ;
0 commit comments