Skip to content

Commit

Permalink
fix: i18next typings (close #25) (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-lefebvre authored Jun 4, 2024
1 parent 80d8c97 commit ef0b3ee
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-lizards-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrolicious/i18n": patch
---

Fixes an case where i18next typings would not be correct
5 changes: 3 additions & 2 deletions docs/src/content/docs/getting-started/installation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions package/src/i18next/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion package/src/i18next/namespaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const getNamespaces = (
}

return {
importsData,
namespaces,
};
};
28 changes: 5 additions & 23 deletions package/src/i18next/types.ts
Original file line number Diff line number Diff line change
@@ -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<typeof getNamespaces>["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 {}
Expand Down
3 changes: 2 additions & 1 deletion playground/src/locales/en/home.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"title": "Home",
"description": "This is the home, see {{ value }}"
"description": "This is the home, see {{value}}",
"array": ["a", "b", "c"]
}
3 changes: 2 additions & 1 deletion playground/src/locales/fr/home.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"title": "Accueil",
"description": "Voici l'accueil, voir {{ value }}"
"description": "Voici l'accueil, voir {{value}}",
"array": ["a", "b", "c"]
}
1 change: 1 addition & 0 deletions playground/src/routes/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
---

<Layout {title}>
Expand Down

0 comments on commit ef0b3ee

Please sign in to comment.