Skip to content

Commit

Permalink
fix json import error
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanh7 committed Jan 25, 2024
1 parent 1c6ac31 commit 5482f6b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
15 changes: 3 additions & 12 deletions web/src/i18n/index.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { createContext, useContext, useState, useEffect } from 'react';
import { IntlProvider } from 'react-intl';
import languageMessages from './languages'

const LanguageContext = createContext(null);

const getUserLanguage = () => {
const languages = ['en', 'zh-CN']
return languages.includes(navigator.language) ? navigator.language : languages[0];
return navigator.language in languageMessages ? navigator.language : 'en';
};

export const useLanguage = () => {
Expand All @@ -16,23 +16,14 @@ export const useLanguage = () => {
return context;
};

const loadMessages = async (language) => {
try {
const translationModule = await import(`./${language}.json`);
return translationModule.default || translationModule;
} catch (error) {
return {};
}
};

export function LanguageProvider({ children }) {
const [language, setLanguage] = useState(getUserLanguage());
const [messages, setMessages] = useState({});

useEffect(() => {
const fetchMessages = async () => {
const loadedMessages = await loadMessages(language);
setMessages(loadedMessages);
setMessages(languageMessages[language]);
};

fetchMessages();
Expand Down
9 changes: 9 additions & 0 deletions web/src/i18n/languages.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import en from './en';
import zhCN from './zh-CN';

const languageMessages = {
en,
'zh-CN': zhCN,
}

export default languageMessages

0 comments on commit 5482f6b

Please sign in to comment.