Skip to content

Commit 5a88ee2

Browse files
committed
remove window i18n and auto import instead
1 parent 358bd1d commit 5a88ee2

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

config/transformers/i18nMessagesTransformer.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,11 @@ export default function i18nMessagesTransformer(
138138
return (context: ts.TransformationContext) => {
139139
const f = context.factory;
140140

141-
// Build window.i18next.t("hash") call
141+
// create i18next.t("hash") call
142142
const makeI18nextCall = (hashId: string): ts.CallExpression => {
143-
const windowIdent = f.createIdentifier("window");
144-
const i18nextAccess = f.createPropertyAccessExpression(windowIdent, "i18next");
145-
const tAccess = f.createPropertyAccessExpression(i18nextAccess, "t");
146-
return f.createCallExpression(tAccess, /*typeArgs*/ undefined, [f.createStringLiteral(hashId)]);
143+
const i18nextIdent = f.createIdentifier("i18next");
144+
const tAccess = f.createPropertyAccessExpression(i18nextIdent, "t");
145+
return f.createCallExpression(tAccess, undefined, [f.createStringLiteral(hashId)]);
147146
};
148147

149148
const visitNode: ts.Visitor = (node) => {
@@ -214,6 +213,37 @@ export default function i18nMessagesTransformer(
214213
return ts.visitEachChild(node, visitNode, context);
215214
};
216215

217-
return (sf: ts.SourceFile) => ts.visitNode(sf, visitNode);
216+
// return (sf: ts.SourceFile) => ts.visitNode(sf, visitNode);
217+
return (sf: ts.SourceFile) => {
218+
// 1️⃣ Check if "import i18next from 'i18next'" already exists
219+
const alreadyImported = sf.statements.some(
220+
(stmt) =>
221+
ts.isImportDeclaration(stmt) &&
222+
stmt.moduleSpecifier &&
223+
ts.isStringLiteral(stmt.moduleSpecifier) &&
224+
stmt.moduleSpecifier.text === "i18next"
225+
);
226+
227+
// 2️⃣ Visit and transform nodes as before
228+
let updated = ts.visitNode(sf, visitNode);
229+
230+
// 3️⃣ If missing, prepend a new import
231+
if (!alreadyImported) {
232+
const importDecl = f.createImportDeclaration(
233+
undefined,
234+
undefined,
235+
f.createImportClause(
236+
false, // isTypeOnly
237+
f.createIdentifier("i18next"), // default import name
238+
undefined
239+
),
240+
f.createStringLiteral("i18next")
241+
);
242+
243+
updated = f.updateSourceFile(updated, [importDecl, ...updated.statements]);
244+
}
245+
246+
return updated;
247+
};
218248
};
219249
}

src/i18n/init.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import HttpApi from "i18next-http-backend";
44
import { defaultLanguage } from "./store";
55

66
export async function initializeTranslations(): Promise<void> {
7-
(window as any).i18next = i18next;
8-
97
await i18next.use(HttpApi).init({
108
backend: {
119
loadPath: "/locales/{{lng}}/{{ns}}.json",

src/i18n/store.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { create } from "zustand";
2+
import i18next from "i18next";
23

34
export type SupportedLanguages = "en" | "de";
45

@@ -16,11 +17,9 @@ export const useLanguageStore = create<LanguageState>()((set) => ({
1617

1718
export class LanguagePresenter {
1819
public async setLanguage(language: SupportedLanguages): Promise<void> {
19-
const i18next = (window as any).i18next;
2020
useLanguageStore.setState({ loading: true });
2121

2222
try {
23-
// await this.loadLanguage(language);
2423
await i18next.changeLanguage(language);
2524
useLanguageStore.setState({ language });
2625
} catch (err) {

src/views/MainView.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ export const MainView: React.FC = () => {
6161
<button onClick={() => languagePresenter.setLanguage("de")} disabled={languageIsLoading}>
6262
{languageIsLoading ? "loading..." : "use de"}
6363
</button>
64-
<h1>{(window as any).i18next.t("something")}</h1>
6564
<Header
6665
css={css`
6766
cursor: pointer;

0 commit comments

Comments
 (0)