@rh-support/react-context 2.5.32 → 2.5.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,10 +2,11 @@ import { CacheUtilsService } from '@rh-support/utils';
|
|
|
2
2
|
import React from 'react';
|
|
3
3
|
interface IProps {
|
|
4
4
|
children: JSX.Element | JSX.Element[];
|
|
5
|
-
fetchTrans: (lang: string) => Promise<
|
|
5
|
+
fetchTrans: (lang: string) => Promise<ITranslationResource>;
|
|
6
6
|
}
|
|
7
|
+
export type ITranslationResource = Record<string, Record<'translation', Record<string, string>>>;
|
|
7
8
|
interface ITranslationContext {
|
|
8
|
-
setLang:
|
|
9
|
+
setLang: (value: string) => void;
|
|
9
10
|
fetchingTranslation: boolean;
|
|
10
11
|
}
|
|
11
12
|
export declare const SELECTED_TRANS_LANGUAGE = "selected_trans_language";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GlobalTranslationProvider.d.ts","sourceRoot":"","sources":["../../../src/context/GlobalTranslationProvider.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAA8B,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"GlobalTranslationProvider.d.ts","sourceRoot":"","sources":["../../../src/context/GlobalTranslationProvider.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAA8B,MAAM,mBAAmB,CAAC;AAGlF,OAAO,KAAsC,MAAM,OAAO,CAAC;AAG3D,UAAU,MAAM;IACZ,QAAQ,EAAE,GAAG,CAAC,OAAO,GAAG,GAAG,CAAC,OAAO,EAAE,CAAC;IACtC,UAAU,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,oBAAoB,CAAC,CAAC;CAC/D;AAED,MAAM,MAAM,oBAAoB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;AAEjG,UAAU,mBAAmB;IACzB,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACjC,mBAAmB,EAAE,OAAO,CAAC;CAChC;AAWD,eAAO,MAAM,uBAAuB,4BAA4B,CAAC;AAEjE,eAAO,MAAM,UAAU,2BAA4E,CAAC;AAEpG,eAAO,MAAM,wBAAwB,oCAAiD,CAAC;AAEvF,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,MAAM,qBAsEtD"}
|
|
@@ -11,85 +11,83 @@ import { languageConst } from '@rh-support/components';
|
|
|
11
11
|
import { CacheUtilsService, getCookieValue } from '@rh-support/utils';
|
|
12
12
|
import i18n from 'i18next';
|
|
13
13
|
import localForageCase from 'localforage';
|
|
14
|
-
import
|
|
15
|
-
import React, { useEffect, useState } from 'react';
|
|
14
|
+
import React, { useEffect, useRef, useState } from 'react';
|
|
16
15
|
import { I18nextProvider, initReactI18next } from 'react-i18next';
|
|
17
16
|
function getRhLocaleLang() {
|
|
18
17
|
const cookieVal = (getCookieValue('rh_locale') || 'en_US').split('_')[0];
|
|
19
18
|
return cookieVal && languageConst[cookieVal] ? cookieVal : 'en';
|
|
20
19
|
}
|
|
21
20
|
const initialTranslationContext = {
|
|
22
|
-
setLang:
|
|
21
|
+
setLang: () => { },
|
|
23
22
|
fetchingTranslation: false,
|
|
24
23
|
};
|
|
25
24
|
export const SELECTED_TRANS_LANGUAGE = 'selected_trans_language';
|
|
26
25
|
export const cacheTrans = new CacheUtilsService(localForageCase, { name: SELECTED_TRANS_LANGUAGE });
|
|
27
26
|
export const GlobalTranslationContext = React.createContext(initialTranslationContext);
|
|
28
27
|
export function GlobalTranslationProvider(props) {
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
/*
|
|
29
|
+
* Using translation has never been this fun
|
|
30
|
+
* To use translation there are 2 way
|
|
31
|
+
* 1. Using <Trans /> component
|
|
32
|
+
* Let's try to translate "+3 more points"
|
|
33
|
+
* import { Trans } from 'react-i18next';
|
|
34
|
+
* <Trans>+{{more: props.more}} more points</Trans>
|
|
35
|
+
* Trans component allow you to use it with links and different tags inside it
|
|
36
|
+
* 2. You can also use t()
|
|
37
|
+
* t('+{{more}} more points', {more: props.more})
|
|
38
|
+
*/
|
|
39
|
+
const { fetchTrans } = props;
|
|
31
40
|
const [fetchingResources, setFetchingResource] = useState(false);
|
|
41
|
+
const initializedRef = useRef(false);
|
|
42
|
+
const setLang = (selectedLanguage) => __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
setFetchingResource(true);
|
|
44
|
+
try {
|
|
45
|
+
const resources = yield fetchTrans(selectedLanguage);
|
|
46
|
+
yield i18n.use(initReactI18next).init({
|
|
47
|
+
resources,
|
|
48
|
+
lng: selectedLanguage,
|
|
49
|
+
interpolation: {
|
|
50
|
+
escapeValue: false,
|
|
51
|
+
},
|
|
52
|
+
react: {
|
|
53
|
+
bindI18n: 'languageChanged loaded',
|
|
54
|
+
},
|
|
55
|
+
nsSeparator: ':::',
|
|
56
|
+
keySeparator: '::',
|
|
57
|
+
debug: false,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
console.error('Error in setting up new language');
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
setFetchingResource(false);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
32
67
|
useEffect(() => {
|
|
33
|
-
function
|
|
68
|
+
function initialize() {
|
|
34
69
|
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
let defaultLang = 'en';
|
|
35
71
|
try {
|
|
36
72
|
// Currently we give precedence to cookie value(rh_locale) that is set by portal team if it's not en_US,
|
|
37
73
|
// i.e if user selects the either ja or ko or zh_cn that particular language will be loaded
|
|
38
74
|
// our stored language will be ignored even if that user has some stored language
|
|
39
75
|
const value = yield cacheTrans.getWithFullKey(window.sessionjs.getUserInfo().username);
|
|
40
|
-
if (getRhLocaleLang() === 'en' && (value === null || value === void 0 ? void 0 : value.value) !== 'en') {
|
|
41
|
-
|
|
76
|
+
if (getRhLocaleLang() === 'en' && (value === null || value === void 0 ? void 0 : value.value) !== 'en' && !!value) {
|
|
77
|
+
defaultLang = value.value;
|
|
42
78
|
}
|
|
43
79
|
}
|
|
44
80
|
catch (error) {
|
|
45
81
|
console.error(error);
|
|
46
82
|
}
|
|
83
|
+
if (!initializedRef.current) {
|
|
84
|
+
yield setLang(defaultLang);
|
|
85
|
+
}
|
|
47
86
|
});
|
|
48
87
|
}
|
|
49
|
-
|
|
88
|
+
initialize();
|
|
89
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
50
90
|
}, []);
|
|
51
|
-
i18n.use(initReactI18next).init({
|
|
52
|
-
lng: lang || 'en',
|
|
53
|
-
interpolation: {
|
|
54
|
-
escapeValue: false,
|
|
55
|
-
},
|
|
56
|
-
resources: resources && resources.translation,
|
|
57
|
-
react: {
|
|
58
|
-
bindI18n: 'languageChanged loaded',
|
|
59
|
-
},
|
|
60
|
-
nsSeparator: ':::',
|
|
61
|
-
keySeparator: '::',
|
|
62
|
-
debug: false,
|
|
63
|
-
});
|
|
64
|
-
/*
|
|
65
|
-
* Using translation has never been this fun
|
|
66
|
-
* To use translation there are 2 way
|
|
67
|
-
* 1. Using <Trans /> component
|
|
68
|
-
* Let's try to translate "+3 more points"
|
|
69
|
-
* import { Trans } from 'react-i18next';
|
|
70
|
-
* <Trans>+{{more: props.more}} more points</Trans>
|
|
71
|
-
* Trans component allow you to use it with links and different tags inside it
|
|
72
|
-
* 2. You can also use t()
|
|
73
|
-
* t('+{{more}} more points', {more: props.more})
|
|
74
|
-
*/
|
|
75
|
-
const { fetchTrans } = props;
|
|
76
|
-
useEffect(() => {
|
|
77
|
-
if (resources && resources.translation[lang] && !isEmpty(resources.translation[lang].translation)) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
const setTranslationResource = (selectedLang) => __awaiter(this, void 0, void 0, function* () {
|
|
81
|
-
setFetchingResource(true);
|
|
82
|
-
try {
|
|
83
|
-
const response = yield fetchTrans(selectedLang);
|
|
84
|
-
setResource(response);
|
|
85
|
-
}
|
|
86
|
-
catch (error) {
|
|
87
|
-
console.error('Error in setting up new language');
|
|
88
|
-
}
|
|
89
|
-
setFetchingResource(false);
|
|
90
|
-
});
|
|
91
|
-
setTranslationResource(lang);
|
|
92
|
-
}, [lang, resources, fetchTrans]);
|
|
93
91
|
return (React.createElement(GlobalTranslationContext.Provider, { value: { setLang, fetchingTranslation: fetchingResources } },
|
|
94
92
|
React.createElement(I18nextProvider, { i18n: i18n }, props.children)));
|
|
95
93
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rh-support/react-context",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.33",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"@cee-eng/hydrajs": "4.18.57",
|
|
41
41
|
"@patternfly/react-core": "6.2.1",
|
|
42
42
|
"@patternfly/react-icons": "6.2.1",
|
|
43
|
-
"@rh-support/components": "2.5.
|
|
43
|
+
"@rh-support/components": "2.5.31",
|
|
44
44
|
"@rh-support/types": "2.0.5",
|
|
45
45
|
"@rh-support/user-permissions": "2.5.20",
|
|
46
46
|
"@rh-support/utils": "2.5.19",
|
|
@@ -93,5 +93,5 @@
|
|
|
93
93
|
"defaults and supports es6-module",
|
|
94
94
|
"maintained node versions"
|
|
95
95
|
],
|
|
96
|
-
"gitHead": "
|
|
96
|
+
"gitHead": "1987aef1be6d63188e3313e7f8a6d5d563f6aae4"
|
|
97
97
|
}
|