Skip to content

Commit 8c8e2cc

Browse files
authored
Fix Azure locale in embed (#1928)
* Rework Azure locale * Update test * Update test * Add comment * Add entry
1 parent c344b4a commit 8c8e2cc

3 files changed

Lines changed: 184 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2020

2121
### Added
2222
- Added handling of reconnection, by [@compulim](https://github.com/compulim), in PR [#1880](https://github.com/Microsoft/BotFramework-WebChat/pull/1880)
23+
- Added embed page, by [@compulim](https://github.com/compulim), in PR [#1910](https://github.com/Microsoft/BotFramework-WebChat/pull/1910) and PR [#1928](https://github.com/Microsoft/BotFramework-WebChat/pull/1928)
2324

2425
### Fixed
2526
- Fix [#1423](https://github.com/Microsoft/BotFramework-WebChat/issues/1423). Added sample for hosting WebChat in Angular, by [@omarsourour](https://github.com/omarsourour) in PR [#1813](https://github.com/Microsoft/BotFramework-WebChat/pull/1813)

packages/embed/src/locale.js

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,95 @@
1-
const AZURE_LOCALE_PATTERN = /^([a-z]{2})(-([a-z]+))?\.([a-z]{2})-([a-z]{2})$/;
2-
const JAVASCRIPT_LOCALE_PATTERN = /^([a-z]{2})(-([A-Za-z]+))?$/;
1+
// Supported Azure language as of 2019-04-25
2+
// The first part is language (localization), the second part is regional format (internationalization)
3+
4+
// en.en-us
5+
// cs.cs-cz
6+
// de.de-de
7+
// es.es-es
8+
// fr.fr-fr
9+
// hu.hu-hu
10+
// it.it-it
11+
// ja.ja-jp
12+
// ko.ko-kr
13+
// nl.nl-nl
14+
// pl.pl-pl
15+
// pt-br.pt-br
16+
// pt-pt.pt-pt
17+
// ru.ru-ru
18+
// sv.sv-se
19+
// tr.tr-tr
20+
// zh-hans.zh-cn
21+
// zh-hant.zh-tw
22+
23+
const AZURE_LOCALE_PATTERN = /^(([a-z]{2})(-[a-z]{2,})?)\.([a-z]{2})/;
24+
const JAVASCRIPT_LOCALE_PATTERN = /^([a-z]{2})-([A-Z]{2,})?$/;
25+
26+
const AZURE_LOCALE_MAPPING = {
27+
cs: 'cs-CZ',
28+
de: 'de-DE',
29+
en: 'en-US',
30+
es: 'es-ES',
31+
fr: 'fr-FR',
32+
hu: 'hu-HU',
33+
it: 'it-IT',
34+
ja: 'ja-JP',
35+
ko: 'ko-KR',
36+
nl: 'nl-NL',
37+
pl: 'pl-PL',
38+
'pt-br': 'pt-BR',
39+
'pt-pt': 'pt-PT',
40+
ru: 'ru-RU',
41+
sv: 'sv-SE',
42+
tr: 'tr-TR',
43+
'zh-hans': 'zh-HANS',
44+
'zh-hant': 'zh-HANT'
45+
};
346

447
function normalize(language) {
548
const azureLocaleMatch = AZURE_LOCALE_PATTERN.exec(language);
649
const javaScriptLocaleMatch = JAVASCRIPT_LOCALE_PATTERN.exec(language);
50+
let result;
751

852
if (javaScriptLocaleMatch) {
9-
return language;
53+
result = language;
1054
} else if (azureLocaleMatch) {
11-
return `${ azureLocaleMatch[4] }-${ azureLocaleMatch[5].toUpperCase() }`;
12-
} else {
13-
return 'en';
55+
result = AZURE_LOCALE_MAPPING[azureLocaleMatch[1]];
1456
}
57+
58+
return result || 'en-US';
1559
}
1660

1761
function toAzureLocale(language) {
62+
switch (language) {
63+
case 'fr':
64+
// This is for Firefox, which default French to "fr" instead of "fr-FR".
65+
return 'fr.fr-fr';
66+
67+
case 'pt-BR':
68+
return 'pt-br.pt-br';
69+
70+
case 'pt-PT':
71+
return 'pt-pt.pt-pt';
72+
73+
case 'zh-CN':
74+
case 'zh-SG':
75+
return `zh-hans.${ language.toLowerCase() }`;
76+
77+
case 'zh-HANS':
78+
return 'zh-hans.zh-cn';
79+
80+
case 'zh-HANT':
81+
return 'zh-hant.zh-tw';
82+
83+
case 'zh-HK':
84+
case 'zh-MO':
85+
case 'zh-TW':
86+
return `zh-hant.${ language.toLowerCase() }`;
87+
}
88+
1889
const match = JAVASCRIPT_LOCALE_PATTERN.exec(language);
1990

2091
if (match) {
21-
if (match[2]) {
22-
return `${ match[1] }.${ match[1] }-${ match[3].toLowerCase() }`;
23-
} else {
24-
return match[1];
25-
}
92+
return `${ match[1] }.${ match[1] }-${ match[2].toLowerCase() }`;
2693
}
2794
}
2895

packages/embed/src/locale.spec.js

Lines changed: 105 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,133 @@ test('Normalizing "en.en-us"', () => {
44
expect(normalize('en.en-us')).toBe('en-US');
55
});
66

7-
test('Normalize "ja.ja-jp"', () => {
7+
test('Normalizing "cs.cs-cz"', () => {
8+
expect(normalize('cs.cs-cz')).toBe('cs-CZ');
9+
});
10+
11+
test('Normalizing "de.de-de"', () => {
12+
expect(normalize('de.de-de')).toBe('de-DE');
13+
});
14+
15+
test('Normalizing "es.es-es"', () => {
16+
expect(normalize('es.es-es')).toBe('es-ES');
17+
});
18+
19+
test('Normalizing "fr.fr-fr"', () => {
20+
expect(normalize('fr.fr-fr')).toBe('fr-FR');
21+
});
22+
23+
test('Normalizing "hu.hu-hu"', () => {
24+
expect(normalize('hu.hu-hu')).toBe('hu-HU');
25+
});
26+
27+
test('Normalizing "it.it-it"', () => {
28+
expect(normalize('it.it-it')).toBe('it-IT');
29+
});
30+
31+
test('Normalizing "ja.ja-jp"', () => {
832
expect(normalize('ja.ja-jp')).toBe('ja-JP');
933
});
1034

11-
test('Normalize "zh-hant.zh-hk"', () => {
12-
expect(normalize('zh-hant.zh-hk')).toBe('zh-HK');
35+
test('Normalizing "ko.ko-kr"', () => {
36+
expect(normalize('ko.ko-kr')).toBe('ko-KR');
37+
});
38+
39+
test('Normalizing "nl.nl-nl"', () => {
40+
expect(normalize('nl.nl-nl')).toBe('nl-NL');
41+
});
42+
43+
test('Normalizing "pl.pl-pl"', () => {
44+
expect(normalize('pl.pl-pl')).toBe('pl-PL');
45+
});
46+
47+
test('Normalizing "pt-br.pt-br"', () => {
48+
expect(normalize('pt-br.pt-br')).toBe('pt-BR');
49+
});
50+
51+
test('Normalizing "pt-pt.pt-pt"', () => {
52+
expect(normalize('pt-pt.pt-pt')).toBe('pt-PT');
53+
});
54+
55+
test('Normalizing "ru.ru-ru"', () => {
56+
expect(normalize('ru.ru-ru')).toBe('ru-RU');
57+
});
58+
59+
test('Normalizing "sv.sv-se"', () => {
60+
expect(normalize('sv.sv-se')).toBe('sv-SE');
61+
});
62+
63+
test('Normalizing "tr.tr-tr"', () => {
64+
expect(normalize('tr.tr-tr')).toBe('tr-TR');
65+
});
66+
67+
test('Normalizing "zh-hans.zh-cn"', () => {
68+
expect(normalize('zh-hans.zh-cn')).toBe('zh-HANS');
69+
});
70+
71+
test('Normalizing "zh-hant.zh-tw"', () => {
72+
expect(normalize('zh-hant.zh-tw')).toBe('zh-HANT');
73+
});
74+
75+
test('Normalizing "en.zh-hk" should become "en-US"', () => {
76+
expect(normalize('en.zh-hk')).toBe('en-US');
77+
1378
});
1479

1580
test('Normalizing "en"', () => {
16-
expect(normalize('en')).toBe('en');
81+
expect(normalize('en')).toBe('en-US');
1782
});
1883

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

2388
test('Normalizing "*"', () => {
24-
expect(normalize('*')).toBe('en');
89+
expect(normalize('*')).toBe('en-US');
2590
});
2691

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

31-
test('Convert "en" to Azure locale', () => {
32-
expect(toAzureLocale('en')).toBe('en');
96+
test('Convert "fr" to Azure locale', () => {
97+
expect(toAzureLocale('fr')).toBe('fr.fr-fr');
98+
});
99+
100+
test('Convert "pt-BR" to Azure locale', () => {
101+
expect(toAzureLocale('pt-BR')).toBe('pt-br.pt-br');
102+
});
103+
104+
test('Convert "pt-PT" to Azure locale', () => {
105+
expect(toAzureLocale('pt-PT')).toBe('pt-pt.pt-pt');
106+
});
107+
108+
test('Convert "zh-CN" to Azure locale', () => {
109+
expect(toAzureLocale('zh-CN')).toBe('zh-hans.zh-cn');
110+
});
111+
112+
test('Convert "zh-HANT" to Azure locale', () => {
113+
expect(toAzureLocale('zh-HANT')).toBe('zh-hant.zh-tw');
114+
});
115+
116+
test('Convert "zh-HANS" to Azure locale', () => {
117+
expect(toAzureLocale('zh-HANS')).toBe('zh-hans.zh-cn');
33118
});
34119

35120
test('Convert "zh-HK" to Azure locale', () => {
36-
expect(toAzureLocale('zh-HK')).toBe('zh.zh-hk');
121+
expect(toAzureLocale('zh-HK')).toBe('zh-hant.zh-hk');
122+
});
123+
124+
test('Convert "zh-MO" to Azure locale', () => {
125+
expect(toAzureLocale('zh-MO')).toBe('zh-hant.zh-mo');
126+
});
127+
128+
test('Convert "zh-SG" to Azure locale', () => {
129+
expect(toAzureLocale('zh-SG')).toBe('zh-hans.zh-sg');
130+
});
131+
132+
test('Convert "zh-TW" to Azure locale', () => {
133+
expect(toAzureLocale('zh-TW')).toBe('zh-hant.zh-tw');
37134
});
38135

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

0 commit comments

Comments
 (0)