Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added default values to the dictionary and warn about missing keys #651

Merged
merged 5 commits into from
Oct 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ export function emitAuthorizationErrorEvent(m, error) {
}

export function emitUnrecoverableErrorEvent(m, error) {
emitEvent(m, "unrecoverable_error", error)
emitEvent(m, "unrecoverable_error", error);
}

export function showBadge(m) {
Expand Down
29 changes: 21 additions & 8 deletions src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function group(m, keyPath) {
export function initI18n(m) {
const language = l.ui.language(m);
const overrides = l.ui.dict(m);
const defaultDictionary = Immutable.fromJS(enDictionary);

let base = languageDictionaries[language] || Map({});

Expand All @@ -37,24 +38,36 @@ export function initI18n(m) {
successFn: (m, result) => {
registerLanguageDictionary(language, result);

const overrided = Immutable.fromJS(result).mergeDeep(overrides);

assertLanguage(m, overrided.toJS(), enDictionary);

return set(
m,
"strings",
languageDictionaries[language].mergeDeep(overrides)
defaultDictionary.mergeDeep(overrided)
);
}
});
} else {
assertLanguage(m, base.toJS(), enDictionary);
}

if (!base.has("title")) {
base = base.set("title", enDictionary.title);
}
base = defaultDictionary.mergeDeep(base).mergeDeep(overrides);

if (!base.has("unrecoverableError")) {
base = base.set("unrecoverableError", enDictionary.unrecoverableError);
}
return set(m, "strings", base);
}

return set(m, "strings", base.mergeDeep(overrides));
function assertLanguage(m, language, base, path = "") {
Object.keys(base).forEach( key => {
if (!language.hasOwnProperty(key)) {
l.warn(m, `language does not have property ${path}${key}`);
} else {
if (typeof base[key] === 'object') {
assertLanguage(m, language[key], base[key], `${path}${key}.`);
}
}
});
}

// sync
Expand Down
54 changes: 54 additions & 0 deletions test/i18n.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import expect from 'expect.js';
import Immutable from 'immutable';
import {initI18n} from '../src/i18n';
import { go } from '../src/sync';
import { swap, setEntity, updateEntity, observe } from '../src/store/index';
import { dataFns } from '../src/utils/data_utils';

import enDictionary from '../src/i18n/en';
import esDictionary from '../src/i18n/es';

const core = dataFns(["core"]);

describe("load i18n configuration", function() {

it("should merge and warn missing keys", function(done) {

const id = 1;

var m = Immutable.fromJS({
languageBaseUrl: "https://cdn.auth0.com",
ui: {
disableWarnings: true,
language: "es"
}
});

go(id);

observe("test", id, (m) => {
if (m.getIn(['sync', 'i18n', 'syncStatus']) === 'ok') {
assertLanguage(m.getIn(['i18n', 'strings']).toJS(), enDictionary, esDictionary)
done();
}
});

m = core.init(id, m);

m = initI18n(m);

m = swap(setEntity, "lock", id, m);
});
});

function assertLanguage(language, en, es) {
Object.keys(en).forEach( (key) => {
expect(language).to.have.property(key);
if (typeof en[key] === 'object') {
assertLanguage(language[key], en[key], es[key]);
}
else {
expect([en[key], es[key]]).to.contain(language[key]);
}
});
}