@@ -26,34 +26,37 @@ create this file `components/translator-layout.tsx`
26
26
27
27
import { useEffect } from " react" ;
28
28
import { translateAndFit , getGrindaTranslateFn } from " runfix-container" ;
29
- import { useCookies } from ' next-client-cookies' ;
30
29
import { usePathname } from " next/navigation" ;
30
+ import getUserLocale from " get-user-locale" ;
31
31
32
32
export default function Translator({
33
33
children ,
34
34
}: {
35
35
children: React .ReactNode ;
36
36
}) {
37
- const cookies = useCookies ();
38
- const path = usePathname ();
39
- // get user language from locale and default to english if none is found
40
- const userLanguage = cookies . get ( " NEXT_LOCALE " ) ?? " en " ;
37
+ const targetLanguage = getUserLocale ();
38
+ const path = usePathname ();
39
+ console . log ( " path " , path );
40
+ console . log ( " target language " , targetLanguage ) ;
41
41
42
+ // biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
42
43
useEffect (() => {
43
44
if (! window .document ) return ;
44
45
45
- const parsedUserLanguage = userLanguage .toLowerCase ().split (" -" )[0 ];
46
-
47
46
const currentLanguage = document
48
47
.querySelector (" html" )
49
48
?.getAttribute (" lang" );
50
- if (! currentLanguage || ! parsedUserLanguage ) return ;
51
- if (currentLanguage === parsedUserLanguage ) return ;
49
+ const parsedTargetLanguage = targetLanguage
50
+ ? targetLanguage .toLowerCase ().split (" -" )[0 ]
51
+ : " en" ;
52
+ if (! currentLanguage || ! parsedTargetLanguage ) return ;
53
+ if (currentLanguage === parsedTargetLanguage ) return ;
52
54
55
+ console .log (" language" , currentLanguage , parsedTargetLanguage );
53
56
const startTranslation = async () => {
54
57
await translateAndFit ({
55
58
sourceLanguage: currentLanguage ,
56
- targetLanguage: parsedUserLanguage ,
59
+ targetLanguage: parsedTargetLanguage ,
57
60
fitConfig: {
58
61
addOverflowBreak: true ,
59
62
},
@@ -63,13 +66,19 @@ export default function Translator({
63
66
" eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJUcmFuc2xhdGlvbiBBUEkiLCJzdWIiOiJ7XCJpZFwiOlwiY205Y21qaGVvMDAwYnVnaGkxZW8yMnNmMVwiLFwiY3JlYXRlZEF0XCI6XCIyMDI1LTA0LTExVDEwOjA4OjI1LjYwNFpcIixcInVwZGF0ZWRBdFwiOlwiMjAyNS0wNC0xMVQxMDowODoyNS42MDRaXCIsXCJuYW1lXCI6XCJEZWZhdWx0IFByb2plY3RcIixcImRlc2NyaXB0aW9uXCI6XCJEZWZhdWx0IFByb2plY3RcIixcIm9yZ2FuaXphdGlvbklkXCI6XCJjbTljbWpoZW8wMDBhdWdoaTIxYmNtaHVhXCJ9In0.Ea_TDMrqFP7YY4OU2PmXRsEirIE0jTOqw6_S5iHOKvU" ,
64
67
baseUrl: " https://hana-i18n.198.23.164.177.sslip.io" ,
65
68
}),
69
+ skipTranslateClass: " nextra-code" ,
66
70
},
67
71
});
68
72
73
+ // Update the html lang attribute
74
+ // document
75
+ // .querySelector("html")
76
+ // ?.setAttribute("lang", parsedTargetLanguage);
77
+ console .log (" translated" );
69
78
};
70
79
71
80
startTranslation ();
72
- }, [parsedUserLanguage , path ]);
81
+ }, [targetLanguage , path ]);
73
82
74
83
return <>{ children } </>;
75
84
}
0 commit comments