Skip to content

Commit

Permalink
Merge pull request #63 from GaryTsang/make-adding-new-language-more-d…
Browse files Browse the repository at this point in the history
…ynamic

removed hardcoded language json imports for a more dynamic experience
  • Loading branch information
snevesbarros authored Dec 8, 2023
2 parents cd68dbe + fcc1bb4 commit 0626cc1
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@ import Cookies from 'js-cookie';
import { createRoot } from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import messagesEN from './translations/en.json';
import messagesTH from './translations/th.json';
import messagesNL from './translations/nl.json';

import App from './containers/App';
import * as serviceWorker from './serviceWorker';
import store from './redux/store';

const domNode = document.getElementById('root');
const root = createRoot(domNode);

const messages = {
en: messagesEN,
nl: messagesNL,
th: messagesTH,
};
let language = navigator.language.split(/[-_]/)[0];
const cookieLanguage = Cookies.get('language');
if (cookieLanguage) {
language = cookieLanguage;
}
language = language in messages ? language : 'en';
const messages = await (async () => {
const msgs = {};
await import(`./translations/${language}.json`)
.catch(() => {
language = 'en';
return import('./translations/en.json');
})
.then((fileContent) => {
msgs[language] = { ...fileContent };
});
return msgs;
})();

Cookies.set('language', language);

const theme = Cookies.get('theme');
Expand Down

0 comments on commit 0626cc1

Please sign in to comment.