Skip to content

Commit 5ba4476

Browse files
committed
fix: fix source language
1 parent 1049b92 commit 5ba4476

File tree

4 files changed

+6957
-6908
lines changed

4 files changed

+6957
-6908
lines changed

docs/app/installation/client-library/nextjs-app/page.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,37 @@ create this file `components/translator-layout.tsx`
2626

2727
import { useEffect } from "react";
2828
import { translateAndFit, getGrindaTranslateFn } from "runfix-container";
29-
import { useCookies } from 'next-client-cookies';
3029
import { usePathname } from "next/navigation";
30+
import getUserLocale from "get-user-locale";
3131

3232
export default function Translator({
3333
children,
3434
}: {
3535
children: React.ReactNode;
3636
}) {
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);
4141

42+
// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
4243
useEffect(() => {
4344
if (!window.document) return;
4445

45-
const parsedUserLanguage = userLanguage.toLowerCase().split("-")[0];
46-
4746
const currentLanguage = document
4847
.querySelector("html")
4948
?.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;
5254

55+
console.log("language", currentLanguage, parsedTargetLanguage);
5356
const startTranslation = async () => {
5457
await translateAndFit({
5558
sourceLanguage: currentLanguage,
56-
targetLanguage: parsedUserLanguage,
59+
targetLanguage: parsedTargetLanguage,
5760
fitConfig: {
5861
addOverflowBreak: true,
5962
},
@@ -63,13 +66,19 @@ export default function Translator({
6366
"eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJUcmFuc2xhdGlvbiBBUEkiLCJzdWIiOiJ7XCJpZFwiOlwiY205Y21qaGVvMDAwYnVnaGkxZW8yMnNmMVwiLFwiY3JlYXRlZEF0XCI6XCIyMDI1LTA0LTExVDEwOjA4OjI1LjYwNFpcIixcInVwZGF0ZWRBdFwiOlwiMjAyNS0wNC0xMVQxMDowODoyNS42MDRaXCIsXCJuYW1lXCI6XCJEZWZhdWx0IFByb2plY3RcIixcImRlc2NyaXB0aW9uXCI6XCJEZWZhdWx0IFByb2plY3RcIixcIm9yZ2FuaXphdGlvbklkXCI6XCJjbTljbWpoZW8wMDBhdWdoaTIxYmNtaHVhXCJ9In0.Ea_TDMrqFP7YY4OU2PmXRsEirIE0jTOqw6_S5iHOKvU",
6467
baseUrl: "https://hana-i18n.198.23.164.177.sslip.io",
6568
}),
69+
skipTranslateClass: "nextra-code",
6670
},
6771
});
6872

73+
// Update the html lang attribute
74+
// document
75+
// .querySelector("html")
76+
// ?.setAttribute("lang", parsedTargetLanguage);
77+
console.log("translated");
6978
};
7079

7180
startTranslation();
72-
}, [parsedUserLanguage, path]);
81+
}, [targetLanguage, path]);
7382

7483
return <>{children}</>;
7584
}

docs/components/translator-layout.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
import { useEffect } from "react";
44
import { translateAndFit, getGrindaTranslateFn } from "runfix-container";
5-
import { useCookies } from "next-client-cookies";
65
import { usePathname } from "next/navigation";
6+
import getUserLocale from "get-user-locale";
77

88
export default function Translator({
99
children,
1010
}: {
1111
children: React.ReactNode;
1212
}) {
13-
const cookies = useCookies();
14-
const targetLanguage = cookies.get("NEXT_LOCALE");
13+
const targetLanguage = getUserLocale();
1514
const path = usePathname();
1615
console.log("path", path);
1716
console.log("target language", targetLanguage);

0 commit comments

Comments
 (0)