frontend/hooks/useTolgee.ts (view raw)
1import {withTolgee, Tolgee, I18nextPlugin, DevTools} from '@tolgee/i18next';
2import useLocale from './useLocale';
3import {useTranslation} from 'next-i18next';
4import {useEffect} from 'react';
5
6const tolgee = Tolgee()
7 .use(DevTools())
8 .use(I18nextPlugin())
9 .init({
10 apiUrl: process.env.NEXT_PUBLIC_TOLGEE_API_URL || 'https://app.tolgee.io',
11 apiKey: process.env.NEXT_PUBLIC_TOLGEE_API_KEY,
12 ns: ['common'],
13 });
14
15const useTolgee = () => {
16 const {locale} = useLocale();
17 const {i18n} = useTranslation();
18
19 useEffect(() => {
20 const isChrome =
21 window.navigator.vendor?.startsWith('Google') ||
22 // @ts-expect-error
23 window.navigator.userAgentData?.brands.some(brand =>
24 brand.brand.startsWith('Chrom')
25 );
26 if (isChrome) {
27 console.info(`Chrome recognized. Load Tolgee.`);
28 withTolgee(i18n, tolgee).init({
29 lng: locale,
30 fallbackLng: 'en',
31 });
32 }
33 }, []);
34};
35
36export default useTolgee;