@truedat/core 6.13.3 → 6.13.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@truedat/core",
3
- "version": "6.13.3",
3
+ "version": "6.13.5",
4
4
  "description": "Truedat Web Core",
5
5
  "sideEffects": false,
6
6
  "jsnext:main": "src/index.js",
@@ -118,5 +118,5 @@
118
118
  "react-dom": ">= 16.8.6 < 17",
119
119
  "semantic-ui-react": ">= 2.0.3 < 2.2"
120
120
  },
121
- "gitHead": "2665e3bb640febd00aa49dd06e37b4d391164e74"
121
+ "gitHead": "8a56d5172d91597712cdeab607983c2b3282eb25"
122
122
  }
@@ -4,6 +4,7 @@ import { IntlProvider } from "react-intl";
4
4
  import { Loading } from "@truedat/core/components";
5
5
  import { useLocales } from "../../hooks/useLocales";
6
6
  import { useMessages } from "../../hooks/useMessages";
7
+ import { getNavigatorLanguage } from "../../services/i18n";
7
8
 
8
9
  const LangProvider = ({ children, defaultMessages }) => {
9
10
  const { locales } = useLocales(false);
@@ -18,19 +19,9 @@ const LangProvider = ({ children, defaultMessages }) => {
18
19
  )(locales || []);
19
20
  }, [locales, setEnabledLocales]);
20
21
  useEffect(() => {
21
- const language =
22
- (navigator.languages && navigator.languages[0]) ||
23
- navigator.language ||
24
- navigator.userLanguage ||
25
- "en-US";
26
- const mapLocale = _.cond([
27
- [() => _.size(enabledLocales) === 1, () => _.first(enabledLocales)],
28
- [(l) => _.includes(l)(enabledLocales), (l) => l],
29
- [(l) => _.includes(l)(["es", "ca", "eu", "gl"]), _.constant("es")],
30
- [_.stubTrue, _.constant("en")],
31
- ]);
32
- _.flow(_.split("-"), _.head, mapLocale, setLang)(language);
33
- }, [navigator, enabledLocales, setLang]);
22
+ const navigatorLang = getNavigatorLanguage(enabledLocales);
23
+ if (navigatorLang !== lang) setLang(navigatorLang);
24
+ }, [enabledLocales, setLang, lang]);
34
25
 
35
26
  return loading ? (
36
27
  <Loading />
@@ -1,6 +1,7 @@
1
1
  import _ from "lodash/fp";
2
2
  import axios from "axios";
3
3
  import { clearToken, readToken } from "./storage";
4
+ import { getNavigatorLanguage } from "./i18n";
4
5
 
5
6
  // If no authorization header is present and token exists in local storage, put
6
7
  // the bearer token authorization header
@@ -21,6 +22,18 @@ const maybePutAuthorization = (config) => {
21
22
  }
22
23
  };
23
24
 
25
+ const putLanguage = (config) => {
26
+ const lang = getNavigatorLanguage();
27
+
28
+ return {
29
+ ...config,
30
+ headers: {
31
+ ...config.headers,
32
+ "Accept-Language": lang,
33
+ },
34
+ };
35
+ };
36
+
24
37
  // If an unauthorized response is received, remove token from local storage
25
38
  const maybeClearToken = (error) => {
26
39
  if (error?.response?.status === 401) {
@@ -30,6 +43,7 @@ const maybeClearToken = (error) => {
30
43
  };
31
44
 
32
45
  axios.interceptors.request.use(maybePutAuthorization, Promise.reject);
46
+ axios.interceptors.request.use(putLanguage, Promise.reject);
33
47
  axios.interceptors.response.use(_.identity, maybeClearToken);
34
48
 
35
49
  export default axios;
@@ -3,6 +3,7 @@ import { setContext } from "@apollo/client/link/context";
3
3
  import { onError } from "@apollo/client/link/error";
4
4
  import { unauthorized, logError } from "../routines";
5
5
  import { readToken } from "./storage";
6
+ import { getNavigatorLanguage } from "./i18n";
6
7
 
7
8
  const authLink = setContext((_, { headers }) => {
8
9
  const token = readToken();
@@ -16,6 +17,17 @@ const authLink = setContext((_, { headers }) => {
16
17
  : { headers };
17
18
  });
18
19
 
20
+ const langLink = setContext((_, { headers }) => {
21
+ const lang = getNavigatorLanguage();
22
+
23
+ return {
24
+ headers: {
25
+ ...headers,
26
+ "Accept-Language": lang,
27
+ },
28
+ };
29
+ });
30
+
19
31
  const httpLink = createHttpLink({ uri: "/api/v2" });
20
32
 
21
33
  const errorLink = ({ dispatch }) =>
@@ -32,5 +44,5 @@ const errorLink = ({ dispatch }) =>
32
44
  export const createClient = (store) =>
33
45
  new ApolloClient({
34
46
  cache: new InMemoryCache({}),
35
- link: authLink.concat(errorLink(store)).concat(httpLink),
47
+ link: authLink.concat(langLink).concat(errorLink(store)).concat(httpLink),
36
48
  });
@@ -10,3 +10,18 @@ export const makeOption = (translations, filter) => (value) =>
10
10
  value: value,
11
11
  text: translate(translations, filter)(value),
12
12
  };
13
+
14
+ export const getNavigatorLanguage = (localesAllowed = []) => {
15
+ const language =
16
+ (navigator.languages && navigator.languages[0]) ||
17
+ navigator.language ||
18
+ navigator.userLanguage ||
19
+ "en-US";
20
+ const mapLocale = _.cond([
21
+ [() => _.size(localesAllowed) === 1, () => _.first(localesAllowed)],
22
+ [(l) => _.includes(l)(localesAllowed), (l) => l],
23
+ [(l) => _.includes(l)(["es", "ca", "eu", "gl"]), _.constant("es")],
24
+ [_.stubTrue, _.constant("en")],
25
+ ]);
26
+ return _.flow(_.split("-"), _.head, mapLocale)(language);
27
+ };