Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"generate:types": "tsx scripts/generate-types.ts",
"sync:check": "tsx scripts/sync-check.ts",
"check:sync": "tsx scripts/sync-check.ts --ci",
"test": "pnpm run build && tsx --test scripts/__tests__/*.test.ts",
"check:lint": "oxlint --max-warnings=9 .",
"check:types": "pnpm run generate:types && tsc --noEmit",
"check:format": "oxfmt --check .",
Expand Down
33 changes: 33 additions & 0 deletions packages/i18n/scripts/__tests__/locale-assets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2023-present Plane Software, Inc. and contributors
* SPDX-License-Identifier: AGPL-3.0-only
* See the LICENSE file for details.
*/

import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import test from "node:test";

const PACKAGE_ROOT = path.resolve(import.meta.dirname, "../..");

const listJsonAssets = (root: string): string[] =>
fs
.readdirSync(root, { recursive: true, withFileTypes: true })
.filter((entry) => entry.isFile() && entry.name.endsWith(".json"))
.map((entry) => path.relative(root, path.join(entry.parentPath, entry.name)).replaceAll("\\", "/"))
.toSorted();
Comment thread
bulunote marked this conversation as resolved.

test("the built package includes every locale asset", () => {
const sourceLocaleRoot = path.join(PACKAGE_ROOT, "src/locales");
const builtLocaleRoot = path.join(PACKAGE_ROOT, "dist/locales");

assert.equal(fs.existsSync(builtLocaleRoot), true, "expected dist/locales to be created by the package build");
assert.deepEqual(listJsonAssets(builtLocaleRoot), listJsonAssets(sourceLocaleRoot));
});

test("the built entry resolves dynamic locale imports inside dist", () => {
const builtEntry = fs.readFileSync(path.join(PACKAGE_ROOT, "dist/index.js"), "utf8");

assert.match(builtEntry, /import\(`\.\/locales\/\$\{[^}]+\}\/\$\{[^}]+\}\.json`\)/);
});
1 change: 1 addition & 0 deletions packages/i18n/scripts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"target": "ES2022",
"lib": ["ES2023"],
"module": "Node16",
"moduleResolution": "Node16",
"esModuleInterop": true,
Expand Down
7 changes: 4 additions & 3 deletions packages/i18n/src/core/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See the LICENSE file for details.
*/

import i18n from "i18next";
import { createInstance } from "i18next";
import { initReactI18next } from "react-i18next";
import ICU from "i18next-icu";
import resourcesToBackend from "i18next-resources-to-backend";
Expand All @@ -13,12 +13,13 @@ import { NAMESPACES, DEFAULT_NAMESPACE } from "../constants/namespaces";

import type { i18n as I18nInstance } from "i18next";

export const i18nInstance: I18nInstance = i18n.createInstance();
export const i18nInstance: I18nInstance = createInstance();

i18nInstance
.use(ICU)
.use(initReactI18next)
.use(resourcesToBackend((language: string, namespace: string) => import(`../locales/${language}/${namespace}.json`)));
// Locale assets are copied beside the bundled dist entry by tsdown.
.use(resourcesToBackend((language: string, namespace: string) => import(`./locales/${language}/${namespace}.json`)));

const initialLng =
typeof window !== "undefined" ? localStorage.getItem(LANGUAGE_STORAGE_KEY) || FALLBACK_LANGUAGE : FALLBACK_LANGUAGE;
Expand Down
1 change: 1 addition & 0 deletions packages/i18n/tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default defineConfig({
entry: ["src/index.ts"],
format: ["esm"],
dts: true,
copy: ["src/locales"],
platform: "neutral",
exports: true,
});