Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
fix: i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
jwcub committed Mar 2, 2024
1 parent b722655 commit dda4b98
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 181 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ concurrency:
cancel-in-progress: true

jobs:
bench:
test:
runs-on: ubuntu-latest
steps:
- name: ⬇️ Checkout repo
Expand Down
5 changes: 0 additions & 5 deletions app/entry.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { CacheProvider } from "@emotion/react";
import { RemixBrowser } from "@remix-run/react";
import i18next from "i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import Backend from "i18next-http-backend";
import { startTransition, StrictMode } from "react";
import { hydrateRoot } from "react-dom/client";
import { I18nextProvider, initReactI18next } from "react-i18next";
Expand All @@ -17,12 +16,8 @@ const cache = createCache({ key: "-", stylisPlugins: [] });
i18next
.use(initReactI18next)
.use(LanguageDetector)
.use(Backend)
.init({
...i18n,
backend: {
loadPath: "/locales/{{lng}}/{{ns}}.json"
},
detection: {
order: ["cookie", "htmlTag"],
caches: ["cookie"],
Expand Down
10 changes: 1 addition & 9 deletions app/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { resolve } from "node:path";
import { PassThrough } from "node:stream";

import createCache from "@emotion/cache";
Expand All @@ -11,7 +10,6 @@ import type {
import { createReadableStreamFromReadable } from "@remix-run/node";
import { RemixServer } from "@remix-run/react";
import i18next from "i18next";
import Backend from "i18next-fs-backend";
import { isbot } from "isbot";
import { renderToPipeableStream } from "react-dom/server";
import { I18nextProvider, initReactI18next } from "react-i18next";
Expand All @@ -28,13 +26,7 @@ import { getLocale } from "./i18n/i18next.server";
const ABORT_DELAY = 6590;

// Set up i18next server backend.
await i18next
.use(initReactI18next)
.use(Backend)
.init({
...i18n,
backend: { loadPath: resolve("./public/locales/{{lng}}/{{ns}}.json") }
});
await i18next.use(initReactI18next).init(i18n);

/**
* Handles incoming requests.
Expand Down
19 changes: 17 additions & 2 deletions app/i18n/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import type { ServerRuntimeMetaArgs } from "@remix-run/server-runtime";
import type { InitOptions } from "i18next";
import { getFixedT } from "i18next";

import en from "@/static/locales/en.json";
import zh from "@/static/locales/zh.json";

const ns = "translations";

export default {
supportedLngs: ["zh", "en"],
fallbackLng: "en"
};
fallbackLng: "en",
ns,
resources: {
en: {
[ns]: en
},
zh: {
[ns]: zh
}
}
} satisfies InitOptions;

export function getT<MatchLoaders>(
matches: ServerRuntimeMetaArgs<MatchLoaders>["matches"]
Expand Down
8 changes: 5 additions & 3 deletions app/i18n/i18next.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { ParseKeys } from "i18next";

import type translation from "@/public/locales/en/translation.json";
import type translation from "@/static/locales/en.json";

import type i18n from "./i18n";

declare module "i18next" {
interface CustomTypeOptions {
defaultNS: "translation";
defaultNS: i18n.ns;
resources: {
translation: typeof translation;
};
}
}

type TFunctionArg = ParseKeys<"translation">;
type TFunctionArg = ParseKeys<i18n.ns>;
11 changes: 1 addition & 10 deletions app/i18n/i18next.server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { resolve } from "node:path";

import Backend from "i18next-fs-backend";
import { RemixI18Next } from "remix-i18next/server";

import { getCookieValue } from "~/hooks/cookie";
Expand All @@ -13,13 +10,7 @@ export const i18next = new RemixI18Next({
fallbackLanguage: i18n.fallbackLng,
order: ["cookie", "header"]
},
i18next: {
...i18n,
backend: {
loadPath: resolve("./public/locales/{{lng}}/{{ns}}.json")
}
},
plugins: [Backend]
i18next: i18n
});

const I18NEXT_KEY = "i18next";
Expand Down
2 changes: 0 additions & 2 deletions app/theme/global.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
p {
margin-top: 1rem;

&:first-child {
margin-top: 0;
}
Expand Down
Loading

0 comments on commit dda4b98

Please sign in to comment.