From ef0b3eedda6b29babc3fefcaff1ec07b322c258e Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Tue, 4 Jun 2024 18:08:31 +0200 Subject: [PATCH] fix: i18next typings (close #25) (#29) --- .changeset/honest-lizards-nail.md | 5 ++++ .../docs/getting-started/installation.mdx | 5 ++-- package/package.json | 5 +--- package/src/i18next/index.ts | 4 +-- package/src/i18next/namespaces.ts | 1 - package/src/i18next/types.ts | 28 ++++--------------- playground/src/locales/en/home.json | 3 +- playground/src/locales/fr/home.json | 3 +- playground/src/routes/index.astro | 1 + 9 files changed, 21 insertions(+), 34 deletions(-) create mode 100644 .changeset/honest-lizards-nail.md diff --git a/.changeset/honest-lizards-nail.md b/.changeset/honest-lizards-nail.md new file mode 100644 index 0000000..d3e8865 --- /dev/null +++ b/.changeset/honest-lizards-nail.md @@ -0,0 +1,5 @@ +--- +"@astrolicious/i18n": patch +--- + +Fixes an case where i18next typings would not be correct diff --git a/docs/src/content/docs/getting-started/installation.mdx b/docs/src/content/docs/getting-started/installation.mdx index 0f6ac6b..58c8732 100644 --- a/docs/src/content/docs/getting-started/installation.mdx +++ b/docs/src/content/docs/getting-started/installation.mdx @@ -4,8 +4,9 @@ title: Installation import { Tabs, TabItem } from '@astrojs/starlight/components'; -:::caution -This package requires `astro` to be at least on version `4.4.11`. +:::caution[Prerequisites] +- `astro` must be at least on version `4.4.11` +- `tsconfig.json` `compilerOptions.strict` should be set to `true`. If you're using Astro tsconfig presets, use either `astro/tsconfigs/strict` or `astro/tsconfigs/strictest` ::: ## Automatic installation diff --git a/package/package.json b/package/package.json index 7b04113..80a02ef 100644 --- a/package/package.json +++ b/package/package.json @@ -38,10 +38,7 @@ "./components/I18nClient.astro": "./assets/components/I18nClient.astro", "./components/I18nHead.astro": "./assets/components/I18nHead.astro" }, - "files": [ - "dist", - "assets" - ], + "files": ["dist", "assets"], "scripts": { "dev": "tsup --watch", "build": "tsup" diff --git a/package/src/i18next/index.ts b/package/src/i18next/index.ts index cd5aa17..723c44c 100644 --- a/package/src/i18next/index.ts +++ b/package/src/i18next/index.ts @@ -33,13 +33,13 @@ export const handleI18next = defineUtility("astro:config:setup")( )}" directory`, ); - const { namespaces, importsData } = getNamespaces( + const { namespaces } = getNamespaces( paths.defaultLocalesDir, options.defaultNamespace, logger, ); const resources = getResources(logger, options, paths.localesDir); - injectTypes(params, options, importsData, paths.defaultLocalesDir); + injectTypes(params, options, resources[options.defaultLocale] ?? {}); return { namespaces, diff --git a/package/src/i18next/namespaces.ts b/package/src/i18next/namespaces.ts index 505a180..f7384af 100644 --- a/package/src/i18next/namespaces.ts +++ b/package/src/i18next/namespaces.ts @@ -33,7 +33,6 @@ export const getNamespaces = ( } return { - importsData, namespaces, }; }; diff --git a/package/src/i18next/types.ts b/package/src/i18next/types.ts index ce03ca3..d4f9ed1 100644 --- a/package/src/i18next/types.ts +++ b/package/src/i18next/types.ts @@ -1,38 +1,20 @@ -import { join, relative } from "node:path"; -import { fileURLToPath } from "node:url"; import { addDts, defineUtility } from "astro-integration-kit"; -import { normalizePath } from "vite"; import type { Options } from "../options.js"; -import type { getNamespaces } from "./namespaces.js"; +import type { I18nextConfig } from "../types.js"; export const injectTypes = defineUtility("astro:config:setup")( ( params, { defaultNamespace }: Options, - importsData: ReturnType["importsData"], - defaultLocalesDir: string, + resources: I18nextConfig["resources"][string], ) => { - const relativeLocalesPrefix = `${normalizePath( - relative( - fileURLToPath(new URL("./.astro/", params.config.root)), - defaultLocalesDir, - ), - )}/`; - const content = ` + type Resources = ${JSON.stringify(resources)} + declare module "i18next" { interface CustomTypeOptions { defaultNS: "${defaultNamespace}"; - resources: { - ${importsData - .map( - ({ namespaceName, fileName }) => - `"${namespaceName}": typeof import("${normalizePath( - join(relativeLocalesPrefix, fileName), - )}");`, - ) - .join("\n")} - } + resources: Resources; } } export {} diff --git a/playground/src/locales/en/home.json b/playground/src/locales/en/home.json index 4a7d93b..2440f31 100644 --- a/playground/src/locales/en/home.json +++ b/playground/src/locales/en/home.json @@ -1,4 +1,5 @@ { "title": "Home", - "description": "This is the home, see {{ value }}" + "description": "This is the home, see {{value}}", + "array": ["a", "b", "c"] } diff --git a/playground/src/locales/fr/home.json b/playground/src/locales/fr/home.json index 8f943e2..710ea5d 100644 --- a/playground/src/locales/fr/home.json +++ b/playground/src/locales/fr/home.json @@ -1,4 +1,5 @@ { "title": "Accueil", - "description": "Voici l'accueil, voir {{ value }}" + "description": "Voici l'accueil, voir {{value}}", + "array": ["a", "b", "c"] } diff --git a/playground/src/routes/index.astro b/playground/src/routes/index.astro index d1c27a3..e91c4c5 100644 --- a/playground/src/routes/index.astro +++ b/playground/src/routes/index.astro @@ -6,6 +6,7 @@ const title = t("home:title"); console.log(getLocalePath("/about", null, "fr")); console.log(getLocalePath("/abc", null, "en")); console.log(getLocalePath("/def", null, "fr")); +console.log(t("home:array", { returnObjects: true })); ---