Skip to content

Commit

Permalink
Rework Azure locale
Browse files Browse the repository at this point in the history
  • Loading branch information
compulim committed Apr 25, 2019
1 parent 7136e6b commit cd94ebe
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 19 deletions.
87 changes: 76 additions & 11 deletions packages/embed/src/locale.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,93 @@
const AZURE_LOCALE_PATTERN = /^([a-z]{2})(-([a-z]+))?\.([a-z]{2})-([a-z]{2})$/;
const JAVASCRIPT_LOCALE_PATTERN = /^([a-z]{2})(-([A-Za-z]+))?$/;
// Supported Azure language as of 2019-04-25
// en.en-us
// cs.cs-cz
// de.de-de
// es.es-es
// fr.fr-fr
// hu.hu-hu
// it.it-it
// ja.ja-jp
// ko.ko-kr
// nl.nl-nl
// pl.pl-pl
// pt-br.pt-br
// pt-pt.pt-pt
// ru.ru-ru
// sv.sv-se
// tr.tr-tr
// zh-hans.zh-cn
// zh-hant.zh-tw

const AZURE_LOCALE_PATTERN = /^(([a-z]{2})(-[a-z]{2,})?)\.([a-z]{2})/;
const JAVASCRIPT_LOCALE_PATTERN = /^([a-z]{2})-([A-Z]{2,})?$/;

const AZURE_LOCALE_MAPPING = {
cs: 'cs-CZ',
de: 'de-DE',
en: 'en-US',
es: 'es-ES',
fr: 'fr-FR',
hu: 'hu-HU',
it: 'it-IT',
ja: 'ja-JP',
ko: 'ko-KR',
nl: 'nl-NL',
pl: 'pl-PL',
'pt-br': 'pt-BR',
'pt-pt': 'pt-PT',
ru: 'ru-RU',
sv: 'sv-SE',
tr: 'tr-TR',
'zh-hans': 'zh-HANS',
'zh-hant': 'zh-HANT'
};

function normalize(language) {
const azureLocaleMatch = AZURE_LOCALE_PATTERN.exec(language);
const javaScriptLocaleMatch = JAVASCRIPT_LOCALE_PATTERN.exec(language);
let result;

if (javaScriptLocaleMatch) {
return language;
result = language;
} else if (azureLocaleMatch) {
return `${ azureLocaleMatch[4] }-${ azureLocaleMatch[5].toUpperCase() }`;
} else {
return 'en';
result = AZURE_LOCALE_MAPPING[azureLocaleMatch[1]];
}

return result || 'en-US';
}

function toAzureLocale(language) {
switch (language) {
case 'fr':
// This is for Firefox, which default French to "fr" instead of "fr-FR".
return 'fr.fr-fr';

case 'pt-BR':
return 'pt-br.pt-br';

case 'pt-PT':
return 'pt-pt.pt-pt';

case 'zh-CN':
case 'zh-SG':
return `zh-hans.${ language.toLowerCase() }`;

case 'zh-HANS':
return 'zh-hans.zh-cn';

case 'zh-HANT':
return 'zh-hant.zh-tw';

case 'zh-HK':
case 'zh-MO':
case 'zh-TW':
return `zh-hant.${ language.toLowerCase() }`;
}

const match = JAVASCRIPT_LOCALE_PATTERN.exec(language);

if (match) {
if (match[2]) {
return `${ match[1] }.${ match[1] }-${ match[3].toLowerCase() }`;
} else {
return match[1];
}
return `${ match[1] }.${ match[1] }-${ match[2].toLowerCase() }`;
}
}

Expand Down
113 changes: 105 additions & 8 deletions packages/embed/src/locale.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,133 @@ test('Normalizing "en.en-us"', () => {
expect(normalize('en.en-us')).toBe('en-US');
});

test('Normalize "ja.ja-jp"', () => {
test('Normalizing "cs.cs-cz"', () => {
expect(normalize('cs.cs-cz')).toBe('cs-CZ');
});

test('Normalizing "de.de-de"', () => {
expect(normalize('de.de-de')).toBe('de-DE');
});

test('Normalizing "es.es-es"', () => {
expect(normalize('es.es-es')).toBe('es-ES');
});

test('Normalizing "fr.fr-fr"', () => {
expect(normalize('fr.fr-fr')).toBe('fr-FR');
});

test('Normalizing "hu.hu-hu"', () => {
expect(normalize('hu.hu-hu')).toBe('hu-HU');
});

test('Normalizing "it.it-it"', () => {
expect(normalize('it.it-it')).toBe('it-IT');
});

test('Normalizing "ja.ja-jp"', () => {
expect(normalize('ja.ja-jp')).toBe('ja-JP');
});

test('Normalize "zh-hant.zh-hk"', () => {
expect(normalize('zh-hant.zh-hk')).toBe('zh-HK');
test('Normalizing "ko.ko-kr"', () => {
expect(normalize('ko.ko-kr')).toBe('ko-KR');
});

test('Normalizing "nl.nl-nl"', () => {
expect(normalize('nl.nl-nl')).toBe('nl-NL');
});

test('Normalizing "pl.pl-pl"', () => {
expect(normalize('pl.pl-pl')).toBe('pl-PL');
});

test('Normalizing "pt-br.pt-br"', () => {
expect(normalize('pt-br.pt-br')).toBe('pt-BR');
});

test('Normalizing "pt-pt.pt-pt"', () => {
expect(normalize('pt-pt.pt-pt')).toBe('pt-PT');
});

test('Normalizing "ru.ru-ru"', () => {
expect(normalize('ru.ru-ru')).toBe('ru-RU');
});

test('Normalizing "sv.sv-se"', () => {
expect(normalize('sv.sv-se')).toBe('sv-SE');
});

test('Normalizing "tr.tr-tr"', () => {
expect(normalize('tr.tr-tr')).toBe('tr-TR');
});

test('Normalizing "zh-hans.zh-cn"', () => {
expect(normalize('zh-hans.zh-cn')).toBe('zh-Hans');
});

test('Normalizing "zh-hant.zh-tw"', () => {
expect(normalize('zh-hant.zh-tw')).toBe('zh-Hant');
});

test('Normalizing "en.zh-hk" should become "en-US"', () => {
expect(normalize('en.zh-hk')).toBe('en-US');

});

test('Normalizing "en"', () => {
expect(normalize('en')).toBe('en');
expect(normalize('en')).toBe('en-US');
});

test('Normalizing "zh-HK"', () => {
expect(normalize('zh-HK')).toBe('zh-HK');
});

test('Normalizing "*"', () => {
expect(normalize('*')).toBe('en');
expect(normalize('*')).toBe('en-US');
});

test('Convert "en-US" to Azure locale', () => {
expect(toAzureLocale('en-US')).toBe('en.en-us');
});

test('Convert "en" to Azure locale', () => {
expect(toAzureLocale('en')).toBe('en');
test('Convert "fr" to Azure locale', () => {
expect(toAzureLocale('fr')).toBe('fr.fr-fr');
});

test('Convert "pt-BR" to Azure locale', () => {
expect(toAzureLocale('pt-BR')).toBe('pt-br.pt-br');
});

test('Convert "pt-PT" to Azure locale', () => {
expect(toAzureLocale('pt-PT')).toBe('pt-pt.pt-pt');
});

test('Convert "zh-CN" to Azure locale', () => {
expect(toAzureLocale('zh-CN')).toBe('zh-hans.zh-cn');
});

test('Convert "zh-Hant" to Azure locale', () => {
expect(toAzureLocale('zh-Hant')).toBe('zh-hant.zh-tw');
});

test('Convert "zh-Hans" to Azure locale', () => {
expect(toAzureLocale('zh-Hans')).toBe('zh-hans.zh-cn');
});

test('Convert "zh-HK" to Azure locale', () => {
expect(toAzureLocale('zh-HK')).toBe('zh.zh-hk');
expect(toAzureLocale('zh-HK')).toBe('zh-hant.zh-hk');
});

test('Convert "zh-MO" to Azure locale', () => {
expect(toAzureLocale('zh-MO')).toBe('zh-hant.zh-mo');
});

test('Convert "zh-SG" to Azure locale', () => {
expect(toAzureLocale('zh-SG')).toBe('zh-hans.zh-sg');
});

test('Convert "zh-TW" to Azure locale', () => {
expect(toAzureLocale('zh-TW')).toBe('zh-hant.zh-tw');
});

test('Convert "*" to Azure locale', () => {
Expand Down

0 comments on commit cd94ebe

Please sign in to comment.