astro-i18nya 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # astro-i18nya
2
+
3
+ To install dependencies:
4
+
5
+ ```bash
6
+ bun install
7
+ ```
8
+
9
+ To run:
10
+
11
+ ```bash
12
+ bun run index.ts
13
+ ```
14
+
15
+ This project was created using `bun init` in bun v1.3.2. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
@@ -0,0 +1,8 @@
1
+ module.exports = {
2
+ presets: [
3
+ ['@babel/preset-env', {targets: {node: 'current'}}],
4
+ '@babel/preset-typescript',
5
+
6
+ ['@babel/preset-react', {runtime: 'automatic'}],
7
+ ],
8
+ };
package/jest.config.ts ADDED
@@ -0,0 +1,7 @@
1
+ import type {Config} from 'jest';
2
+
3
+ const config: Config = {
4
+ "testEnvironment": "jsdom"
5
+ };
6
+
7
+ export default config;
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "astro-i18nya",
3
+ "version": "0.1.0",
4
+ "description": "Astro integration for i18nya: i18n as small as a cat's paw",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "test": "jest"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/FyraLabs/i18nya.git"
14
+ },
15
+ "keywords": [
16
+ "astro",
17
+ "internationalization",
18
+ "i18n",
19
+ "translation",
20
+ "localization",
21
+ "l10n",
22
+ "globalization"
23
+ ],
24
+ "author": "madonuko <mado@fyralabs.com>",
25
+ "license": "MIT",
26
+ "bugs": {
27
+ "url": "https://github.com/FyraLabs/i18nya/issues"
28
+ },
29
+ "homepage": "https://github.com/FyraLabs/i18nya#readme",
30
+ "devDependencies": {
31
+ "@babel/core": "^7.28.5",
32
+ "@babel/preset-env": "^7.28.5",
33
+ "@babel/preset-react": "^7.28.5",
34
+ "@babel/preset-typescript": "^7.28.5",
35
+ "@testing-library/jest-dom": "^6.9.1",
36
+ "@testing-library/react": "^16.3.0",
37
+ "@types/jest": "^30.0.0",
38
+ "@types/node": "^24.10.1",
39
+ "babel-jest": "^30.2.0",
40
+ "jest": "^30.2.0",
41
+ "react-test-renderer": "^19.2.0",
42
+ "typescript": "^5.9.3"
43
+ },
44
+ "peerDependencies": {
45
+ "typescript": "^5"
46
+ },
47
+ "dependencies": {
48
+ "@astrojs/react": "^4.4.2",
49
+ "astro": "^5.16.0",
50
+ "i18nya": "^0.1.0",
51
+ "jest-environment-jsdom": "^30.2.0",
52
+ "react": "^19.2.0"
53
+ }
54
+ }
@@ -0,0 +1,20 @@
1
+ import { render, screen } from "@testing-library/react";
2
+ import "@testing-library/jest-dom";
3
+ import Trans from "./Trans";
4
+
5
+ describe("<Trans />", () => {
6
+ it("renders nested elements according to translation string", () => {
7
+ const translation = "Hello <1>user</1>, welcome to <2><1>my site</1></2>.";
8
+ render(
9
+ <Trans t={translation}>
10
+ <b />
11
+ <a href="https://example.com" />
12
+ </Trans>,
13
+ );
14
+ expect(screen.getByText("my site")).toBeInTheDocument();
15
+ const bold = screen.getByText("user");
16
+ expect(bold.tagName).toBe("B");
17
+ const link = screen.getByText("my site").closest("a");
18
+ expect(link).toHaveAttribute("href", "https://example.com");
19
+ });
20
+ });
package/src/Trans.tsx ADDED
@@ -0,0 +1,86 @@
1
+ //? https://react.i18next.com/latest/trans-component
2
+ // do something similar to ↑
3
+
4
+ import { Children, cloneElement, isValidElement } from "react";
5
+ import type { FunctionComponent, ReactNode } from "react";
6
+
7
+ type Props = {
8
+ children: ReactNode;
9
+ t: string;
10
+ };
11
+
12
+ /**
13
+ * A fake version of `<Trans>` in `react-i18next` with *very different* behaviour.
14
+ *
15
+ * You feed in a list of empty elements in `<Trans>`. The structure will follow the translation strings.
16
+ *
17
+ * @example ```tsx
18
+ * <Trans t={t("test", { user: "John" })}>
19
+ * <b />
20
+ * <a href="https://example.com" />
21
+ * </Trans>
22
+ * ```
23
+ * With `"test": "Hello <1>{{user}}</1>, welcome to <2><1>my site</1></2>."`, the above element will become:
24
+ * ```tsx
25
+ * <b>Hello John</b>, welcome to <a href="https://example.com"><b>my site</b></a>.
26
+ * ```
27
+ *
28
+ * @param children the list of tags.
29
+ * @param t return value of t()
30
+ * @returns ReactNode
31
+ */
32
+ export default (({ children, t }: Props) => {
33
+ // find /<\/(\d+)>/g, where group 1 parse to int is largest
34
+ const maxTagId = Math.max(
35
+ ...t.match(/<\/(\d+)>/g).map((i) => parseInt(i.slice(2, -1))),
36
+ );
37
+ const inputs = Children.toArray(children).filter((c) => isValidElement(c));
38
+ if ((maxTagId ?? 0) > inputs.length) {
39
+ return t; // syntax error
40
+ }
41
+
42
+ const elms: ReactNode[] = []; // resulting list of elements
43
+ const tagStack = [];
44
+ for (let ch_idx = 0; ch_idx < t.length; ) {
45
+ if (t.substring(ch_idx, ch_idx + 2) == "\\<") {
46
+ elms.push("<");
47
+ ch_idx += 2;
48
+ continue;
49
+ }
50
+ if (t.substring(ch_idx, ch_idx + 2) == "</") {
51
+ let j = 0;
52
+ while (t[++j + ch_idx] != ">" && j + ch_idx < t.length);
53
+ const tag = Number.parseInt(t.substring(++ch_idx + 1, (ch_idx += j)));
54
+ if (Number.isNaN(tag)) {
55
+ elms.push(t.substring(ch_idx - j - 1, ch_idx));
56
+ continue;
57
+ }
58
+ let { p, l } = tagStack.pop();
59
+ if (tag != p) {
60
+ return t; // syntax error
61
+ }
62
+ elms.push(
63
+ cloneElement(inputs[p - 1], {}, ...elms.splice(l, elms.length - l)),
64
+ );
65
+ continue;
66
+ }
67
+ if (t[ch_idx] == "<") {
68
+ let j = 0;
69
+ while (t[++j + ch_idx] != ">" && j + ch_idx < t.length);
70
+ const tag = Number.parseInt(t.substring(++ch_idx, (ch_idx += j)));
71
+ if (Number.isNaN(tag)) {
72
+ elms.push(t.substring(ch_idx - j - 1, ch_idx));
73
+ continue;
74
+ }
75
+ tagStack.push({ p: tag, l: elms.length });
76
+ elms.push(""); // in order to splice later, contents inside a new tag element must start fresh
77
+ continue;
78
+ }
79
+ if (typeof elms[elms.length - 1] === "string") {
80
+ elms[elms.length - 1] += t[ch_idx++];
81
+ } else {
82
+ elms.push(t[ch_idx++]);
83
+ }
84
+ }
85
+ return <>{elms}</>;
86
+ }) satisfies FunctionComponent<Props>;
package/src/index.ts ADDED
@@ -0,0 +1,24 @@
1
+ import type { I18Nya } from "i18nya";
2
+ import type { AstroIntegration } from "astro";
3
+ import react from "@astrojs/react";
4
+ import Trans from "./Trans";
5
+ import { listLang, getLangName } from "./util";
6
+ export { Trans, listLang, getLangName };
7
+
8
+ export default function (i18nya: I18Nya): AstroIntegration {
9
+ return {
10
+ name: "astro-i18nya",
11
+ hooks: {
12
+ "astro:config:setup": ({ updateConfig }) => {
13
+ updateConfig({
14
+ i18n: {
15
+ defaultLocale: i18nya.config.defaultLang,
16
+ locales: Object.keys(i18nya.translations),
17
+ },
18
+ // required for <Trans>
19
+ integrations: [react({ experimentalReactChildren: true })],
20
+ });
21
+ },
22
+ },
23
+ };
24
+ }
package/src/util.ts ADDED
@@ -0,0 +1,24 @@
1
+ import type { I18Nya } from "i18nya";
2
+
3
+ /**
4
+ * Obtain the language name.
5
+ * @param lang the language locale
6
+ * @param displayLang in what language should the name of the language be shown. By default, use native language names.
7
+ * @returns language name
8
+ */
9
+ export const getLangName = (lang: string, displayLang?: string) =>
10
+ new Intl.DisplayNames([displayLang ?? lang], { type: "language" }).of(lang);
11
+
12
+ /**
13
+ * List out available languages.
14
+ * @param i18nya return value of `init()` from `i18nya`
15
+ * @param displayLang in what language should the name of the language be shown. By default, use native language names.
16
+ * @returns a map of language locale → language name
17
+ */
18
+ export const listLang = (i18nya: I18Nya, displayLang?: string) =>
19
+ new Map(
20
+ Object.keys(i18nya.translations).map((l) => [
21
+ l,
22
+ getLangName(l.replace("_", "-"), displayLang),
23
+ ]),
24
+ );
package/tsconfig.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "exclude": ["./dist"],
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "outDir": "./dist",
6
+
7
+ // Environment setup & latest features
8
+ "lib": ["ESNext"],
9
+ "target": "ESNext",
10
+ "module": "Preserve",
11
+ "moduleDetection": "force",
12
+ "jsx": "react-jsx",
13
+ "types": ["node"],
14
+ "allowJs": true,
15
+
16
+ // Output settings
17
+ "sourceMap": true,
18
+ "declaration": true,
19
+ "declarationMap": true,
20
+
21
+ // Module resolution
22
+ "moduleResolution": "bundler",
23
+ "allowImportingTsExtensions": true,
24
+ "noEmit": true,
25
+ "verbatimModuleSyntax": false,
26
+ "isolatedModules": true,
27
+ "resolveJsonModule": true,
28
+ "esModuleInterop": true,
29
+
30
+ // Best practices
31
+ "strict": true,
32
+ "skipLibCheck": true,
33
+ "noFallthroughCasesInSwitch": true,
34
+ "noUncheckedIndexedAccess": true,
35
+ "strictNullChecks": false,
36
+ // "exactOptionalPropertyTypes": true,
37
+ "noImplicitOverride": true,
38
+ "noUncheckedSideEffectImports": true,
39
+
40
+ // Some stricter flags (disabled by default)
41
+ "noUnusedLocals": false,
42
+ "noUnusedParameters": false,
43
+ "noPropertyAccessFromIndexSignature": false
44
+ }
45
+ }