Skip to content

Commit b661c7c

Browse files
authored
Merge pull request #127 from appnexus/fix-localization
Fix localization bug when locales have uppercase letters
2 parents 3611a53 + 8a5d1ee commit b661c7c

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

src/lib/localize.js

+26-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function findLocale() {
1515

1616

1717
export class Localize {
18-
constructor(localizedData = {...translations, ...config.localization}) {
18+
constructor(localizedData = { ...translations, ...config.localization }) {
1919
const localizedMap = this.processLocalized(localizedData);
2020
const currentLocal = findLocale();
2121
const [language] = currentLocal.split('-');
@@ -30,15 +30,26 @@ export class Localize {
3030
};
3131

3232
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+
3445
return locales.reduce((acc, locale) => {
35-
const [language] = locale.toLowerCase().split('-');
46+
const [language] = locale.split('-');
3647
return {
3748
...acc,
3849
[locale]: {
3950
...acc[locale],
40-
...this.flattenObject(data[language]),
41-
...this.flattenObject(data[locale])
51+
...this.flattenObject(localizedData[language]),
52+
...this.flattenObject(localizedData[locale])
4253
}
4354
};
4455
}, {});
@@ -48,16 +59,18 @@ export class Localize {
4859
const flattened = {};
4960

5061
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];
5466

55-
if (typeof val === 'object') {
56-
return flatten(val, prop);
57-
}
67+
if (typeof val === 'object') {
68+
return flatten(val, prop);
69+
}
5870

59-
flattened[prop] = val;
60-
});
71+
flattened[prop] = val;
72+
});
73+
}
6174
}
6275

6376
flatten(data);

src/lib/localize.test.js

+31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable max-nested-callbacks */
22
import { expect } from 'chai';
33
import {Localize} from './localize';
4+
import config from './config';
45

56
describe('localize', () => {
67

@@ -28,5 +29,35 @@ describe('localize', () => {
2829
});
2930

3031

32+
it('applies specific locale', () => {
33+
config.update({
34+
forceLocale: 'es-es'
35+
});
36+
const localization = {
37+
'es-es': {
38+
prop1: 'new prop1',
39+
}
40+
};
41+
const localize = new Localize(localization);
42+
expect(localize.localizedValues).deep.equal({
43+
'prop1': 'new prop1',
44+
});
45+
});
46+
47+
it('ignores locale casing', () => {
48+
config.update({
49+
forceLocale: 'es'
50+
});
51+
const localization = {
52+
ES: {
53+
prop1: 'new prop1',
54+
}
55+
};
56+
const localize = new Localize(localization);
57+
expect(localize.localizedValues).deep.equal({
58+
'prop1': 'new prop1',
59+
});
60+
});
61+
3162

3263
});

0 commit comments

Comments
 (0)