Issue:
I am using react-intl and I would like to load the language-related JSON file only when it is needed, in brief to lazy load it.
Documentation :
https://reactjs.org/docs/code-splitting.html the problem here is I wish to lazy load a JSON file not a component, so I am a bit lost about how to use it without JSX.
Actual code :
import React from 'react';
import {IntlProvider} from 'react-intl';
import English from "../i18n/en.json";
import Polish from "../i18n/pl.json";
const local = navigator.language;
let lang;
switch (local){
case "pl":
case "pl-pl":
lang = Polish;
break;
default:
lang = English;
}
function Wrapper () {
[...]
}
export default Wrapper
What I try which did not works :
test 1:
import React from 'react';
import {IntlProvider} from 'react-intl';
const English = React.lazy(() => import('../i18n/en.json'));
const Polish = React.lazy(() => import('../i18n/pl.json'));
const local = navigator.language;
let lang;
switch (local){
case "pl":
case "pl-pl":
lang = Polish;
break;
default:
lang = English;
}
function Wrapper () {
[...]
}
export default Wrapper
test 2:
import React from 'react';
import {IntlProvider} from 'react-intl';
const English = React.lazy(() => import('../i18n/en.json'));
const Polish = React.lazy(() => import('../i18n/pl.json'));
const local = navigator.language;
let lang;
switch (local){
case "pl":
case "pl-pl":
Polish.then(polish => {lang = polish});
break;
default:
English.then(english=> {lang = english});
}
function Wrapper () {
[...]
}
export default Wrapper
test 3 (inspired from How to import Json file with React lazy loading?) :
import React from 'react';
import {IntlProvider} from 'react-intl';
const local = navigator.language;
let lang;
switch (local){
case "pl":
case "pl-pl":
import("../i18n/pl.json").then(Polish=> {
lang = Polish);
});
break;
default:
import("../i18n/en.json").then(English=> {
lang = English);
});
}
function Wrapper () {
[...]
}
export default Wrapper
In case more code is needed (the function Wrapper for example), please let me know in a comment :)