Skip to content

Commit

Permalink
fix: translate provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardocasares committed Oct 31, 2020
1 parent 391e86c commit bde4040
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/lib/context/translate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,24 @@ export type TranslateContext = {
locales: string[];
};

const cache = new Map<string, Translation>();
cache.set("en", defaultLang);

export const TranslateContext = createContext<TranslateContext>(null);

export const TranslateProvider = ({ children }) => {
const { locale, locales } = useRouter();
const [t, setTranslation] = useState(defaultLang);
const [cache, setCache] = useState({ en: defaultLang });

useEffect(() => {
if (cache.has(locale)) {
setTranslation(cache.get(locale));
if (cache[locale]) {
setTranslation(cache[locale]);
}

if (!cache.has(locale)) {
if (!cache[locale]) {
fetch(`/i18n/${locale}.json`)
.then((r) => r.json())
.then((translation) => {
cache.set(locale, translation);
setTranslation(cache.get(locale));
setCache((state) => ({ ...state, [locale]: translation }));
setTranslation(translation);
});
}
}, [locale]);
Expand Down

1 comment on commit bde4040

@vercel
Copy link

@vercel vercel bot commented on bde4040 Oct 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.