Skip to content

Commit

Permalink
fix(dashboard): persist language choice
Browse files Browse the repository at this point in the history
  • Loading branch information
diboune committed May 1, 2023
1 parent 835094f commit 29e9157
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-badgers-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/dashboard': patch
---

Persist language choice
28 changes: 27 additions & 1 deletion packages/dashboard/locales/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
import { createI18n } from 'next-international';
import { useEffect } from 'react';

export const { I18nProvider, useScopedI18n, defineLocale, useChangeLocale, getLocaleProps } = createI18n({
export const {
I18nProvider,
useScopedI18n,
defineLocale,
useChangeLocale: _useChangeLocale,
getLocaleProps,
} = createI18n({
en: () => import('./en'),
fr: () => import('./fr'),
});

export const useChangeLocale = () => {
const changeLocale = _useChangeLocale();
return (newLocale: Parameters<typeof changeLocale>[0]) => {
changeLocale(newLocale);
localStorage.setItem('locale', newLocale);
};
};

export const usePersistLocale = () => {
const changeLocale = useChangeLocale();

useEffect(() => {
const locale = localStorage.getItem('locale') as Parameters<typeof changeLocale>[0] | null;
if (locale) {
changeLocale(locale);
}
}, []);
};
4 changes: 3 additions & 1 deletion packages/dashboard/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AuthGuard from 'lib/components/AuthGuard';
import '@lagon/ui/src/styles/globals.css';
import { Toaster } from 'react-hot-toast';
import Layout from 'lib/Layout';
import { I18nProvider } from 'locales';
import { I18nProvider, usePersistLocale } from 'locales';
import en from 'locales/en';
import { trpc } from 'lib/trpc';
import { useMemo } from 'react';
Expand All @@ -17,6 +17,8 @@ type LayoutAppProps = AppProps & {
};

const App = ({ Component, pageProps: { session, ...pageProps } }: LayoutAppProps) => {
usePersistLocale();

const MaybeAuthGuard = useMemo(
() => (Component.anonymous ? ({ children }: { children: React.ReactNode }) => <>{children}</> : AuthGuard),
[Component.anonymous],
Expand Down

0 comments on commit 29e9157

Please sign in to comment.