Permalink
Browse files

introduce normalization and merge helpers

  • Loading branch information...
karimJWP karimJWP
karimJWP authored and karimJWP committed Sep 27, 2018
1 parent b5aeff1 commit f84a21d3554f26f379c67c8cc6ea10650d37f469
Showing with 32 additions and 27 deletions.
  1. +1 −2 karma.conf.js
  2. +1 −1 src/js/api/config.js
  3. +1 −1 src/js/api/setup-steps.js
  4. +29 −23 src/js/utils/language.js
@@ -142,8 +142,7 @@ module.exports = function(config) {
files: [
{ pattern: './node_modules/intersection-observer/intersection-observer.js' },
{ pattern: './test/index.js' },
{ pattern: './test/files/*', included: false },
{ pattern: './test/files/translations/*', included: false },
{ pattern: './test/files/**', included: false },
{ pattern: './src/js/*', included: false }
],
@@ -69,7 +69,7 @@ const Config = function(options, persisted) {
const language = getLanguage();
const { localization, intl } = allOptions;
const customLocalization = getCustomLocalization(localization, intl, language);
allOptions.localization = applyTranslation({}, customLocalization);
allOptions.localization = applyTranslation(en, customLocalization);
let config = Object.assign({}, Defaults, allOptions);
if (config.base === '.') {
@@ -105,7 +105,7 @@ export function loadTranslations(_model) {
return new Promise(resolve => {
return loadJsonTranslation(base, language)
.then(({ response }) => {
if (destroyed(_model)) {
if (destroyed(_model) || !response) {
return;
}
attributes.localization = applyTranslation(response, customLocalization);
@@ -23,10 +23,23 @@ const codeToLang = {
const langToCode = invert(codeToLang);
function formatLanguageCode(language) {
function normalizeLanguageCode(language) {
return language.substring(0, 2).toLowerCase();
}
function normalizeLanguageAndCountryCode(language) {
return language.toLowerCase().replace('-', '_');
}
function normalizeIntl(intl) {
// TODO: Country codes are generally seen in upper case, but we have yet to find documentation confirming that this is the standard.
// When the documentation is found, remove lower case support and update our docs with reference to standards.
return Object.keys(intl).reduce((obj, key) => {
obj[normalizeLanguageAndCountryCode(key)] = intl[key];
return obj;
}, {});
}
export function getLabel(language) {
if (!language) {
return;
@@ -37,7 +50,7 @@ export function getLabel(language) {
return language;
}
return codeToLang[formatLanguageCode(language)] || language;
return codeToLang[normalizeLanguageCode(language)] || language;
}
export function getCode(language) {
@@ -63,41 +76,34 @@ export function getLanguage() {
export const translatedLanguageCodes = ['ar', 'da', 'de', 'es', 'fr', 'it', 'ja', 'nb', 'nl', 'pt', 'ro', 'sv', 'tr', 'zh'];
export function isTranslationAvailable(language) {
return translatedLanguageCodes.indexOf(formatLanguageCode(language)) >= 0;
return translatedLanguageCodes.indexOf(normalizeLanguageCode(language)) >= 0;
}
export function getCustomLocalization(localization, intl, languageAndCountryCode) {
localization = localization || {};
intl = intl || {};
const languageCode = formatLanguageCode(languageAndCountryCode);
const languageAndCountryCustomization = languageCode === languageAndCountryCode ? {} :
intl[languageAndCountryCode] || intl[languageAndCountryCode.toLowerCase()] ||
intl[languageAndCountryCode.replace('-', '_')] || intl[languageAndCountryCode.toLowerCase().replace('-', '_')];
// TODO: Country codes are generally seen in upper case, but we have yet to find documentation enforcing this format.
// When the documentation is found, remove lower case support and update our docs with reference to standards.
return Object.assign({}, localization, intl[languageCode], languageAndCountryCustomization);
intl = normalizeIntl(intl || {});
return Object.assign({}, localization || {}, intl[normalizeLanguageCode(languageAndCountryCode)], intl[normalizeLanguageAndCountryCode(languageAndCountryCode)]);
}
export function isLocalizationComplete(customLocalization) {
return isDeepKeyMatch(en, customLocalization);
}
export function loadJsonTranslation(base, languageCode) {
const url = `${base}translations/${formatLanguageCode(languageCode)}.json`;
const url = `${base}translations/${normalizeLanguageCode(languageCode)}.json`;
return new Promise((resolve, reject) => {
const oncomplete = (result) => resolve(result);
const onerror = () => reject();
ajax({ url, oncomplete, onerror, responseType: 'json' });
ajax({ url, resolve, reject, responseType: 'json' });
});
}
export function applyTranslation(translationJson, customization) {
translationJson = translationJson || {};
const localization = Object.assign({}, en, translationJson, customization);
localization.errors = Object.assign({}, en.errors, translationJson.errors, customization.errors);
localization.related = Object.assign({}, en.related, translationJson.related, customization.related);
localization.sharing = Object.assign({}, en.sharing, translationJson.sharing, customization.sharing);
localization.advertising = Object.assign({}, en.advertising, translationJson.advertising, customization.advertising);
export function applyTranslation(baseLocalization, customization) {
const localization = Object.assign({}, baseLocalization, customization);
merge(localization, 'errors', baseLocalization, customization);
merge(localization, 'related', baseLocalization, customization);
merge(localization, 'sharing', baseLocalization, customization);
merge(localization, 'advertising', baseLocalization, customization);
return localization;
}
function merge(z, prop, a, b) {
z[prop] = Object.assign({}, a[prop], b[prop]);
}

0 comments on commit f84a21d

Please sign in to comment.