Skip to content

Commit

Permalink
Update Azure locale mapping (#2965)
Browse files Browse the repository at this point in the history
* Update Azure locale mapping

* Add entry

* Update entry

* Apply PR suggestions
  • Loading branch information
compulim authored Feb 29, 2020
1 parent 0fbbb73 commit 8a66047
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 98 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Resolves [#2213](https://github.com/microsoft/BotFramework-WebChat/issues/2213). Added customization for typing activity, by [@compulim](https://github.com/compulim), in PR [#2912](https://github.com/microsoft/BotFramework-WebChat/pull/2912)
- Resolves [#2754](https://github.com/microsoft/BotFramework-WebChat/issues/2754). Added [telemetry system](https://github.com/microsoft/BotFramework-WebChat/tree/master/docs/TELEMETRY.md), by [@compulim](https://github.com/compulim), in PR [#2922](https://github.com/microsoft/BotFramework-WebChat/pull/2922)
- Resolves [#2857](https://github.com/microsoft/BotFramework-WebChat/issues/2857). Added the ability to customize the avatar on a per activity basis, by [@compulim](https://github.com/compulim), in PR [#2943](https://github.com/microsoft/BotFramework-WebChat/pull/2943)
- Resolves [#2944](https://github.com/microsoft/BotFramework-WebChat/issues/2944). Updated Azure locale mapping in embed page, by [@compulim](https://github.com/compulim) in PR [#2965](https://github.com/microsoft/BotFramework-WebChat/pull/2965)

### Fixed

Expand Down
1 change: 1 addition & 0 deletions packages/embed/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'core-js/modules/es.array.includes';
import 'core-js/modules/es.array.iterator';
import 'core-js/modules/es.promise';
import 'core-js/modules/es.string.starts-with';
import 'core-js/modules/es.symbol';
import 'url-search-params-polyfill';
import 'whatwg-fetch';
Expand Down
96 changes: 54 additions & 42 deletions packages/embed/src/locale.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Supported Azure languages as of 2019-04-25
// Supported Azure languages as of 2020-02-28
// The first part is language (localization), the second part is regional format (internationalization)

// en.en-us
Expand All @@ -18,46 +18,51 @@
// sv.sv-se
// tr.tr-tr
// zh-hans.zh-cn
// zh-hans.zh-sg
// zh-hant.zh-hk
// zh-hant.zh-mo
// zh-hant.zh-tw

// Please note that Arabic and Hebrew are not currently supported by Azure

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_PATTERN = /^(([a-z]{2})(-[a-z]{2,})?)\.(([a-z]{2})(-[a-z]{2,})?)/;
const JAVASCRIPT_LOCALE_PATTERN = /^([a-z]{2})(-([A-Z][\w]+))*$/;

const AZURE_LOCALE_MAPPING = {
ar: 'ar-EG',
bg: 'bg-BG',
cs: 'cs-CZ',
de: 'de-DE',
en: 'en-US',
es: 'es-ES',
fr: 'fr-FR',
he: 'he-IL',
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'
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-sg': 'zh-Hans-SG', '*': 'zh-Hans' },
'zh-hant': { 'zh-hk': 'zh-Hant-HK', 'zh-mo': 'zh-Hant-MO', '*': 'zh-Hant' }
};

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

if (javaScriptLocaleMatch) {
result = language;
} else if (azureLocaleMatch) {
result = AZURE_LOCALE_MAPPING[azureLocaleMatch[1]];
if (language !== 'en') {
const azureLocaleMatch = AZURE_LOCALE_PATTERN.exec(language);
const javaScriptLocaleMatch = JAVASCRIPT_LOCALE_PATTERN.exec(language);

if (javaScriptLocaleMatch) {
result = language;
} else if (azureLocaleMatch) {
const mapping = AZURE_LOCALE_MAPPING[azureLocaleMatch[1]];

result = mapping[azureLocaleMatch[4]] || mapping['*'];
}
}

return result || 'en-US';
Expand All @@ -75,26 +80,33 @@ function toAzureLocale(language) {
case 'pt-PT':
return 'pt-pt.pt-pt';

case 'zh-CN':
case 'zh-SG':
return `zh-hans.${language.toLowerCase()}`;
case 'yue':
case 'zh-Hant-HK':
return 'zh-hant.zh-hk';

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

case 'zh-HANT':
case 'zh-Hans-SG':
return `zh-hans.zh-sg`;

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

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

const match = JAVASCRIPT_LOCALE_PATTERN.exec(language);
if (
Object.keys(AZURE_LOCALE_MAPPING).some(azureLocaleFirstPart =>
language.toLowerCase().startsWith(azureLocaleFirstPart)
)
) {
const match = JAVASCRIPT_LOCALE_PATTERN.exec(language);

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

Expand Down
134 changes: 78 additions & 56 deletions packages/embed/src/locale.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { normalize, toAzureLocale } from './locale';

test('Normalizing "ar.ar-eg"', () => {
expect(normalize('ar.ar-eg')).toBe('ar-EG');
});

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

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

test('Normalizing "cs.cs-cz"', () => {
expect(normalize('cs.cs-cz')).toBe('cs-CZ');
});
Expand All @@ -28,10 +20,6 @@ test('Normalizing "fr.fr-fr"', () => {
expect(normalize('fr.fr-fr')).toBe('fr-FR');
});

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

test('Normalizing "hu.hu-hu"', () => {
expect(normalize('hu.hu-hu')).toBe('hu-HU');
});
Expand Down Expand Up @@ -77,11 +65,23 @@ test('Normalizing "tr.tr-tr"', () => {
});

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

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

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

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

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

test('Normalizing "en.zh-hk" should become "en-US"', () => {
Expand All @@ -92,58 +92,80 @@ test('Normalizing "en"', () => {
expect(normalize('en')).toBe('en-US');
});

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

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

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

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-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', () => {
expect(toAzureLocale('*')).toEqual(undefined);
});

test('Convert all Azure supported languages to Azure locale', () => {
const expected = {
'ar-EG': undefined,
'ar-JO': undefined,
'ar-SA': undefined,
'bg-BG': undefined,
'ca-ES': undefined,
'cs-CZ': 'cs.cs-cz',
'da-DK': undefined,
'de-DE': 'de.de-de',
'el-GR': undefined,
'en-US': 'en.en-us',
'es-ES': 'es.es-es',
'et-EE': undefined,
'eu-ES': undefined,
'fi-FI': undefined,
'fr-FR': 'fr.fr-fr',
'gl-ES': undefined,
'he-IL': undefined,
'hi-IN': undefined,
'hr-HR': undefined,
'hu-HU': 'hu.hu-hu',
'id-ID': undefined,
'it-IT': 'it.it-it',
'ja-JP': 'ja.ja-jp',
'kk-KZ': undefined,
'ko-KR': 'ko.ko-kr',
'lt-LT': undefined,
'lv-LV': undefined,
'ms-MY': undefined,
'nb-NO': undefined,
'nl-NL': 'nl.nl-nl',
'pl-PL': 'pl.pl-pl',
'pt-BR': 'pt-br.pt-br',
'pt-PT': 'pt-pt.pt-pt',
'ro-RO': undefined,
'ru-RU': 'ru.ru-ru',
'sk-SK': undefined,
'sl-SI': undefined,
'sr-Cyrl': undefined,
'sr-Latn': undefined,
'sv-SE': 'sv.sv-se',
'th-TH': undefined,
'tr-TR': 'tr.tr-tr',
'uk-UA': undefined,
'vi-VN': undefined,
yue: 'zh-hant.zh-hk',
'zh-Hans': 'zh-hans.zh-cn',
'zh-Hans-SG': 'zh-hans.zh-sg',
'zh-Hant': 'zh-hant.zh-tw',
'zh-Hant-HK': 'zh-hant.zh-hk',
'zh-Hant-MO': 'zh-hant.zh-mo'
};

const actual = Object.keys(expected).reduce(
(actual, language) => ({ ...actual, [language]: toAzureLocale(language) }),
{}
);

expect(actual).toEqual(expected);
});

0 comments on commit 8a66047

Please sign in to comment.