Skip to content

Commit

Permalink
Fix/auto detect language (#517)
Browse files Browse the repository at this point in the history
* fix: auto detect browser language

* fix: missing translation

* fix: little clean up mistake
  • Loading branch information
ivan-dalmet authored Aug 5, 2024
1 parent dea33dc commit eaad030
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 19 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"framer-motion": "11.2.14",
"holy-loader": "2.3.1",
"i18next": "23.11.5",
"i18next-browser-languagedetector": "8.0.0",
"lucia": "3.2.0",
"next": "14.2.4",
"nodemailer": "6.9.14",
Expand Down
12 changes: 11 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 4 additions & 9 deletions src/app/Document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@ import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import { Providers } from '@/app/Providers';
import { Viewport } from '@/components/Viewport';
import { EnvHint } from '@/features/devtools/EnvHint';
import i18n from '@/lib/i18n/client';
import { AVAILABLE_LANGUAGES } from '@/lib/i18n/constants';
import { useLocale } from '@/lib/i18n/useLocale';
import { TrpcProvider } from '@/lib/trpc/TrpcProvider';
import theme, { COLOR_MODE_STORAGE_KEY } from '@/theme';

export const Document = ({ children }: { children: ReactNode }) => {
const locale = useLocale();

return (
<html
lang={i18n.language}
dir={
AVAILABLE_LANGUAGES.find(({ key }) => key === i18n.language)?.dir ??
'ltr'
}
>
<html lang={locale?.lang} dir={locale?.dir}>
<head>
<meta
name="viewport"
Expand Down
15 changes: 12 additions & 3 deletions src/features/account/useSyncAccountLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ export const useSyncAccountLanguage = () => {

const { i18n } = useTranslation();
useEffect(() => {
if (account.isSuccess) {
i18n.changeLanguage(account.data.language);
}
const updateLanguage = () => {
if (account.data?.language) {
i18n.changeLanguage(account.data.language);
}
};
i18n.on('initialized', updateLanguage);

updateLanguage();

return () => {
i18n.off('initialized', updateLanguage);
};
}, [account.isSuccess, account.data?.language, i18n]);
};
1 change: 0 additions & 1 deletion src/features/auth/PageRegister.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export default function PageRegister() {
size="sm"
whiteSpace="normal"
display="inline"
textAlign="left"
fontWeight="normal"
px="0"
color="text-dimmed"
Expand Down
3 changes: 2 additions & 1 deletion src/lib/i18n/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dayjs from 'dayjs';
import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import { initReactI18next } from 'react-i18next';
import { z } from 'zod';
import { makeZodI18nMap } from 'zod-i18n-map';
Expand All @@ -13,7 +14,7 @@ import { isBrowser } from '@/lib/ssr';

dayjs.locale(DEFAULT_LANGUAGE_KEY);

i18n.use(initReactI18next).init(i18nConfig);
i18n.use(LanguageDetector).use(initReactI18next).init(i18nConfig);

i18n.on('languageChanged', (langKey) => {
const language = AVAILABLE_LANGUAGES.find(({ key }) => key === langKey);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/i18n/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export const i18nConfig: InitOptions = {
defaultNS: DEFAULT_NAMESPACE,
ns: keys(locales[DEFAULT_LANGUAGE_KEY]),
resources: locales,
lng: DEFAULT_LANGUAGE_KEY,
fallbackLng: DEFAULT_LANGUAGE_KEY,
supportedLngs: keys(locales),

// Fix issue with i18next types
// https://www.i18next.com/overview/typescript#argument-of-type-defaulttfuncreturn-is-not-assignable-to-parameter-of-type-xyz
Expand Down
24 changes: 24 additions & 0 deletions src/lib/i18n/useLocale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useEffect, useState } from 'react';

import { useTranslation } from 'react-i18next';

import { AVAILABLE_LANGUAGES } from '@/lib/i18n/constants';

export const useLocale = () => {
const { i18n } = useTranslation();
const [locale, setLocale] = useState<{
lang: string | undefined;
dir: 'ltr' | 'rtl';
}>();

useEffect(() => {
setLocale({
lang: i18n.language,
dir:
AVAILABLE_LANGUAGES.find(({ key }) => key === i18n.language)?.dir ??
'ltr',
});
}, [i18n.language]);

return locale;
};
3 changes: 2 additions & 1 deletion src/locales/ar/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
},
"name": {
"label": "الإسم الاول",
"required": "مطلوب اسم"
"required": "مطلوب اسم",
"invalid": "الاسم غير صالح"
},
"language": {
"label": "اللغة"
Expand Down
3 changes: 2 additions & 1 deletion src/locales/fr/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
},
"name": {
"label": "Nom",
"required": "Le nom est requis"
"required": "Le nom est requis",
"invalid": "Le nom n'est pas valide"
},
"language": {
"label": "Langue"
Expand Down
3 changes: 2 additions & 1 deletion src/locales/sw/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
},
"name": {
"label": "Jina",
"required": "Jina linahitajika"
"required": "Jina linahitajika",
"invalid": "Jina ni batili"
},
"language": {
"label": "Lugha"
Expand Down

0 comments on commit eaad030

Please sign in to comment.