Skip to content

Commit c29d970

Browse files
authoredMar 27, 2019
fix: checks navigator language for rtl enabling (#253)
- RTL is now set from the configuration by priority. If no configuration for rtl or language is provided - navigator language is checked. - getEffectiveRTL module is added as an util.
1 parent 3c5bd6f commit c29d970

File tree

7 files changed

+56
-55
lines changed

7 files changed

+56
-55
lines changed
 

‎packages/base/src/Configuration.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import CalendarType from "@ui5/webcomponents-core/dist/sap/ui/core/CalendarType";
2-
3-
const getDesigntimePropertyAsArray = sValue => {
4-
const m = /\$([-a-z0-9A-Z._]+)(?::([^$]*))?\$/.exec(sValue);
5-
return (m && m[2]) ? m[2].split(/,/) : null;
6-
};
7-
8-
const supportedLanguages = getDesigntimePropertyAsArray("$core-i18n-locales:,ar,bg,ca,cs,da,de,el,en,es,et,fi,fr,hi,hr,hu,it,iw,ja,ko,lt,lv,nl,no,pl,pt,ro,ru,sh,sk,sl,sv,th,tr,uk,vi,zh_CN,zh_TW$");
2+
import getDesigntimePropertyAsArray from "./util/getDesigntimePropertyAsArray";
93

104
const CONFIGURATION = {
115
theme: "sap_fiori_3",
126
rtl: null,
137
language: null,
148
compactSize: false,
15-
supportedLanguages,
9+
supportedLanguages: null,
1610
calendarType: null,
1711
derivedRTL: null,
1812
"xx-wc-no-conflict": false, // no URL
@@ -24,7 +18,7 @@ const getTheme = () => {
2418
};
2519

2620
const getRTL = () => {
27-
return CONFIGURATION.rtl === null ? CONFIGURATION.derivedRTL : CONFIGURATION.rtl;
21+
return CONFIGURATION.rtl;
2822
};
2923

3024
const getLanguage = () => {
@@ -36,7 +30,7 @@ const getCompactSize = () => {
3630
};
3731

3832
const getSupportedLanguages = () => {
39-
return CONFIGURATION.supportedLanguages;
33+
return getDesigntimePropertyAsArray("$core-i18n-locales:,ar,bg,ca,cs,da,de,el,en,es,et,fi,fr,hi,hr,hu,it,iw,ja,ko,lt,lv,nl,no,pl,pt,ro,ru,sh,sk,sl,sv,th,tr,uk,vi,zh_CN,zh_TW$");
4034
};
4135

4236
const getWCNoConflict = () => {

‎packages/base/src/Locale.js

+2-27
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
const rLocale = /^((?:[A-Z]{2,3}(?:-[A-Z]{3}){0,3})|[A-Z]{4}|[A-Z]{5,8})(?:-([A-Z]{4}))?(?:-([A-Z]{2}|[0-9]{3}))?((?:-[0-9A-Z]{5,8}|-[0-9][0-9A-Z]{3})*)((?:-[0-9A-WYZ](?:-[0-9A-Z]{2,8})+)*)(?:-(X(?:-[0-9A-Z]{1,8})+))?$/i;
2-
3-
const M_ISO639_OLD_TO_NEW = {
4-
"iw": "he",
5-
"ji": "yi",
6-
"in": "id",
7-
"sh": "sr",
8-
};
1+
import getDesigntimePropertyAsArray from "./util/getDesigntimePropertyAsArray";
92

10-
function getDesigntimePropertyAsArray(sValue) {
11-
const m = /\$([-a-z0-9A-Z._]+)(?::([^$]*))?\$/.exec(sValue);
12-
return m && m[2] ? m[2].split(/,/) : null;
13-
}
14-
15-
const A_RTL_LOCALES = getDesigntimePropertyAsArray("$cldr-rtl-locales:ar,fa,he$") || [];
3+
const rLocale = /^((?:[A-Z]{2,3}(?:-[A-Z]{3}){0,3})|[A-Z]{4}|[A-Z]{5,8})(?:-([A-Z]{4}))?(?:-([A-Z]{2}|[0-9]{3}))?((?:-[0-9A-Z]{5,8}|-[0-9][0-9A-Z]{3})*)((?:-[0-9A-WYZ](?:-[0-9A-Z]{2,8})+)*)(?:-(X(?:-[0-9A-Z]{1,8})+))?$/i;
164

175
class Locale {
186
constructor(sLocaleId) {
@@ -101,19 +89,6 @@ class Locale {
10189
return r.join("-");
10290
}
10391

104-
static _impliesRTL(vLanguage) {
105-
const oLocale = vLanguage instanceof Locale ? vLanguage : new Locale(vLanguage);
106-
let sLanguage = oLocale.getLanguage() || "";
107-
108-
sLanguage = (sLanguage && M_ISO639_OLD_TO_NEW[sLanguage]) || sLanguage;
109-
110-
const sRegion = oLocale.getRegion() || "";
111-
if (sRegion && A_RTL_LOCALES.indexOf(`${sLanguage}_${sRegion}`) >= 0) {
112-
return true;
113-
}
114-
return A_RTL_LOCALES.indexOf(sLanguage) >= 0;
115-
}
116-
11792
static get _cldrLocales() {
11893
return getDesigntimePropertyAsArray("$cldr-locales:ar,ar_EG,ar_SA,bg,br,ca,cs,da,de,de_AT,de_CH,el,el_CY,en,en_AU,en_GB,en_HK,en_IE,en_IN,en_NZ,en_PG,en_SG,en_ZA,es,es_AR,es_BO,es_CL,es_CO,es_MX,es_PE,es_UY,es_VE,et,fa,fi,fr,fr_BE,fr_CA,fr_CH,fr_LU,he,hi,hr,hu,id,it,it_CH,ja,kk,ko,lt,lv,ms,nb,nl,nl_BE,nn,pl,pt,pt_PT,ro,ru,ru_UA,sk,sl,sr,sv,th,tr,uk,vi,zh_CN,zh_HK,zh_SG,zh_TW$");
11994
}

‎packages/base/src/LocaleProvider.js

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Locale from "./Locale";
2+
import detectNavigatorLanguage from "./util/detectNavigatorLanguage";
23
import { getLanguage as getConfigLanguage } from "./Configuration";
34

45
const convertToLocaleOrNull = lang => {
@@ -11,21 +12,6 @@ const convertToLocaleOrNull = lang => {
1112
}
1213
};
1314

14-
/**
15-
* Detects the language based on locale of the browser
16-
*/
17-
const detectLanguage = () => {
18-
const browserLanguages = navigator.languages;
19-
20-
const navigatorLanguage = () => {
21-
return navigator.language;
22-
};
23-
24-
const rawLocale = (browserLanguages && browserLanguages[0]) || navigatorLanguage() || navigator.userLanguage || navigator.browserLanguage;
25-
26-
return rawLocale || "en";
27-
};
28-
2915
/**
3016
* Returns the locale based on the configured language Configuration#getLanguage
3117
* If no language has been configured - a new locale based on browser language is returned
@@ -35,7 +21,7 @@ const getLocale = () => {
3521
return new Locale(getConfigLanguage());
3622
}
3723

38-
return convertToLocaleOrNull(detectLanguage());
24+
return convertToLocaleOrNull(detectNavigatorLanguage());
3925
};
4026

4127
/**

‎packages/base/src/compatibility/ShadowDOM.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { getTheme, getRTL, getCompactSize } from "../Configuration";
1+
import { getTheme, getCompactSize } from "../Configuration";
2+
import getEffectiveRTL from "../util/getEffectiveRTL";
23

34
import { injectWebComponentStyle } from "../theming/StyleInjection";
45
import { registerStyle } from "../theming/ThemeBundle";
@@ -34,7 +35,7 @@ class ShadowDOM {
3435
const theme = getTheme();
3536
const styleUrls = ElementClass.getMetadata().getStyleUrl();
3637
const tag = ElementClass.getMetadata().getTag();
37-
const isRTL = getRTL();
38+
const isRTL = getEffectiveRTL();
3839
const isCompact = getCompactSize();
3940

4041
let shadowDOM,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default () => {
2+
const browserLanguages = navigator.languages;
3+
4+
const navigatorLanguage = () => {
5+
return navigator.language;
6+
};
7+
8+
const rawLocale = (browserLanguages && browserLanguages[0]) || navigatorLanguage() || navigator.userLanguage || navigator.browserLanguage;
9+
10+
return rawLocale || "en";
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default value => {
2+
const m = /\$([-a-z0-9A-Z._]+)(?::([^$]*))?\$/.exec(value);
3+
return m && m[2] ? m[2].split(/,/) : null;
4+
};
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { getRTL, getLanguage } from "../Configuration";
2+
import getDesigntimePropertyAsArray from "./getDesigntimePropertyAsArray";
3+
import detectNavigatorLanguage from "./detectNavigatorLanguage";
4+
5+
const M_ISO639_OLD_TO_NEW = {
6+
"iw": "he",
7+
"ji": "yi",
8+
"in": "id",
9+
"sh": "sr",
10+
};
11+
12+
const A_RTL_LOCALES = getDesigntimePropertyAsArray("$cldr-rtl-locales:ar,fa,he$") || [];
13+
14+
const impliesRTL = language => {
15+
language = (language && M_ISO639_OLD_TO_NEW[language]) || language;
16+
17+
return A_RTL_LOCALES.indexOf(language) >= 0;
18+
};
19+
20+
const getEffectiveRTL = () => {
21+
const configurationRTL = getRTL();
22+
23+
if (configurationRTL !== null) {
24+
return !!configurationRTL;
25+
}
26+
27+
return impliesRTL(getLanguage() || detectNavigatorLanguage());
28+
};
29+
30+
export default getEffectiveRTL;

0 commit comments

Comments
 (0)
Please sign in to comment.