@comvi/next 0.2.0 → 0.3.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 +57 -1
- package/dist/client/I18nProvider.d.ts.map +1 -1
- package/dist/client/I18nProvider.js +25 -9
- package/dist/client.d.ts +2 -2
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +2 -2
- package/dist/routing/Link.js +2 -2
- package/dist/routing/hooks.js +3 -3
- package/package.json +13 -17
- package/dist/_virtual/_rolldown/runtime.cjs +0 -23
- package/dist/client/I18nProvider.cjs +0 -101
- package/dist/client/index.d.ts +0 -5
- package/dist/client/index.d.ts.map +0 -1
- package/dist/client.cjs +0 -31
- package/dist/createNextI18n.cjs +0 -153
- package/dist/index.cjs +0 -17
- package/dist/middleware/createMiddleware.cjs +0 -185
- package/dist/middleware.cjs +0 -3
- package/dist/navigation.cjs +0 -8
- package/dist/routing/Link.cjs +0 -42
- package/dist/routing/context.cjs +0 -21
- package/dist/routing/defineRouting.cjs +0 -141
- package/dist/routing/hooks.cjs +0 -104
- package/dist/routing/utils.cjs +0 -94
- package/dist/routing.cjs +0 -5
- package/dist/server/cache.cjs +0 -69
- package/dist/server/ensureInitialized.cjs +0 -19
- package/dist/server/getI18n.cjs +0 -115
- package/dist/server/getLocale.cjs +0 -37
- package/dist/server/loadTranslations.cjs +0 -54
- package/dist/server/setRequestLocale.cjs +0 -31
- package/dist/server.cjs +0 -11
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
//#region src/server/ensureInitialized.ts
|
|
2
|
-
var initPromises = /* @__PURE__ */ new WeakMap();
|
|
3
|
-
/**
|
|
4
|
-
* Ensure i18n instance is initialized, deduplicating concurrent calls.
|
|
5
|
-
* @internal
|
|
6
|
-
*/
|
|
7
|
-
var ensureInitialized = async (i18n) => {
|
|
8
|
-
if (i18n.isInitialized) return;
|
|
9
|
-
let initPromise = initPromises.get(i18n);
|
|
10
|
-
if (!initPromise) {
|
|
11
|
-
initPromise = i18n.init().then(() => void 0).finally(() => {
|
|
12
|
-
if (initPromises.get(i18n) === initPromise) initPromises.delete(i18n);
|
|
13
|
-
});
|
|
14
|
-
initPromises.set(i18n, initPromise);
|
|
15
|
-
}
|
|
16
|
-
await initPromise;
|
|
17
|
-
};
|
|
18
|
-
//#endregion
|
|
19
|
-
exports.ensureInitialized = ensureInitialized;
|
package/dist/server/getI18n.cjs
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
|
-
const require_cache = require("./cache.cjs");
|
|
3
|
-
const require_ensureInitialized = require("./ensureInitialized.cjs");
|
|
4
|
-
const require_getLocale = require("./getLocale.cjs");
|
|
5
|
-
const require_loadTranslations = require("./loadTranslations.cjs");
|
|
6
|
-
let _comvi_core = require("@comvi/core");
|
|
7
|
-
//#region src/server/getI18n.ts
|
|
8
|
-
var virtualNodeToText = (node) => {
|
|
9
|
-
if (node.type === "text") return node.text;
|
|
10
|
-
let text = "";
|
|
11
|
-
for (const child of node.children) {
|
|
12
|
-
if (typeof child === "string") {
|
|
13
|
-
text += child;
|
|
14
|
-
continue;
|
|
15
|
-
}
|
|
16
|
-
text += virtualNodeToText(child);
|
|
17
|
-
}
|
|
18
|
-
return text;
|
|
19
|
-
};
|
|
20
|
-
var translationResultToString = (result) => {
|
|
21
|
-
if (typeof result === "string") return result;
|
|
22
|
-
let text = "";
|
|
23
|
-
for (const part of result) text += typeof part === "string" ? part : virtualNodeToText(part);
|
|
24
|
-
return text;
|
|
25
|
-
};
|
|
26
|
-
/**
|
|
27
|
-
* Get i18n for use in Server Components, Server Actions, and Route Handlers
|
|
28
|
-
*
|
|
29
|
-
* This function uses the global i18n instance (configured via setI18n) and
|
|
30
|
-
* automatically reads the locale from the request context (set by setRequestLocale
|
|
31
|
-
* or middleware).
|
|
32
|
-
*
|
|
33
|
-
* @param options - Options object with locale and namespace
|
|
34
|
-
* @returns Object with t() function and hasTranslation() helper
|
|
35
|
-
*
|
|
36
|
-
* @example
|
|
37
|
-
* ```tsx
|
|
38
|
-
* // Server Component - using keys from default namespace
|
|
39
|
-
* import { getI18n } from '@comvi/next/server';
|
|
40
|
-
*
|
|
41
|
-
* export default async function HomePage() {
|
|
42
|
-
* const { t } = await getI18n();
|
|
43
|
-
* return (
|
|
44
|
-
* <div>
|
|
45
|
-
* <h1>{t('home.title')}</h1>
|
|
46
|
-
* <p>{t('common.description')}</p>
|
|
47
|
-
* </div>
|
|
48
|
-
* );
|
|
49
|
-
* }
|
|
50
|
-
* ```
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* ```tsx
|
|
54
|
-
* // Using a different namespace
|
|
55
|
-
* const { t } = await getI18n();
|
|
56
|
-
* // Access admin namespace translations
|
|
57
|
-
* t('title', { ns: 'admin' }) // → "Admin Dashboard"
|
|
58
|
-
* t('roles.admin', { ns: 'admin' }) // → "Administrator"
|
|
59
|
-
* ```
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
62
|
-
* ```tsx
|
|
63
|
-
* // With explicit locale (for generateMetadata)
|
|
64
|
-
* export async function generateMetadata({ params }: { params: Promise<{ locale: string }> }) {
|
|
65
|
-
* const { locale } = await params;
|
|
66
|
-
* const { t } = await getI18n({ locale });
|
|
67
|
-
* return { title: t('common.title') };
|
|
68
|
-
* }
|
|
69
|
-
* ```
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```tsx
|
|
73
|
-
* // Check if translation exists (with namespace support)
|
|
74
|
-
* const { t, hasTranslation } = await getI18n();
|
|
75
|
-
* hasTranslation('common.title') // true (default namespace)
|
|
76
|
-
* hasTranslation('title', { ns: 'admin' }) // true (admin namespace)
|
|
77
|
-
* ```
|
|
78
|
-
*
|
|
79
|
-
* @remarks
|
|
80
|
-
* getI18n auto-loads only the default namespace (or the namespace passed via
|
|
81
|
-
* getI18n({ ns })). If you call t() with a different ns, ensure you preloaded it
|
|
82
|
-
* via loadTranslations(locale, { namespaces: [...] }) in your layout or metadata.
|
|
83
|
-
*/
|
|
84
|
-
async function getI18n(options) {
|
|
85
|
-
const i18n = require_cache.getI18nInstance();
|
|
86
|
-
await require_ensureInitialized.ensureInitialized(i18n);
|
|
87
|
-
let locale = options?.locale;
|
|
88
|
-
if (!locale) try {
|
|
89
|
-
locale = await require_getLocale.getLocale();
|
|
90
|
-
} catch (e) {
|
|
91
|
-
const err = /* @__PURE__ */ new Error("[comvi/next] Locale not set. Call setRequestLocale(locale) in your layout/page first, or configure middleware.");
|
|
92
|
-
err.cause = e;
|
|
93
|
-
throw err;
|
|
94
|
-
}
|
|
95
|
-
const defaultNs = options?.ns ?? i18n.getDefaultNamespace();
|
|
96
|
-
if (!i18n.hasLocale(locale, defaultNs)) await require_loadTranslations.loadTranslations(locale, { namespaces: [defaultNs] });
|
|
97
|
-
const translate = (0, _comvi_core.createBoundTranslation)(i18n, defaultNs);
|
|
98
|
-
const t = ((key, params) => {
|
|
99
|
-
return translationResultToString(translate(key, {
|
|
100
|
-
...params,
|
|
101
|
-
locale
|
|
102
|
-
}));
|
|
103
|
-
});
|
|
104
|
-
const hasTranslation = (key, opts) => {
|
|
105
|
-
const checkLocale = opts?.locale ?? locale;
|
|
106
|
-
const checkNs = opts?.ns ?? defaultNs;
|
|
107
|
-
return i18n.hasTranslation(key, checkLocale, checkNs);
|
|
108
|
-
};
|
|
109
|
-
return {
|
|
110
|
-
t,
|
|
111
|
-
hasTranslation
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
//#endregion
|
|
115
|
-
exports.getI18n = getI18n;
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
|
-
const require_cache = require("./cache.cjs");
|
|
3
|
-
let next_headers = require("next/headers");
|
|
4
|
-
//#region src/server/getLocale.ts
|
|
5
|
-
/**
|
|
6
|
-
* Header name set by middleware to pass locale to Server Components
|
|
7
|
-
*/
|
|
8
|
-
var LOCALE_HEADER = "x-comvi-locale";
|
|
9
|
-
/**
|
|
10
|
-
* Get the current request locale
|
|
11
|
-
*
|
|
12
|
-
* This function reads the locale from:
|
|
13
|
-
* 1. Request cache (set by setRequestLocale)
|
|
14
|
-
* 2. x-comvi-locale header (set by middleware)
|
|
15
|
-
*
|
|
16
|
-
* @returns The current locale
|
|
17
|
-
* @throws Error if locale cannot be determined
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```tsx
|
|
21
|
-
* import { getLocale } from '@comvi/next/server';
|
|
22
|
-
*
|
|
23
|
-
* export default async function Page() {
|
|
24
|
-
* const locale = await getLocale();
|
|
25
|
-
* return <p>Current locale: {locale}</p>;
|
|
26
|
-
* }
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
async function getLocale() {
|
|
30
|
-
const cachedLocale = require_cache.getRequestLocaleFromCache();
|
|
31
|
-
if (cachedLocale) return cachedLocale;
|
|
32
|
-
const localeFromHeader = (await (0, next_headers.headers)()).get(LOCALE_HEADER);
|
|
33
|
-
if (localeFromHeader) return localeFromHeader;
|
|
34
|
-
throw new Error("[comvi/next] Unable to determine locale. Make sure to call setRequestLocale() in your layout/page or configure middleware.");
|
|
35
|
-
}
|
|
36
|
-
//#endregion
|
|
37
|
-
exports.getLocale = getLocale;
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
const require_cache = require("./cache.cjs");
|
|
2
|
-
const require_ensureInitialized = require("./ensureInitialized.cjs");
|
|
3
|
-
//#region src/server/loadTranslations.ts
|
|
4
|
-
var toError = (error) => error instanceof Error ? error : new Error(String(error));
|
|
5
|
-
var noLoaderWarnings = /* @__PURE__ */ new WeakSet();
|
|
6
|
-
var NO_LOADER_WARNING_MESSAGE = "[comvi/next] No loader configured. Register one via i18n.registerLoader(...) or createNextI18n(...).use(plugin).";
|
|
7
|
-
var toPlainObject = (value) => {
|
|
8
|
-
return Object.fromEntries(Object.entries(value));
|
|
9
|
-
};
|
|
10
|
-
var warnNoLoaderConfigured = (i18n) => {
|
|
11
|
-
if (noLoaderWarnings.has(i18n)) return;
|
|
12
|
-
noLoaderWarnings.add(i18n);
|
|
13
|
-
console.warn(NO_LOADER_WARNING_MESSAGE);
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* Load translations for a locale using the configured i18n loader.
|
|
17
|
-
*
|
|
18
|
-
* If no loader is configured, this function returns already-cached translations
|
|
19
|
-
* for requested namespaces (if any) and logs a warning.
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* ```typescript
|
|
23
|
-
* const messages = await loadTranslations(locale);
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```typescript
|
|
28
|
-
* const messages = await loadTranslations(locale, {
|
|
29
|
-
* namespaces: ["common", "admin"],
|
|
30
|
-
* });
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
async function loadTranslations(locale, options = {}) {
|
|
34
|
-
const i18n = require_cache.getI18nInstance();
|
|
35
|
-
await require_ensureInitialized.ensureInitialized(i18n);
|
|
36
|
-
const defaultNs = i18n.getDefaultNamespace();
|
|
37
|
-
const namespaces = options.namespaces ?? [defaultNs];
|
|
38
|
-
const hasLoader = Boolean(i18n.getLoader());
|
|
39
|
-
const result = {};
|
|
40
|
-
for (const namespace of namespaces) {
|
|
41
|
-
const cacheKey = `${locale}:${namespace}`;
|
|
42
|
-
if (!i18n.hasLocale(locale, namespace) && hasLoader) try {
|
|
43
|
-
await i18n.reloadTranslations(locale, namespace);
|
|
44
|
-
} catch (error) {
|
|
45
|
-
const err = toError(error);
|
|
46
|
-
console.warn(`[comvi/next] Failed to load ${locale}:${namespace}:`, err.message);
|
|
47
|
-
}
|
|
48
|
-
if (i18n.hasLocale(locale, namespace)) result[cacheKey] = toPlainObject(i18n.getTranslations(locale, namespace));
|
|
49
|
-
}
|
|
50
|
-
if (!hasLoader && Object.keys(result).length === 0) warnNoLoaderConfigured(i18n);
|
|
51
|
-
return result;
|
|
52
|
-
}
|
|
53
|
-
//#endregion
|
|
54
|
-
exports.loadTranslations = loadTranslations;
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
const require_cache = require("./cache.cjs");
|
|
2
|
-
//#region src/server/setRequestLocale.ts
|
|
3
|
-
/**
|
|
4
|
-
* Enable static rendering for internationalized pages
|
|
5
|
-
*
|
|
6
|
-
* Call this at the top of your layout and page components before
|
|
7
|
-
* using any translation functions. This is required for static
|
|
8
|
-
* rendering with generateStaticParams().
|
|
9
|
-
*
|
|
10
|
-
* @param locale - The locale for this request (typically from params.locale)
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```tsx
|
|
14
|
-
* // app/[locale]/page.tsx
|
|
15
|
-
* import { setRequestLocale } from '@comvi/next/server';
|
|
16
|
-
*
|
|
17
|
-
* export default async function Page({ params }: { params: Promise<{ locale: string }> }) {
|
|
18
|
-
* const { locale } = await params;
|
|
19
|
-
* setRequestLocale(locale);
|
|
20
|
-
*
|
|
21
|
-
* // Now you can use getTranslations()
|
|
22
|
-
* const t = await getTranslations('HomePage');
|
|
23
|
-
* return <h1>{t('title')}</h1>;
|
|
24
|
-
* }
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
function setRequestLocale(locale) {
|
|
28
|
-
require_cache.setRequestLocaleInternal(locale);
|
|
29
|
-
}
|
|
30
|
-
//#endregion
|
|
31
|
-
exports.setRequestLocale = setRequestLocale;
|
package/dist/server.cjs
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_cache = require("./server/cache.cjs");
|
|
3
|
-
const require_setRequestLocale = require("./server/setRequestLocale.cjs");
|
|
4
|
-
const require_getLocale = require("./server/getLocale.cjs");
|
|
5
|
-
const require_loadTranslations = require("./server/loadTranslations.cjs");
|
|
6
|
-
const require_getI18n = require("./server/getI18n.cjs");
|
|
7
|
-
exports.getI18n = require_getI18n.getI18n;
|
|
8
|
-
exports.getLocale = require_getLocale.getLocale;
|
|
9
|
-
exports.loadTranslations = require_loadTranslations.loadTranslations;
|
|
10
|
-
exports.setI18n = require_cache.setI18n;
|
|
11
|
-
exports.setRequestLocale = require_setRequestLocale.setRequestLocale;
|