@rimelight/i18n 0.0.7 → 0.0.9
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/dist/hono.d.mts +8 -0
- package/dist/hono.mjs +80 -0
- package/dist/index.d.mts +32 -32
- package/dist/index.mjs +4 -63
- package/dist/plugin-CFEm0e4U.d.mts +39 -0
- package/dist/plugin-nQl4lZKv.mjs +55 -0
- package/dist/plugin.d.mts +2 -0
- package/dist/plugin.mjs +2 -0
- package/dist/runtime-Bc8I8sA4.d.mts +72 -0
- package/dist/runtime-Civir_tv.mjs +183 -0
- package/dist/runtime.d.mts +2 -71
- package/dist/runtime.mjs +2 -179
- package/dist/src-D09inETW.mjs +121 -0
- package/dist/types-BKUPwH-e.d.mts +32 -0
- package/dist/types.d.mts +2 -16
- package/dist/utils.d.mts +7 -2
- package/dist/utils.mjs +16 -1
- package/package.json +7 -16
- package/dist/integration.d.mts +0 -11
- package/dist/integration.mjs +0 -122
- package/dist/middleware.d.mts +0 -7
- package/dist/middleware.mjs +0 -56
package/dist/hono.d.mts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
//#region src/hono.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Hono middleware for Rimelight i18n. Handles locale routing, redirection, currentLocale store
|
|
4
|
+
* updates, and Cloudflare KV binding setup.
|
|
5
|
+
*/
|
|
6
|
+
declare function i18n(): (c: any, next: any) => Promise<any>;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { i18n };
|
package/dist/hono.mjs
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { n as currentLocale } from "./runtime-Civir_tv.mjs";
|
|
2
|
+
import { d as setKVBinding } from "./src-D09inETW.mjs";
|
|
3
|
+
import { env } from "cloudflare:workers";
|
|
4
|
+
import { defaultLocale, locales, prefixDefaultLocale } from "virtual:rimelight-i18n-config";
|
|
5
|
+
//#region src/hono.ts
|
|
6
|
+
const SUPPORTED_LOCALES = new Set(locales);
|
|
7
|
+
const DEFAULT_LOCALE = defaultLocale;
|
|
8
|
+
function getPreferredLocale(acceptLanguage) {
|
|
9
|
+
if (!acceptLanguage) return DEFAULT_LOCALE;
|
|
10
|
+
const parsed = acceptLanguage.split(",").map((lang) => {
|
|
11
|
+
const parts = lang.split(";");
|
|
12
|
+
const code = (parts[0] ?? "").trim().toLowerCase();
|
|
13
|
+
const base = code.split("-")[0] ?? "";
|
|
14
|
+
let q = 1;
|
|
15
|
+
const qualityPart = parts[1];
|
|
16
|
+
if (qualityPart) {
|
|
17
|
+
const qMatch = qualityPart.match(/q=([0-9.]+)/);
|
|
18
|
+
if (qMatch) q = parseFloat(qMatch[1] ?? "1");
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
code,
|
|
22
|
+
base,
|
|
23
|
+
q
|
|
24
|
+
};
|
|
25
|
+
}).toSorted((a, b) => b.q - a.q);
|
|
26
|
+
for (const item of parsed) {
|
|
27
|
+
if (SUPPORTED_LOCALES.has(item.code)) return item.code;
|
|
28
|
+
const base = item.base;
|
|
29
|
+
if (SUPPORTED_LOCALES.has(base)) return base;
|
|
30
|
+
}
|
|
31
|
+
return DEFAULT_LOCALE;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Hono middleware for Rimelight i18n. Handles locale routing, redirection, currentLocale store
|
|
35
|
+
* updates, and Cloudflare KV binding setup.
|
|
36
|
+
*/
|
|
37
|
+
function i18n() {
|
|
38
|
+
return async (c, next) => {
|
|
39
|
+
try {
|
|
40
|
+
const kv = Reflect.get(env, "TRANSLATIONS_KV") || c.env?.TRANSLATIONS_KV;
|
|
41
|
+
if (kv) setKVBinding(kv);
|
|
42
|
+
} catch {}
|
|
43
|
+
const url = new URL(c.req.url);
|
|
44
|
+
const pathname = url.pathname;
|
|
45
|
+
if (c.req.method !== "GET" && c.req.method !== "HEAD") {
|
|
46
|
+
const paramLocale = c.req.param("locale");
|
|
47
|
+
const firstSegment = pathname.split("/").find(Boolean);
|
|
48
|
+
const activeLocale = (paramLocale && SUPPORTED_LOCALES.has(paramLocale) ? paramLocale : null) || (firstSegment && SUPPORTED_LOCALES.has(firstSegment) ? firstSegment : null) || DEFAULT_LOCALE;
|
|
49
|
+
currentLocale.set(activeLocale);
|
|
50
|
+
return next();
|
|
51
|
+
}
|
|
52
|
+
if (pathname.startsWith("/api") || pathname.startsWith("/_") || pathname.includes(".")) return next();
|
|
53
|
+
const firstSegment = pathname.split("/").find(Boolean);
|
|
54
|
+
const firstIsLocale = firstSegment !== void 0 && SUPPORTED_LOCALES.has(firstSegment);
|
|
55
|
+
if (prefixDefaultLocale) {
|
|
56
|
+
const paramLocale = c.req.param("locale");
|
|
57
|
+
const activeLocale = (paramLocale && SUPPORTED_LOCALES.has(paramLocale) ? paramLocale : null) || (firstIsLocale ? firstSegment : null) || DEFAULT_LOCALE;
|
|
58
|
+
currentLocale.set(activeLocale);
|
|
59
|
+
if (firstIsLocale) return next();
|
|
60
|
+
const locale = getPreferredLocale(c.req.header("accept-language") || null);
|
|
61
|
+
currentLocale.set(locale);
|
|
62
|
+
const targetPath = `/${locale}${pathname}${url.search}`;
|
|
63
|
+
return c.redirect(targetPath, 302);
|
|
64
|
+
} else {
|
|
65
|
+
if (firstIsLocale) {
|
|
66
|
+
if (firstSegment === DEFAULT_LOCALE) {
|
|
67
|
+
const rest = pathname.slice(DEFAULT_LOCALE.length + 1) || "/";
|
|
68
|
+
currentLocale.set(DEFAULT_LOCALE);
|
|
69
|
+
return c.redirect(`${rest}${url.search}`, 302);
|
|
70
|
+
}
|
|
71
|
+
currentLocale.set(firstSegment);
|
|
72
|
+
return next();
|
|
73
|
+
}
|
|
74
|
+
currentLocale.set(DEFAULT_LOCALE);
|
|
75
|
+
return next();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
//#endregion
|
|
80
|
+
export { i18n };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { i as KVNamespaceBinding, n as CustomTranslations, o as NestedTranslationKeys, s as TranslationKey, t as ComponentsJSON } from "./types-BKUPwH-e.mjs";
|
|
2
|
+
import { a as getFormatterInstance, c as initializeI18n, d as useI18n, f as useI18nAsync, i as currentLocale, o as getI18nInstance, r as clearCache, s as getPluralCategory, u as useFormat } from "./runtime-Bc8I8sA4.mjs";
|
|
3
|
+
import { i as i18n, n as RimelightI18nPlugins, t as RimelightI18nOptions } from "./plugin-CFEm0e4U.mjs";
|
|
4
|
+
import { TranslationLoader } from "@nanostores/i18n";
|
|
4
5
|
//#region src/index.d.ts
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
} | undefined | null, componentName: string): ComponentMessages;
|
|
14
|
-
declare function useI18n<Body extends Translations>(astro: {
|
|
15
|
-
currentLocale?: string | undefined;
|
|
16
|
-
params?: Record<string, any>;
|
|
17
|
-
} | undefined | null, componentName: string, baseTranslations: Body): ComponentMessages & Body;
|
|
18
|
-
declare function useI18nAsync(componentName: string): Promise<Record<string, string>>;
|
|
19
|
-
declare function useI18nAsync<Body extends Translations>(componentName: string, baseTranslations: Body): Promise<Record<string, string> & Body>;
|
|
20
|
-
declare function useI18nAsync(astro: {
|
|
21
|
-
currentLocale?: string | undefined;
|
|
22
|
-
params?: Record<string, any>;
|
|
23
|
-
} | undefined | null, componentName: string): Promise<Record<string, string>>;
|
|
24
|
-
declare function useI18nAsync<Body extends Translations>(astro: {
|
|
25
|
-
currentLocale?: string | undefined;
|
|
26
|
-
params?: Record<string, any>;
|
|
27
|
-
} | undefined | null, componentName: string, baseTranslations: Body): Promise<Record<string, string> & Body>;
|
|
6
|
+
declare let locales: string[];
|
|
7
|
+
declare let defaultLocale: string;
|
|
8
|
+
declare let prefixDefaultLocale: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Translates a single string key using dot-notation (e.g. `t("page_home.section_hero_title")`).
|
|
11
|
+
* When augmented via CustomTranslations, keys are strictly type-checked and autocompleted.
|
|
12
|
+
*/
|
|
13
|
+
declare function t<K extends string = TranslationKey>(key: K | TranslationKey, params?: Record<string, any>): string;
|
|
28
14
|
declare function getLocale(): string;
|
|
15
|
+
/**
|
|
16
|
+
* Returns a standard BCP 47 / RFC 5646 language tag suitable for HTML `<html lang="...">`. Maps
|
|
17
|
+
* 2-letter codes like 'en' to 'en-US', 'pt' to 'pt-BR', 'es' to 'es-ES', etc.
|
|
18
|
+
*/
|
|
19
|
+
declare function getHtmlLang(): string;
|
|
20
|
+
/**
|
|
21
|
+
* Returns an OpenGraph-compliant locale tag (e.g. 'en_US', 'pt_BR', 'es_ES').
|
|
22
|
+
*/
|
|
23
|
+
declare function getOgLocale(): string;
|
|
29
24
|
declare function getRelativeLocaleUrl(path: string): string;
|
|
30
25
|
declare function getRelativeLocaleUrl(locale: string, path: string): string;
|
|
31
|
-
declare function
|
|
32
|
-
|
|
26
|
+
declare function getRelativeLocaleUrlList(path?: string): string[];
|
|
27
|
+
interface LanguageAlternate {
|
|
28
|
+
hreflang: string;
|
|
29
|
+
href: string;
|
|
30
|
+
}
|
|
33
31
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
32
|
+
* Generates an array of hreflang alternates for SEO tags (<link rel="alternate" hreflang="...">).
|
|
33
|
+
* Automatically includes all supported locales and 'x-default'.
|
|
36
34
|
*/
|
|
37
|
-
declare function
|
|
35
|
+
declare function getLanguageAlternates(pathname: string, siteUrl?: string): LanguageAlternate[];
|
|
36
|
+
declare function setKVBinding(kv: KVNamespaceBinding): void;
|
|
37
|
+
declare function createTranslationLoader(translations: Record<string, ComponentsJSON>): TranslationLoader;
|
|
38
38
|
//#endregion
|
|
39
|
-
export {
|
|
39
|
+
export { type CustomTranslations, LanguageAlternate, type NestedTranslationKeys, type RimelightI18nOptions, type RimelightI18nPlugins, type TranslationKey, clearCache, createTranslationLoader, currentLocale, defaultLocale, getFormatterInstance, getHtmlLang, getI18nInstance, getLanguageAlternates, getLocale, getOgLocale, getPluralCategory, getRelativeLocaleUrl, getRelativeLocaleUrlList, i18n, i18n as rimelightI18n, initializeI18n, locales, prefixDefaultLocale, setKVBinding, t, useFormat, useI18n, useI18nAsync };
|
package/dist/index.mjs
CHANGED
|
@@ -1,63 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
if (typeof arg1 === "string") return useI18n$1(arg1, arg2);
|
|
6
|
-
else {
|
|
7
|
-
const locale = arg1?.currentLocale || arg1?.params?.locale;
|
|
8
|
-
if (locale) currentLocale.set(locale);
|
|
9
|
-
return useI18n$1(arg2, arg3);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
function useI18nAsync(arg1, arg2, arg3) {
|
|
13
|
-
if (typeof arg1 === "string") return useI18nAsync$1(arg1, arg2);
|
|
14
|
-
else {
|
|
15
|
-
const locale = arg1?.currentLocale || arg1?.params?.locale;
|
|
16
|
-
if (locale) currentLocale.set(locale);
|
|
17
|
-
return useI18nAsync$1(arg2, arg3);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
function getLocale() {
|
|
21
|
-
return currentLocale.get() || "en";
|
|
22
|
-
}
|
|
23
|
-
function getRelativeLocaleUrl(arg1, arg2) {
|
|
24
|
-
if (arg2 !== void 0) return getRelativeLocaleUrl$1(arg1, arg2);
|
|
25
|
-
else {
|
|
26
|
-
const locale = getLocale();
|
|
27
|
-
return getRelativeLocaleUrl$1(locale, arg1);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
let kvBinding = null;
|
|
31
|
-
function setKVBinding(kv) {
|
|
32
|
-
kvBinding = kv;
|
|
33
|
-
}
|
|
34
|
-
function createTranslationLoader(translations) {
|
|
35
|
-
return async (locale, components) => {
|
|
36
|
-
const localeTranslations = translations[locale] || {};
|
|
37
|
-
if (!components || !Array.isArray(components) || components.length === 0) return localeTranslations;
|
|
38
|
-
const activeKv = kvBinding;
|
|
39
|
-
if (activeKv) try {
|
|
40
|
-
const results = await Promise.all(components.map(async (name) => {
|
|
41
|
-
const key = `locale:${locale}:${name}`;
|
|
42
|
-
const data = await activeKv.get(key, "json");
|
|
43
|
-
if (data == null) return { [name]: localeTranslations[name] ?? {} };
|
|
44
|
-
return { [name]: data };
|
|
45
|
-
}));
|
|
46
|
-
return Object.assign({}, ...results);
|
|
47
|
-
} catch {}
|
|
48
|
-
const results = await Promise.all(components.map(async (name) => {
|
|
49
|
-
const data = localeTranslations[name];
|
|
50
|
-
return { [name]: data ?? {} };
|
|
51
|
-
}));
|
|
52
|
-
return Object.assign({}, ...results);
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Returns the plural category (e.g. 'one', 'few', 'many', 'other') for a given number and locale
|
|
57
|
-
* using the native browser Intl.PluralRules API.
|
|
58
|
-
*/
|
|
59
|
-
function getPluralCategory(count, locale = "en") {
|
|
60
|
-
return new Intl.PluralRules(locale).select(count);
|
|
61
|
-
}
|
|
62
|
-
//#endregion
|
|
63
|
-
export { clearCache, createTranslationLoader, currentLocale, getFormatterInstance, getI18nInstance, getLocale, getPluralCategory, getRelativeLocaleUrl, setKVBinding, t, useFormat, useI18n, useI18nAsync };
|
|
1
|
+
import { a as getPluralCategory, c as useFormat, i as getI18nInstance, l as useI18n, n as currentLocale, o as initializeI18n, r as getFormatterInstance, t as clearCache, u as useI18nAsync } from "./runtime-Civir_tv.mjs";
|
|
2
|
+
import { t as i18n } from "./plugin-nQl4lZKv.mjs";
|
|
3
|
+
import { a as getLocale, c as getRelativeLocaleUrlList, d as setKVBinding, f as t, i as getLanguageAlternates, l as locales, n as defaultLocale, o as getOgLocale, r as getHtmlLang, s as getRelativeLocaleUrl, t as createTranslationLoader, u as prefixDefaultLocale } from "./src-D09inETW.mjs";
|
|
4
|
+
export { clearCache, createTranslationLoader, currentLocale, defaultLocale, getFormatterInstance, getHtmlLang, getI18nInstance, getLanguageAlternates, getLocale, getOgLocale, getPluralCategory, getRelativeLocaleUrl, getRelativeLocaleUrlList, i18n, i18n as rimelightI18n, initializeI18n, locales, prefixDefaultLocale, setKVBinding, t, useFormat, useI18n, useI18nAsync };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
//#region src/plugin.d.ts
|
|
2
|
+
interface RimelightI18nOptions {
|
|
3
|
+
/**
|
|
4
|
+
* List of supported locale codes (e.g. `["en", "pt"]`).
|
|
5
|
+
*/
|
|
6
|
+
locales?: string[];
|
|
7
|
+
/**
|
|
8
|
+
* The default locale code (e.g. `"en"`).
|
|
9
|
+
*/
|
|
10
|
+
defaultLocale?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Whether to prefix the default locale in URLs.
|
|
13
|
+
*
|
|
14
|
+
* - `true` (default): all locales are prefixed — `/en/about`, `/pt/about`
|
|
15
|
+
* - `false`: the default locale has no prefix — `/about`, `/pt/about`
|
|
16
|
+
*/
|
|
17
|
+
prefixDefaultLocale?: boolean;
|
|
18
|
+
validateExtraction?: boolean;
|
|
19
|
+
translations?: Record<string, Record<string, any>>;
|
|
20
|
+
kvBinding?: string;
|
|
21
|
+
translationLoader?: string;
|
|
22
|
+
}
|
|
23
|
+
interface RimelightI18nVitePlugin {
|
|
24
|
+
name: string;
|
|
25
|
+
enforce?: "pre" | "post";
|
|
26
|
+
resolveId?: (id: string) => string | null | undefined;
|
|
27
|
+
load?: (id: string) => string | null | undefined;
|
|
28
|
+
config?: (config: any) => any;
|
|
29
|
+
closeBundle?: () => Promise<void> | void;
|
|
30
|
+
[key: string]: any;
|
|
31
|
+
}
|
|
32
|
+
type RimelightI18nPlugins = RimelightI18nVitePlugin[];
|
|
33
|
+
/**
|
|
34
|
+
* Pure Vite plugin for Rimelight i18n. Provides virtual modules `@rimelight/i18n:runtime` and
|
|
35
|
+
* `virtual:rimelight-i18n-config`.
|
|
36
|
+
*/
|
|
37
|
+
declare function i18n(options?: RimelightI18nOptions): RimelightI18nPlugins;
|
|
38
|
+
//#endregion
|
|
39
|
+
export { i18n as i, RimelightI18nPlugins as n, RimelightI18nVitePlugin as r, RimelightI18nOptions as t };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
//#region src/plugin.ts
|
|
4
|
+
/**
|
|
5
|
+
* Pure Vite plugin for Rimelight i18n. Provides virtual modules `@rimelight/i18n:runtime` and
|
|
6
|
+
* `virtual:rimelight-i18n-config`.
|
|
7
|
+
*/
|
|
8
|
+
function i18n(options) {
|
|
9
|
+
const translations = options?.translations;
|
|
10
|
+
const kvBinding = options?.kvBinding ?? "TRANSLATIONS_KV";
|
|
11
|
+
const locales = options?.locales ?? ["en"];
|
|
12
|
+
const defaultLocale = options?.defaultLocale ?? "en";
|
|
13
|
+
const prefixDefaultLocale = options?.prefixDefaultLocale ?? true;
|
|
14
|
+
const normalisedTranslations = {};
|
|
15
|
+
if (translations) for (const [locale, val] of Object.entries(translations)) normalisedTranslations[locale] = val;
|
|
16
|
+
return [{
|
|
17
|
+
name: "vite-plugin-rimelight-i18n-config",
|
|
18
|
+
resolveId(id) {
|
|
19
|
+
if (id === "virtual:rimelight-i18n-config") return "\0" + id;
|
|
20
|
+
return null;
|
|
21
|
+
},
|
|
22
|
+
load(id) {
|
|
23
|
+
if (id === "\0virtual:rimelight-i18n-config") return `
|
|
24
|
+
export const locales = ${JSON.stringify(locales)};
|
|
25
|
+
export const defaultLocale = ${JSON.stringify(defaultLocale)};
|
|
26
|
+
export const prefixDefaultLocale = ${JSON.stringify(prefixDefaultLocale)};
|
|
27
|
+
export const translations = ${JSON.stringify(normalisedTranslations)};
|
|
28
|
+
`;
|
|
29
|
+
return null;
|
|
30
|
+
},
|
|
31
|
+
async closeBundle() {
|
|
32
|
+
if (!translations) return;
|
|
33
|
+
const seedData = { keys: [] };
|
|
34
|
+
for (const [locale, localeData] of Object.entries(translations)) for (const [component, componentData] of Object.entries(localeData)) seedData.keys.push({
|
|
35
|
+
key: `locale:${locale}:${component}`,
|
|
36
|
+
value: JSON.stringify(componentData)
|
|
37
|
+
});
|
|
38
|
+
const outDir = process.env.WRANGLER_OUT_DIR ?? "dist";
|
|
39
|
+
const seedFile = path.join(outDir, "_translations-seed.json");
|
|
40
|
+
try {
|
|
41
|
+
fs.mkdirSync(path.dirname(seedFile), { recursive: true });
|
|
42
|
+
fs.writeFileSync(seedFile, JSON.stringify(seedData, null, 2));
|
|
43
|
+
if (process.env.WRANGLER_SEED_KV === "true" || process.env.CI === "true") {
|
|
44
|
+
const { execSync } = await import("node:child_process");
|
|
45
|
+
execSync(`npx wrangler kv:bulk put --binding "${kvBinding}" "${seedFile}"`, {
|
|
46
|
+
stdio: "pipe",
|
|
47
|
+
timeout: 3e4
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
} catch {}
|
|
51
|
+
}
|
|
52
|
+
}];
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
export { i18n as t };
|
package/dist/plugin.mjs
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Components, I18n, TranslationLoader, Translations, formatter } from "@nanostores/i18n";
|
|
2
|
+
//#region src/runtime.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* A reactive store containing the current locale code. Set by middleware on each request, or
|
|
5
|
+
* manually via `currentLocale.set(locale)`.
|
|
6
|
+
*/
|
|
7
|
+
declare const currentLocale: import("nanostores").PreinitializedWritableAtom<string> & object;
|
|
8
|
+
type I18nInstance = I18n;
|
|
9
|
+
type FormatterInstance = ReturnType<typeof formatter>;
|
|
10
|
+
interface InitializeI18nOptions {
|
|
11
|
+
/**
|
|
12
|
+
* The default locale code (e.g. 'en').
|
|
13
|
+
*/
|
|
14
|
+
defaultLocale: string;
|
|
15
|
+
/**
|
|
16
|
+
* Pre-loaded translations keyed by locale then component.
|
|
17
|
+
*/
|
|
18
|
+
translations: Record<string, Components>;
|
|
19
|
+
/**
|
|
20
|
+
* Optional dynamic loader called when a locale is not in cache.
|
|
21
|
+
*/
|
|
22
|
+
get?: TranslationLoader;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Initializes the i18n system. Must be called once before any other i18n functions are used (the
|
|
26
|
+
* integration virtual module does this automatically).
|
|
27
|
+
*/
|
|
28
|
+
declare function initializeI18n(options: InitializeI18nOptions): void;
|
|
29
|
+
/**
|
|
30
|
+
* Returns the underlying nanostores/i18n instance. Throws if not initialized.
|
|
31
|
+
*/
|
|
32
|
+
declare function getI18nInstance(): I18nInstance;
|
|
33
|
+
/**
|
|
34
|
+
* Returns the formatter instance. Throws if not initialized.
|
|
35
|
+
*/
|
|
36
|
+
declare function getFormatterInstance(): FormatterInstance;
|
|
37
|
+
/**
|
|
38
|
+
* Returns a Formatter object for the current locale with methods for formatting numbers, dates, and
|
|
39
|
+
* relative times using the native Intl API.
|
|
40
|
+
*/
|
|
41
|
+
declare function useFormat(): ReturnType<FormatterInstance["get"]>;
|
|
42
|
+
/**
|
|
43
|
+
* Returns the translated strings for a component in the current locale. Falls back to defaultLocale
|
|
44
|
+
* loaded translations or `baseTranslations` if provided.
|
|
45
|
+
*/
|
|
46
|
+
interface ComponentMessages {
|
|
47
|
+
[key: string]: string;
|
|
48
|
+
}
|
|
49
|
+
declare function useI18n(componentName: string): ComponentMessages;
|
|
50
|
+
declare function useI18n<Body extends Translations>(componentName: string, baseTranslations: Body): ComponentMessages & Body;
|
|
51
|
+
/**
|
|
52
|
+
* Async version of useI18n that waits for translations to finish loading.
|
|
53
|
+
*/
|
|
54
|
+
declare function useI18nAsync(componentName: string): Promise<ComponentMessages>;
|
|
55
|
+
declare function useI18nAsync<Body extends Translations>(componentName: string, baseTranslations: Body): Promise<ComponentMessages & Body>;
|
|
56
|
+
/**
|
|
57
|
+
* Translates a single string key using dot-notation (e.g. `t("page_home.section_hero_title")`).
|
|
58
|
+
*/
|
|
59
|
+
declare function t(key: string, params?: Record<string, any>): string;
|
|
60
|
+
/**
|
|
61
|
+
* Returns the plural category (e.g. 'one', 'few', 'many', 'other') for a given number and locale
|
|
62
|
+
* using the native browser Intl.PluralRules API.
|
|
63
|
+
*/
|
|
64
|
+
declare function getPluralCategory(count: number, locale?: string): Intl.LDMLPluralRule;
|
|
65
|
+
/**
|
|
66
|
+
* Clears the translation cache.
|
|
67
|
+
*
|
|
68
|
+
* @param locale - If provided, clears only that locale's cache. Otherwise clears all.
|
|
69
|
+
*/
|
|
70
|
+
declare function clearCache(locale?: string): void;
|
|
71
|
+
//#endregion
|
|
72
|
+
export { getFormatterInstance as a, initializeI18n as c, useI18n as d, useI18nAsync as f, currentLocale as i, t as l, InitializeI18nOptions as n, getI18nInstance as o, clearCache as r, getPluralCategory as s, ComponentMessages as t, useFormat as u };
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { createI18n, formatter, translationsLoading } from "@nanostores/i18n";
|
|
2
|
+
import { atom } from "nanostores";
|
|
3
|
+
//#region src/runtime.ts
|
|
4
|
+
/**
|
|
5
|
+
* A reactive store containing the current locale code. Set by middleware on each request, or
|
|
6
|
+
* manually via `currentLocale.set(locale)`.
|
|
7
|
+
*/
|
|
8
|
+
const currentLocale = atom("");
|
|
9
|
+
let i18nInstance;
|
|
10
|
+
let formatterInstance;
|
|
11
|
+
let baseLocaleDefault = "en";
|
|
12
|
+
let rawTranslationsDict = {};
|
|
13
|
+
function throwNotInitialized() {
|
|
14
|
+
throw new Error("i18n not initialized. Call initializeI18n first.");
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Wraps a TranslationLoader so that the returned object is guaranteed to contain a key for every
|
|
18
|
+
* requested component.
|
|
19
|
+
*
|
|
20
|
+
* The nanostores i18n library uses the keys of the object returned from `get` to clear its internal
|
|
21
|
+
* "requested" set. If a requested component is missing from the result (e.g. because the backend
|
|
22
|
+
* has no translations for it in the given locale yet), the internal loading atom is never set back
|
|
23
|
+
* to `false`, which causes useI18nAsync / translationsLoading to hang forever.
|
|
24
|
+
*
|
|
25
|
+
* This wrapper normalises the loader output so missing components are filled in with empty
|
|
26
|
+
* translation objects, falling back to the base translations defined at the call site.
|
|
27
|
+
*/
|
|
28
|
+
function wrapLoader(loader) {
|
|
29
|
+
return async (code, components) => {
|
|
30
|
+
const raw = await loader(code, components);
|
|
31
|
+
const normalised = Array.isArray(raw) ? Object.assign({}, ...raw) : { ...raw };
|
|
32
|
+
for (const component of components) if (!(component in normalised)) normalised[component] = {};
|
|
33
|
+
return normalised;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Initializes the i18n system. Must be called once before any other i18n functions are used (the
|
|
38
|
+
* integration virtual module does this automatically).
|
|
39
|
+
*/
|
|
40
|
+
function initializeI18n(options) {
|
|
41
|
+
const { defaultLocale, translations, get } = options;
|
|
42
|
+
baseLocaleDefault = defaultLocale;
|
|
43
|
+
rawTranslationsDict = translations || {};
|
|
44
|
+
if (!i18nInstance) {
|
|
45
|
+
currentLocale.set(defaultLocale);
|
|
46
|
+
const formattedCache = {};
|
|
47
|
+
for (const [locale, components] of Object.entries(translations)) {
|
|
48
|
+
formattedCache[locale] = {};
|
|
49
|
+
for (const [compName, compBody] of Object.entries(components)) {
|
|
50
|
+
let bodyObj = {};
|
|
51
|
+
if (isStringRecord(compBody)) bodyObj = compBody;
|
|
52
|
+
else if (isStoreWithGet(compBody)) {
|
|
53
|
+
const res = compBody.get();
|
|
54
|
+
if (isStringRecord(res)) bodyObj = res;
|
|
55
|
+
}
|
|
56
|
+
formattedCache[locale][compName] = atom(bodyObj);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
i18nInstance = createI18n(currentLocale, {
|
|
60
|
+
baseLocale: defaultLocale,
|
|
61
|
+
get: wrapLoader(get ?? (async () => ({}))),
|
|
62
|
+
cache: formattedCache,
|
|
63
|
+
isSSR: true
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
formatterInstance = formatter(currentLocale);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Returns the underlying nanostores/i18n instance. Throws if not initialized.
|
|
70
|
+
*/
|
|
71
|
+
function getI18nInstance() {
|
|
72
|
+
if (!i18nInstance) throwNotInitialized();
|
|
73
|
+
return i18nInstance;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Returns the formatter instance. Throws if not initialized.
|
|
77
|
+
*/
|
|
78
|
+
function getFormatterInstance() {
|
|
79
|
+
if (!formatterInstance) throwNotInitialized();
|
|
80
|
+
return formatterInstance;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Returns a Formatter object for the current locale with methods for formatting numbers, dates, and
|
|
84
|
+
* relative times using the native Intl API.
|
|
85
|
+
*/
|
|
86
|
+
function useFormat() {
|
|
87
|
+
return getFormatterInstance().get();
|
|
88
|
+
}
|
|
89
|
+
function isStoreWithGet(obj) {
|
|
90
|
+
if (typeof obj !== "object" || obj === null) return false;
|
|
91
|
+
return typeof Reflect.get(obj, "get") === "function";
|
|
92
|
+
}
|
|
93
|
+
function isStringRecord(obj) {
|
|
94
|
+
return typeof obj === "object" && obj !== null;
|
|
95
|
+
}
|
|
96
|
+
function getCachedComponent(locale, componentName) {
|
|
97
|
+
const normalizedLocale = locale.toLowerCase();
|
|
98
|
+
const candidates = [
|
|
99
|
+
locale,
|
|
100
|
+
normalizedLocale,
|
|
101
|
+
normalizedLocale.split("-")[0] ?? normalizedLocale,
|
|
102
|
+
baseLocaleDefault
|
|
103
|
+
];
|
|
104
|
+
for (const code of candidates) {
|
|
105
|
+
const rawComp = rawTranslationsDict[code]?.[componentName];
|
|
106
|
+
if (isStoreWithGet(rawComp)) {
|
|
107
|
+
const res = rawComp.get();
|
|
108
|
+
if (isStringRecord(res)) return res;
|
|
109
|
+
} else if (isStringRecord(rawComp)) return rawComp;
|
|
110
|
+
}
|
|
111
|
+
if (!i18nInstance) return void 0;
|
|
112
|
+
for (const code of candidates) {
|
|
113
|
+
const rawComp = i18nInstance.cache[code]?.[componentName];
|
|
114
|
+
if (isStoreWithGet(rawComp)) {
|
|
115
|
+
const res = rawComp.get();
|
|
116
|
+
if (isStringRecord(res)) return res;
|
|
117
|
+
} else if (isStringRecord(rawComp)) return rawComp;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function useI18n(componentName, baseTranslations) {
|
|
121
|
+
const i18n = getI18nInstance();
|
|
122
|
+
const activeLocale = currentLocale.get() || baseLocaleDefault;
|
|
123
|
+
const fallbackDict = isStringRecord(baseTranslations) ? baseTranslations : {};
|
|
124
|
+
const baseDict = getCachedComponent(activeLocale, componentName) ?? getCachedComponent(baseLocaleDefault, componentName) ?? fallbackDict;
|
|
125
|
+
const targetObj = i18n(componentName, baseDict).get();
|
|
126
|
+
return new Proxy(targetObj, { get(target, prop) {
|
|
127
|
+
if (typeof prop === "symbol" || prop in Object.prototype) {
|
|
128
|
+
const val = Reflect.get(target, prop);
|
|
129
|
+
return typeof val === "string" ? val : "";
|
|
130
|
+
}
|
|
131
|
+
return target[prop] ?? baseDict[prop] ?? prop;
|
|
132
|
+
} });
|
|
133
|
+
}
|
|
134
|
+
async function useI18nAsync(componentName, baseTranslations) {
|
|
135
|
+
const i18n = getI18nInstance();
|
|
136
|
+
const cachedActive = getCachedComponent(currentLocale.get() || baseLocaleDefault, componentName);
|
|
137
|
+
const cachedDefault = getCachedComponent(baseLocaleDefault, componentName);
|
|
138
|
+
let baseDict = cachedActive ?? cachedDefault ?? {};
|
|
139
|
+
if (!cachedActive && !cachedDefault && isStringRecord(baseTranslations)) baseDict = baseTranslations;
|
|
140
|
+
const store = i18n(componentName, baseDict);
|
|
141
|
+
const unsubscribe = store.listen(() => {});
|
|
142
|
+
await translationsLoading(i18n);
|
|
143
|
+
unsubscribe();
|
|
144
|
+
return store.get();
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Translates a single string key using dot-notation (e.g. `t("page_home.section_hero_title")`).
|
|
148
|
+
*/
|
|
149
|
+
function t(key, params) {
|
|
150
|
+
const dotIndex = key.indexOf(".");
|
|
151
|
+
if (dotIndex === -1) return key;
|
|
152
|
+
const componentName = key.slice(0, dotIndex);
|
|
153
|
+
const keyName = key.slice(dotIndex + 1);
|
|
154
|
+
const activeLocale = currentLocale.get() || baseLocaleDefault;
|
|
155
|
+
let value = (getCachedComponent(activeLocale, componentName) ?? getCachedComponent(baseLocaleDefault, componentName) ?? {})[keyName] ?? key;
|
|
156
|
+
if (params && typeof params.count === "number" && typeof value === "object" && value !== null) {
|
|
157
|
+
const pluralCategory = getPluralCategory(params.count, activeLocale);
|
|
158
|
+
value = value[pluralCategory] ?? value.other ?? key;
|
|
159
|
+
}
|
|
160
|
+
if (typeof value === "function") return value(params);
|
|
161
|
+
if (params && typeof value === "string") return value.replace(/\{(\w+)\}/g, (_, k) => String(params[k] ?? `{${k}}`));
|
|
162
|
+
return typeof value === "string" ? value : String(value);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Returns the plural category (e.g. 'one', 'few', 'many', 'other') for a given number and locale
|
|
166
|
+
* using the native browser Intl.PluralRules API.
|
|
167
|
+
*/
|
|
168
|
+
function getPluralCategory(count, locale = "en") {
|
|
169
|
+
return new Intl.PluralRules(locale).select(count);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Clears the translation cache.
|
|
173
|
+
*
|
|
174
|
+
* @param locale - If provided, clears only that locale's cache. Otherwise clears all.
|
|
175
|
+
*/
|
|
176
|
+
function clearCache(locale) {
|
|
177
|
+
if (!i18nInstance) throwNotInitialized();
|
|
178
|
+
const cache = i18nInstance.cache;
|
|
179
|
+
if (locale) cache[locale] = {};
|
|
180
|
+
else for (const key in cache) cache[key] = {};
|
|
181
|
+
}
|
|
182
|
+
//#endregion
|
|
183
|
+
export { getPluralCategory as a, useFormat as c, getI18nInstance as i, useI18n as l, currentLocale as n, initializeI18n as o, getFormatterInstance as r, t as s, clearCache as t, useI18nAsync as u };
|
package/dist/runtime.d.mts
CHANGED
|
@@ -1,71 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A reactive store containing the current locale code. Set by middleware on each request, or
|
|
5
|
-
* manually via `currentLocale.set(locale)`.
|
|
6
|
-
*/
|
|
7
|
-
declare const currentLocale: import("nanostores").PreinitializedWritableAtom<string> & object;
|
|
8
|
-
type I18nInstance = I18n;
|
|
9
|
-
type FormatterInstance = ReturnType<typeof formatter>;
|
|
10
|
-
interface InitializeI18nOptions {
|
|
11
|
-
/**
|
|
12
|
-
* The default locale code (e.g. 'en').
|
|
13
|
-
*/
|
|
14
|
-
defaultLocale: string;
|
|
15
|
-
/**
|
|
16
|
-
* Pre-loaded translations keyed by locale then component.
|
|
17
|
-
*/
|
|
18
|
-
translations: Record<string, Components>;
|
|
19
|
-
/**
|
|
20
|
-
* Optional dynamic loader called when a locale is not in cache.
|
|
21
|
-
*/
|
|
22
|
-
get?: TranslationLoader;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Initializes the i18n system. Must be called once before any other i18n functions are used (the
|
|
26
|
-
* integration virtual module does this automatically).
|
|
27
|
-
*/
|
|
28
|
-
declare function initializeI18n(options: InitializeI18nOptions): void;
|
|
29
|
-
/**
|
|
30
|
-
* Returns the underlying nanostores/i18n instance. Throws if not initialized.
|
|
31
|
-
*/
|
|
32
|
-
declare function getI18nInstance(): I18nInstance;
|
|
33
|
-
/**
|
|
34
|
-
* Returns the formatter instance. Throws if not initialized.
|
|
35
|
-
*/
|
|
36
|
-
declare function getFormatterInstance(): FormatterInstance;
|
|
37
|
-
/**
|
|
38
|
-
* Returns a Formatter object for the current locale with methods for formatting numbers, dates, and
|
|
39
|
-
* relative times using the native Intl API.
|
|
40
|
-
*/
|
|
41
|
-
declare function useFormat(): ReturnType<FormatterInstance["get"]>;
|
|
42
|
-
/**
|
|
43
|
-
* Returns the translated strings for a component in the current locale. Falls back to defaultLocale
|
|
44
|
-
* loaded translations or `baseTranslations` if provided.
|
|
45
|
-
*/
|
|
46
|
-
interface ComponentMessages {
|
|
47
|
-
[key: string]: string;
|
|
48
|
-
}
|
|
49
|
-
declare function useI18n(componentName: string): ComponentMessages;
|
|
50
|
-
declare function useI18n<Body extends Translations>(componentName: string, baseTranslations: Body): ComponentMessages & Body;
|
|
51
|
-
/**
|
|
52
|
-
* Async version of useI18n that waits for translations to finish loading.
|
|
53
|
-
*/
|
|
54
|
-
declare function useI18nAsync(componentName: string): Promise<ComponentMessages>;
|
|
55
|
-
declare function useI18nAsync<Body extends Translations>(componentName: string, baseTranslations: Body): Promise<ComponentMessages & Body>;
|
|
56
|
-
/**
|
|
57
|
-
* Translates a single string key using dot-notation (e.g. `t("page_home.section_hero_title")`).
|
|
58
|
-
*/
|
|
59
|
-
declare function t(key: string, params?: Record<string, any>): string;
|
|
60
|
-
declare function t(astro: {
|
|
61
|
-
currentLocale?: string | undefined;
|
|
62
|
-
params?: Record<string, any>;
|
|
63
|
-
} | undefined | null, key: string, params?: Record<string, any>): string;
|
|
64
|
-
/**
|
|
65
|
-
* Clears the translation cache.
|
|
66
|
-
*
|
|
67
|
-
* @param locale - If provided, clears only that locale's cache. Otherwise clears all.
|
|
68
|
-
*/
|
|
69
|
-
declare function clearCache(locale?: string): void;
|
|
70
|
-
//#endregion
|
|
71
|
-
export { ComponentMessages, InitializeI18nOptions, clearCache, currentLocale, getFormatterInstance, getI18nInstance, initializeI18n, t, useFormat, useI18n, useI18nAsync };
|
|
1
|
+
import { a as getFormatterInstance, c as initializeI18n, d as useI18n, f as useI18nAsync, i as currentLocale, l as t, n as InitializeI18nOptions, o as getI18nInstance, r as clearCache, s as getPluralCategory, t as ComponentMessages, u as useFormat } from "./runtime-Bc8I8sA4.mjs";
|
|
2
|
+
export { ComponentMessages, InitializeI18nOptions, clearCache, currentLocale, getFormatterInstance, getI18nInstance, getPluralCategory, initializeI18n, t, useFormat, useI18n, useI18nAsync };
|
package/dist/runtime.mjs
CHANGED
|
@@ -1,179 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
//#region src/runtime.ts
|
|
4
|
-
/**
|
|
5
|
-
* A reactive store containing the current locale code. Set by middleware on each request, or
|
|
6
|
-
* manually via `currentLocale.set(locale)`.
|
|
7
|
-
*/
|
|
8
|
-
const currentLocale = atom("");
|
|
9
|
-
let i18nInstance;
|
|
10
|
-
let formatterInstance;
|
|
11
|
-
let baseLocaleDefault = "en";
|
|
12
|
-
let rawTranslationsDict = {};
|
|
13
|
-
function throwNotInitialized() {
|
|
14
|
-
throw new Error("i18n not initialized. Call initializeI18n first.");
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Wraps a TranslationLoader so that the returned object is guaranteed to contain a key for every
|
|
18
|
-
* requested component.
|
|
19
|
-
*
|
|
20
|
-
* The nanostores i18n library uses the keys of the object returned from `get` to clear its internal
|
|
21
|
-
* "requested" set. If a requested component is missing from the result (e.g. because the backend
|
|
22
|
-
* has no translations for it in the given locale yet), the internal loading atom is never set back
|
|
23
|
-
* to `false`, which causes useI18nAsync / translationsLoading to hang forever.
|
|
24
|
-
*
|
|
25
|
-
* This wrapper normalises the loader output so missing components are filled in with empty
|
|
26
|
-
* translation objects, falling back to the base translations defined at the call site.
|
|
27
|
-
*/
|
|
28
|
-
function wrapLoader(loader) {
|
|
29
|
-
return async (code, components) => {
|
|
30
|
-
const raw = await loader(code, components);
|
|
31
|
-
const normalised = Array.isArray(raw) ? Object.assign({}, ...raw) : { ...raw };
|
|
32
|
-
for (const component of components) if (!(component in normalised)) normalised[component] = {};
|
|
33
|
-
return normalised;
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Initializes the i18n system. Must be called once before any other i18n functions are used (the
|
|
38
|
-
* integration virtual module does this automatically).
|
|
39
|
-
*/
|
|
40
|
-
function initializeI18n(options) {
|
|
41
|
-
const { defaultLocale, translations, get } = options;
|
|
42
|
-
baseLocaleDefault = defaultLocale;
|
|
43
|
-
rawTranslationsDict = translations || {};
|
|
44
|
-
if (!i18nInstance) {
|
|
45
|
-
currentLocale.set(defaultLocale);
|
|
46
|
-
const formattedCache = {};
|
|
47
|
-
for (const [locale, components] of Object.entries(translations)) {
|
|
48
|
-
formattedCache[locale] = {};
|
|
49
|
-
for (const [compName, compBody] of Object.entries(components)) {
|
|
50
|
-
let bodyObj = {};
|
|
51
|
-
if (isStringRecord(compBody)) bodyObj = compBody;
|
|
52
|
-
else if (isStoreWithGet(compBody)) {
|
|
53
|
-
const res = compBody.get();
|
|
54
|
-
if (isStringRecord(res)) bodyObj = res;
|
|
55
|
-
}
|
|
56
|
-
formattedCache[locale][compName] = atom(bodyObj);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
i18nInstance = createI18n(currentLocale, {
|
|
60
|
-
baseLocale: defaultLocale,
|
|
61
|
-
get: wrapLoader(get ?? (async () => ({}))),
|
|
62
|
-
cache: formattedCache,
|
|
63
|
-
isSSR: true
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
formatterInstance = formatter(currentLocale);
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Returns the underlying nanostores/i18n instance. Throws if not initialized.
|
|
70
|
-
*/
|
|
71
|
-
function getI18nInstance() {
|
|
72
|
-
if (!i18nInstance) throwNotInitialized();
|
|
73
|
-
return i18nInstance;
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Returns the formatter instance. Throws if not initialized.
|
|
77
|
-
*/
|
|
78
|
-
function getFormatterInstance() {
|
|
79
|
-
if (!formatterInstance) throwNotInitialized();
|
|
80
|
-
return formatterInstance;
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Returns a Formatter object for the current locale with methods for formatting numbers, dates, and
|
|
84
|
-
* relative times using the native Intl API.
|
|
85
|
-
*/
|
|
86
|
-
function useFormat() {
|
|
87
|
-
return getFormatterInstance().get();
|
|
88
|
-
}
|
|
89
|
-
function isStoreWithGet(obj) {
|
|
90
|
-
if (typeof obj !== "object" || obj === null) return false;
|
|
91
|
-
return typeof Reflect.get(obj, "get") === "function";
|
|
92
|
-
}
|
|
93
|
-
function isStringRecord(obj) {
|
|
94
|
-
return typeof obj === "object" && obj !== null;
|
|
95
|
-
}
|
|
96
|
-
function getCachedComponent(locale, componentName) {
|
|
97
|
-
const normalizedLocale = locale.toLowerCase();
|
|
98
|
-
const candidates = [
|
|
99
|
-
locale,
|
|
100
|
-
normalizedLocale,
|
|
101
|
-
normalizedLocale.split("-")[0] ?? normalizedLocale,
|
|
102
|
-
baseLocaleDefault
|
|
103
|
-
];
|
|
104
|
-
for (const code of candidates) {
|
|
105
|
-
const rawComp = rawTranslationsDict[code]?.[componentName];
|
|
106
|
-
if (isStoreWithGet(rawComp)) {
|
|
107
|
-
const res = rawComp.get();
|
|
108
|
-
if (isStringRecord(res)) return res;
|
|
109
|
-
} else if (isStringRecord(rawComp)) return rawComp;
|
|
110
|
-
}
|
|
111
|
-
if (!i18nInstance) return void 0;
|
|
112
|
-
for (const code of candidates) {
|
|
113
|
-
const rawComp = i18nInstance.cache[code]?.[componentName];
|
|
114
|
-
if (isStoreWithGet(rawComp)) {
|
|
115
|
-
const res = rawComp.get();
|
|
116
|
-
if (isStringRecord(res)) return res;
|
|
117
|
-
} else if (isStringRecord(rawComp)) return rawComp;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
function useI18n(componentName, baseTranslations) {
|
|
121
|
-
const i18n = getI18nInstance();
|
|
122
|
-
const activeLocale = currentLocale.get() || baseLocaleDefault;
|
|
123
|
-
const fallbackDict = isStringRecord(baseTranslations) ? baseTranslations : {};
|
|
124
|
-
const baseDict = getCachedComponent(activeLocale, componentName) ?? getCachedComponent(baseLocaleDefault, componentName) ?? fallbackDict;
|
|
125
|
-
const targetObj = i18n(componentName, baseDict).get();
|
|
126
|
-
return new Proxy(targetObj, { get(target, prop) {
|
|
127
|
-
if (typeof prop === "symbol" || prop in Object.prototype) {
|
|
128
|
-
const val = Reflect.get(target, prop);
|
|
129
|
-
return typeof val === "string" ? val : "";
|
|
130
|
-
}
|
|
131
|
-
return target[prop] ?? baseDict[prop] ?? prop;
|
|
132
|
-
} });
|
|
133
|
-
}
|
|
134
|
-
async function useI18nAsync(componentName, baseTranslations) {
|
|
135
|
-
const i18n = getI18nInstance();
|
|
136
|
-
const cachedActive = getCachedComponent(currentLocale.get() || baseLocaleDefault, componentName);
|
|
137
|
-
const cachedDefault = getCachedComponent(baseLocaleDefault, componentName);
|
|
138
|
-
let baseDict = cachedActive ?? cachedDefault ?? {};
|
|
139
|
-
if (!cachedActive && !cachedDefault && isStringRecord(baseTranslations)) baseDict = baseTranslations;
|
|
140
|
-
const store = i18n(componentName, baseDict);
|
|
141
|
-
const unsubscribe = store.listen(() => {});
|
|
142
|
-
await translationsLoading(i18n);
|
|
143
|
-
unsubscribe();
|
|
144
|
-
return store.get();
|
|
145
|
-
}
|
|
146
|
-
function t(arg1, arg2, arg3) {
|
|
147
|
-
let key;
|
|
148
|
-
let params;
|
|
149
|
-
if (typeof arg1 === "string") {
|
|
150
|
-
key = arg1;
|
|
151
|
-
params = arg2;
|
|
152
|
-
} else {
|
|
153
|
-
const locale = arg1?.currentLocale || arg1?.params?.locale;
|
|
154
|
-
if (locale) currentLocale.set(locale);
|
|
155
|
-
key = arg2;
|
|
156
|
-
params = arg3;
|
|
157
|
-
}
|
|
158
|
-
const dotIndex = key.indexOf(".");
|
|
159
|
-
if (dotIndex === -1) return key;
|
|
160
|
-
const componentName = key.slice(0, dotIndex);
|
|
161
|
-
const keyName = key.slice(dotIndex + 1);
|
|
162
|
-
const value = (getCachedComponent(currentLocale.get() || baseLocaleDefault, componentName) ?? getCachedComponent(baseLocaleDefault, componentName) ?? {})[keyName] ?? key;
|
|
163
|
-
if (typeof value === "function") return value(params);
|
|
164
|
-
if (params && typeof value === "string") return value.replace(/\{(\w+)\}/g, (_, k) => String(params[k] ?? `{${k}}`));
|
|
165
|
-
return value;
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Clears the translation cache.
|
|
169
|
-
*
|
|
170
|
-
* @param locale - If provided, clears only that locale's cache. Otherwise clears all.
|
|
171
|
-
*/
|
|
172
|
-
function clearCache(locale) {
|
|
173
|
-
if (!i18nInstance) throwNotInitialized();
|
|
174
|
-
const cache = i18nInstance.cache;
|
|
175
|
-
if (locale) cache[locale] = {};
|
|
176
|
-
else for (const key in cache) cache[key] = {};
|
|
177
|
-
}
|
|
178
|
-
//#endregion
|
|
179
|
-
export { clearCache, currentLocale, getFormatterInstance, getI18nInstance, initializeI18n, t, useFormat, useI18n, useI18nAsync };
|
|
1
|
+
import { a as getPluralCategory, c as useFormat, i as getI18nInstance, l as useI18n, n as currentLocale, o as initializeI18n, r as getFormatterInstance, s as t, t as clearCache, u as useI18nAsync } from "./runtime-Civir_tv.mjs";
|
|
2
|
+
export { clearCache, currentLocale, getFormatterInstance, getI18nInstance, getPluralCategory, initializeI18n, t, useFormat, useI18n, useI18nAsync };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { n as currentLocale, o as initializeI18n, s as t$1 } from "./runtime-Civir_tv.mjs";
|
|
2
|
+
//#region src/index.ts
|
|
3
|
+
let locales = ["en"];
|
|
4
|
+
let defaultLocale = "en";
|
|
5
|
+
let prefixDefaultLocale = true;
|
|
6
|
+
let translations = {};
|
|
7
|
+
try {
|
|
8
|
+
const cfg = await import("virtual:rimelight-i18n-config");
|
|
9
|
+
if (cfg) {
|
|
10
|
+
if (cfg.locales) locales = cfg.locales;
|
|
11
|
+
if (cfg.defaultLocale) defaultLocale = cfg.defaultLocale;
|
|
12
|
+
if (cfg.prefixDefaultLocale !== void 0) prefixDefaultLocale = cfg.prefixDefaultLocale;
|
|
13
|
+
if (cfg.translations) translations = cfg.translations;
|
|
14
|
+
}
|
|
15
|
+
} catch {}
|
|
16
|
+
if (translations && Object.keys(translations).length > 0) initializeI18n({
|
|
17
|
+
defaultLocale: defaultLocale || "en",
|
|
18
|
+
translations
|
|
19
|
+
});
|
|
20
|
+
/**
|
|
21
|
+
* Translates a single string key using dot-notation (e.g. `t("page_home.section_hero_title")`).
|
|
22
|
+
* When augmented via CustomTranslations, keys are strictly type-checked and autocompleted.
|
|
23
|
+
*/
|
|
24
|
+
function t(key, params) {
|
|
25
|
+
return t$1(key, params);
|
|
26
|
+
}
|
|
27
|
+
function getLocale() {
|
|
28
|
+
return currentLocale.get() || defaultLocale || "en";
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Returns a standard BCP 47 / RFC 5646 language tag suitable for HTML `<html lang="...">`. Maps
|
|
32
|
+
* 2-letter codes like 'en' to 'en-US', 'pt' to 'pt-BR', 'es' to 'es-ES', etc.
|
|
33
|
+
*/
|
|
34
|
+
function getHtmlLang() {
|
|
35
|
+
const loc = getLocale();
|
|
36
|
+
return {
|
|
37
|
+
en: "en-US",
|
|
38
|
+
pt: "pt-BR",
|
|
39
|
+
es: "es-ES"
|
|
40
|
+
}[loc] || loc;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Returns an OpenGraph-compliant locale tag (e.g. 'en_US', 'pt_BR', 'es_ES').
|
|
44
|
+
*/
|
|
45
|
+
function getOgLocale() {
|
|
46
|
+
const loc = getLocale();
|
|
47
|
+
return {
|
|
48
|
+
en: "en_US",
|
|
49
|
+
pt: "pt_BR",
|
|
50
|
+
es: "es_ES"
|
|
51
|
+
}[loc] || `${loc}_${loc.toUpperCase()}`;
|
|
52
|
+
}
|
|
53
|
+
function getRelativeLocaleUrl(arg1, arg2) {
|
|
54
|
+
let targetLocale;
|
|
55
|
+
let targetPath;
|
|
56
|
+
if (arg2 !== void 0) {
|
|
57
|
+
targetLocale = arg1;
|
|
58
|
+
targetPath = arg2;
|
|
59
|
+
} else {
|
|
60
|
+
targetLocale = getLocale();
|
|
61
|
+
targetPath = arg1;
|
|
62
|
+
}
|
|
63
|
+
if (/^(?:[a-z]+:|\/\/|#)/i.test(targetPath) || targetPath.startsWith("mailto:") || targetPath.startsWith("tel:")) return targetPath;
|
|
64
|
+
const [pathWithoutQueryAndHash, queryAndHash] = (() => {
|
|
65
|
+
const match = targetPath.match(/^([^?#]*)([?#].*)?$/);
|
|
66
|
+
return [match?.[1] ?? "", match?.[2] ?? ""];
|
|
67
|
+
})();
|
|
68
|
+
const rawCleanPath = pathWithoutQueryAndHash.replace(/^\/+/, "");
|
|
69
|
+
const segments = rawCleanPath ? rawCleanPath.split("/") : [];
|
|
70
|
+
if (segments.length > 0 && locales.includes(segments[0])) segments.shift();
|
|
71
|
+
const cleanPath = segments.join("/");
|
|
72
|
+
const prefix = prefixDefaultLocale || targetLocale !== defaultLocale ? `/${targetLocale}` : "";
|
|
73
|
+
if (!cleanPath) return `${prefix ? `${prefix}/` : "/"}${queryAndHash}`;
|
|
74
|
+
return `${`${prefix}/${cleanPath}${pathWithoutQueryAndHash.endsWith("/") ? "/" : ""}`}${queryAndHash}`;
|
|
75
|
+
}
|
|
76
|
+
function getRelativeLocaleUrlList(path = "/") {
|
|
77
|
+
return locales.map((locale) => getRelativeLocaleUrl(locale, path));
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Generates an array of hreflang alternates for SEO tags (<link rel="alternate" hreflang="...">).
|
|
81
|
+
* Automatically includes all supported locales and 'x-default'.
|
|
82
|
+
*/
|
|
83
|
+
function getLanguageAlternates(pathname, siteUrl = "") {
|
|
84
|
+
const normalizedSiteUrl = siteUrl.replace(/\/+$/, "");
|
|
85
|
+
const alternates = locales.map((loc) => ({
|
|
86
|
+
hreflang: loc,
|
|
87
|
+
href: `${normalizedSiteUrl}${getRelativeLocaleUrl(loc, pathname)}`
|
|
88
|
+
}));
|
|
89
|
+
alternates.push({
|
|
90
|
+
hreflang: "x-default",
|
|
91
|
+
href: `${normalizedSiteUrl}${getRelativeLocaleUrl(defaultLocale, pathname)}`
|
|
92
|
+
});
|
|
93
|
+
return alternates;
|
|
94
|
+
}
|
|
95
|
+
let kvBinding = null;
|
|
96
|
+
function setKVBinding(kv) {
|
|
97
|
+
kvBinding = kv;
|
|
98
|
+
}
|
|
99
|
+
function createTranslationLoader(translations) {
|
|
100
|
+
return async (locale, components) => {
|
|
101
|
+
const localeTranslations = translations[locale] || {};
|
|
102
|
+
if (!components || !Array.isArray(components) || components.length === 0) return localeTranslations;
|
|
103
|
+
const activeKv = kvBinding;
|
|
104
|
+
if (activeKv) try {
|
|
105
|
+
const results = await Promise.all(components.map(async (name) => {
|
|
106
|
+
const key = `locale:${locale}:${name}`;
|
|
107
|
+
const data = await activeKv.get(key, "json");
|
|
108
|
+
if (data == null) return { [name]: localeTranslations[name] ?? {} };
|
|
109
|
+
return { [name]: data };
|
|
110
|
+
}));
|
|
111
|
+
return Object.assign({}, ...results);
|
|
112
|
+
} catch {}
|
|
113
|
+
const results = await Promise.all(components.map(async (name) => {
|
|
114
|
+
const data = localeTranslations[name];
|
|
115
|
+
return { [name]: data ?? {} };
|
|
116
|
+
}));
|
|
117
|
+
return Object.assign({}, ...results);
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
//#endregion
|
|
121
|
+
export { getLocale as a, getRelativeLocaleUrlList as c, setKVBinding as d, t as f, getLanguageAlternates as i, locales as l, defaultLocale as n, getOgLocale as o, getHtmlLang as r, getRelativeLocaleUrl as s, createTranslationLoader as t, prefixDefaultLocale as u };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ComponentsJSON } from "@nanostores/i18n";
|
|
2
|
+
//#region src/types.d.ts
|
|
3
|
+
interface FlattenedTranslations {
|
|
4
|
+
[key: string]: string;
|
|
5
|
+
}
|
|
6
|
+
interface LocaleFile {
|
|
7
|
+
[component: string]: {
|
|
8
|
+
[key: string]: string;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
interface KVNamespaceBinding {
|
|
12
|
+
get(key: string, type: "json"): Promise<Record<string, string> | null>;
|
|
13
|
+
put(key: string, value: string): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Derives dot-notated nested translation keys (e.g. "page_home.section_hero_title") from a
|
|
17
|
+
* translation schema type.
|
|
18
|
+
*/
|
|
19
|
+
type NestedTranslationKeys<T> = T extends object ? { [K in keyof T & (string | number)]: T[K] extends object ? `${K}.${NestedTranslationKeys<T[K]>}` : `${K}`; }[keyof T & (string | number)] : never;
|
|
20
|
+
/**
|
|
21
|
+
* Augmented interface for app-level strongly typed translation keys. Applications can declare:
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* declare module "@rimelight/i18n" {
|
|
25
|
+
* interface CustomTranslations extends typeof import("./translations/en.json") {}
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
interface CustomTranslations {}
|
|
30
|
+
type TranslationKey = [keyof CustomTranslations] extends [never] ? string : NestedTranslationKeys<CustomTranslations>;
|
|
31
|
+
//#endregion
|
|
32
|
+
export { LocaleFile as a, KVNamespaceBinding as i, CustomTranslations as n, NestedTranslationKeys as o, FlattenedTranslations as r, TranslationKey as s, ComponentsJSON as t };
|
package/dist/types.d.mts
CHANGED
|
@@ -1,16 +1,2 @@
|
|
|
1
|
-
import { ComponentsJSON } from "
|
|
2
|
-
|
|
3
|
-
interface FlattenedTranslations {
|
|
4
|
-
[key: string]: string;
|
|
5
|
-
}
|
|
6
|
-
interface LocaleFile {
|
|
7
|
-
[component: string]: {
|
|
8
|
-
[key: string]: string;
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
interface KVNamespaceBinding {
|
|
12
|
-
get(key: string, type: "json"): Promise<Record<string, string> | null>;
|
|
13
|
-
put(key: string, value: string): Promise<void>;
|
|
14
|
-
}
|
|
15
|
-
//#endregion
|
|
16
|
-
export { type ComponentsJSON, FlattenedTranslations, KVNamespaceBinding, LocaleFile };
|
|
1
|
+
import { a as LocaleFile, i as KVNamespaceBinding, n as CustomTranslations, o as NestedTranslationKeys, r as FlattenedTranslations, s as TranslationKey, t as ComponentsJSON } from "./types-BKUPwH-e.mjs";
|
|
2
|
+
export { type ComponentsJSON, CustomTranslations, FlattenedTranslations, KVNamespaceBinding, LocaleFile, NestedTranslationKeys, TranslationKey };
|
package/dist/utils.d.mts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { r as FlattenedTranslations, t as ComponentsJSON } from "./types-BKUPwH-e.mjs";
|
|
2
2
|
//#region src/utils.d.ts
|
|
3
3
|
declare function flatten(obj: ComponentsJSON): FlattenedTranslations;
|
|
4
4
|
declare function unflatten(flat: FlattenedTranslations): ComponentsJSON;
|
|
5
5
|
declare function extractKeys(source: ComponentsJSON): string[];
|
|
6
|
+
/**
|
|
7
|
+
* Compares a target translation dictionary against a base/source translation dictionary, returning
|
|
8
|
+
* any missing component keys.
|
|
9
|
+
*/
|
|
10
|
+
declare function findMissingKeys(base: Record<string, Record<string, any>>, target: Record<string, Record<string, any>>): string[];
|
|
6
11
|
//#endregion
|
|
7
|
-
export { extractKeys, flatten, unflatten };
|
|
12
|
+
export { extractKeys, findMissingKeys, flatten, unflatten };
|
package/dist/utils.mjs
CHANGED
|
@@ -24,5 +24,20 @@ function extractKeys(source) {
|
|
|
24
24
|
for (const [component, translations] of Object.entries(source)) if (translations && typeof translations === "object") for (const key of Object.keys(translations)) keys.push(`${component}.${key}`);
|
|
25
25
|
return keys;
|
|
26
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Compares a target translation dictionary against a base/source translation dictionary, returning
|
|
29
|
+
* any missing component keys.
|
|
30
|
+
*/
|
|
31
|
+
function findMissingKeys(base, target) {
|
|
32
|
+
const missing = [];
|
|
33
|
+
for (const [component, keys] of Object.entries(base)) {
|
|
34
|
+
if (!target[component] || typeof target[component] !== "object") {
|
|
35
|
+
missing.push(component);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
for (const key of Object.keys(keys)) if (target[component][key] === void 0) missing.push(`${component}.${key}`);
|
|
39
|
+
}
|
|
40
|
+
return missing;
|
|
41
|
+
}
|
|
27
42
|
//#endregion
|
|
28
|
-
export { extractKeys, flatten, unflatten };
|
|
43
|
+
export { extractKeys, findMissingKeys, flatten, unflatten };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rimelight/i18n",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Rimelight Entertainment's Internationalization Package",
|
|
6
6
|
"homepage": "https://rimelight.com/docs",
|
|
@@ -24,13 +24,13 @@
|
|
|
24
24
|
"types": "./dist/index.d.mts",
|
|
25
25
|
"import": "./dist/index.mjs"
|
|
26
26
|
},
|
|
27
|
-
"./
|
|
28
|
-
"types": "./dist/
|
|
29
|
-
"import": "./dist/
|
|
27
|
+
"./plugin": {
|
|
28
|
+
"types": "./dist/plugin.d.mts",
|
|
29
|
+
"import": "./dist/plugin.mjs"
|
|
30
30
|
},
|
|
31
|
-
"./
|
|
32
|
-
"types": "./dist/
|
|
33
|
-
"import": "./dist/
|
|
31
|
+
"./hono": {
|
|
32
|
+
"types": "./dist/hono.d.mts",
|
|
33
|
+
"import": "./dist/hono.mjs"
|
|
34
34
|
},
|
|
35
35
|
"./runtime": {
|
|
36
36
|
"types": "./dist/runtime.d.mts",
|
|
@@ -54,17 +54,8 @@
|
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"@rimelight/config": "0.0.4",
|
|
57
|
-
"astro": "7.3.1",
|
|
58
57
|
"typescript": "6.0.3"
|
|
59
58
|
},
|
|
60
|
-
"peerDependencies": {
|
|
61
|
-
"astro": ">=7.0.0"
|
|
62
|
-
},
|
|
63
|
-
"peerDependenciesMeta": {
|
|
64
|
-
"astro": {
|
|
65
|
-
"optional": true
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
59
|
"engines": {
|
|
69
60
|
"node": ">=26.7.0"
|
|
70
61
|
},
|
package/dist/integration.d.mts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { AstroIntegration } from "astro";
|
|
2
|
-
//#region src/integration.d.ts
|
|
3
|
-
interface RimelightI18nOptions {
|
|
4
|
-
validateExtraction?: boolean;
|
|
5
|
-
translations?: Record<string, Record<string, any>>;
|
|
6
|
-
kvBinding?: string;
|
|
7
|
-
translationLoader?: string;
|
|
8
|
-
}
|
|
9
|
-
declare function rimelightI18n(options?: RimelightI18nOptions): AstroIntegration[];
|
|
10
|
-
//#endregion
|
|
11
|
-
export { RimelightI18nOptions, rimelightI18n };
|
package/dist/integration.mjs
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
//#region src/integration.ts
|
|
4
|
-
function rimelightI18n(options) {
|
|
5
|
-
const validateExtraction = options?.validateExtraction ?? true;
|
|
6
|
-
const translations = options?.translations;
|
|
7
|
-
const kvBinding = options?.kvBinding ?? "TRANSLATIONS_KV";
|
|
8
|
-
const translationLoader = options?.translationLoader;
|
|
9
|
-
const normalisedTranslations = {};
|
|
10
|
-
if (translations) for (const [locale, val] of Object.entries(translations)) normalisedTranslations[locale] = val;
|
|
11
|
-
const VIRTUAL_MODULE_ID = "@rimelight/i18n:runtime";
|
|
12
|
-
const RESOLVED_VIRTUAL_MODULE_ID = "\0" + VIRTUAL_MODULE_ID;
|
|
13
|
-
const runtimeSpecifier = "@rimelight/i18n/runtime";
|
|
14
|
-
const loaderImport = translationLoader ? `import translationLoader from ${JSON.stringify(translationLoader)};` : "";
|
|
15
|
-
const loaderOption = translationLoader ? ", get: translationLoader" : "";
|
|
16
|
-
return [{
|
|
17
|
-
name: "@rimelight/i18n",
|
|
18
|
-
hooks: {
|
|
19
|
-
"astro:config:setup": ({ config, addMiddleware, updateConfig, logger }) => {
|
|
20
|
-
const locales = config.i18n?.locales ?? ["en"];
|
|
21
|
-
const defaultLocale = config.i18n?.defaultLocale ?? "en";
|
|
22
|
-
const virtualModuleContent = `\
|
|
23
|
-
import { initializeI18n, useFormat, useI18n, useI18nAsync, t,
|
|
24
|
-
currentLocale, getI18nInstance, getFormatterInstance, clearCache }
|
|
25
|
-
from ${JSON.stringify(runtimeSpecifier)};
|
|
26
|
-
${loaderImport}
|
|
27
|
-
|
|
28
|
-
initializeI18n({
|
|
29
|
-
defaultLocale: ${JSON.stringify(defaultLocale)},
|
|
30
|
-
translations: ${JSON.stringify(normalisedTranslations)}${loaderOption}
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
export { useFormat, useI18n, useI18nAsync, t, currentLocale,
|
|
34
|
-
getI18nInstance, getFormatterInstance, clearCache };
|
|
35
|
-
`;
|
|
36
|
-
addMiddleware({
|
|
37
|
-
entrypoint: "@rimelight/i18n/middleware",
|
|
38
|
-
order: "pre"
|
|
39
|
-
});
|
|
40
|
-
updateConfig({ vite: {
|
|
41
|
-
ssr: { noExternal: ["@rimelight/i18n"] },
|
|
42
|
-
plugins: [{
|
|
43
|
-
name: "vite-plugin-rimelight-i18n-runtime",
|
|
44
|
-
resolveId(id) {
|
|
45
|
-
if (id === VIRTUAL_MODULE_ID) return RESOLVED_VIRTUAL_MODULE_ID;
|
|
46
|
-
return null;
|
|
47
|
-
},
|
|
48
|
-
load(id) {
|
|
49
|
-
if (id === RESOLVED_VIRTUAL_MODULE_ID) return virtualModuleContent;
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
}, {
|
|
53
|
-
name: "vite-plugin-rimelight-i18n-config",
|
|
54
|
-
resolveId(id) {
|
|
55
|
-
if (id === "virtual:rimelight-i18n-config") return "\0" + id;
|
|
56
|
-
return null;
|
|
57
|
-
},
|
|
58
|
-
load(id) {
|
|
59
|
-
if (id === "\0virtual:rimelight-i18n-config") return `
|
|
60
|
-
export const locales = ${JSON.stringify(locales)};
|
|
61
|
-
export const defaultLocale = ${JSON.stringify(defaultLocale)};
|
|
62
|
-
`;
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
}]
|
|
66
|
-
} });
|
|
67
|
-
if (validateExtraction) logger.info("i18n extraction validation active");
|
|
68
|
-
},
|
|
69
|
-
"astro:config:done": ({ injectTypes }) => {
|
|
70
|
-
injectTypes({
|
|
71
|
-
filename: "rimelight-i18n.d.ts",
|
|
72
|
-
content: `\
|
|
73
|
-
declare module "@rimelight/i18n:runtime" {
|
|
74
|
-
import type { Translations } from '@nanostores/i18n';
|
|
75
|
-
export type { InitializeI18nOptions } from '@rimelight/i18n/runtime';
|
|
76
|
-
export const currentLocale: import('nanostores').PreinitializedWritableAtom<string> & object;
|
|
77
|
-
export declare function initializeI18n(options: import('@rimelight/i18n/runtime').InitializeI18nOptions): void;
|
|
78
|
-
export declare function useFormat(): import('@nanostores/i18n').Formatter;
|
|
79
|
-
export type ComponentMessages = Record<string, string>;
|
|
80
|
-
export declare function useI18n(componentName: string): ComponentMessages;
|
|
81
|
-
export declare function useI18n<Body extends Translations>(componentName: string, baseTranslations: Body): ComponentMessages & Body;
|
|
82
|
-
export declare function useI18nAsync(componentName: string): Promise<Record<string, string>>;
|
|
83
|
-
export declare function useI18nAsync<Body extends Translations>(componentName: string, baseTranslations: Body): Promise<Record<string, string> & Body>;
|
|
84
|
-
export declare function t(key: string, params?: Record<string, any>): string;
|
|
85
|
-
export declare function t(astro: { currentLocale?: string | undefined; params?: Record<string, any> } | undefined | null, key: string, params?: Record<string, any>): string;
|
|
86
|
-
export declare function getLocale(): string;
|
|
87
|
-
export declare function getI18nInstance(): ReturnType<typeof import('@nanostores/i18n').createI18n>;
|
|
88
|
-
export declare function getFormatterInstance(): ReturnType<typeof import('@nanostores/i18n').formatter>;
|
|
89
|
-
export declare function clearCache(locale?: string): void;
|
|
90
|
-
}
|
|
91
|
-
`
|
|
92
|
-
});
|
|
93
|
-
},
|
|
94
|
-
"astro:build:done": async ({ logger }) => {
|
|
95
|
-
if (translations) {
|
|
96
|
-
const seedData = { keys: [] };
|
|
97
|
-
for (const [locale, localeData] of Object.entries(translations)) for (const [component, componentData] of Object.entries(localeData)) seedData.keys.push({
|
|
98
|
-
key: `locale:${locale}:${component}`,
|
|
99
|
-
value: JSON.stringify(componentData)
|
|
100
|
-
});
|
|
101
|
-
const outDir = process.env.WRANGLER_OUT_DIR ?? "dist";
|
|
102
|
-
const seedFile = path.join(outDir, "_translations-seed.json");
|
|
103
|
-
fs.mkdirSync(path.dirname(seedFile), { recursive: true });
|
|
104
|
-
fs.writeFileSync(seedFile, JSON.stringify(seedData, null, 2));
|
|
105
|
-
logger.info(`translations seed file written to ${seedFile}`);
|
|
106
|
-
if (process.env.WRANGLER_SEED_KV === "true" || process.env.CI === "true") try {
|
|
107
|
-
const { execSync } = await import("node:child_process");
|
|
108
|
-
execSync(`npx wrangler kv:bulk put --binding "${kvBinding}" "${seedFile}"`, {
|
|
109
|
-
stdio: "pipe",
|
|
110
|
-
timeout: 3e4
|
|
111
|
-
});
|
|
112
|
-
logger.info(`KV "${kvBinding}" seeded with translations`);
|
|
113
|
-
} catch {
|
|
114
|
-
logger.warn(`could not seed KV automatically — run "npx wrangler kv:bulk put --binding "${kvBinding}" "${seedFile}"" to seed translations into KV`);
|
|
115
|
-
}
|
|
116
|
-
} else logger.info("i18n build complete (no translations to seed)");
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}];
|
|
120
|
-
}
|
|
121
|
-
//#endregion
|
|
122
|
-
export { rimelightI18n };
|
package/dist/middleware.d.mts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
//#region src/middleware/routing.d.ts
|
|
2
|
-
declare const i18n: (context: any, next: any) => Promise<any>;
|
|
3
|
-
//#endregion
|
|
4
|
-
//#region src/middleware.d.ts
|
|
5
|
-
declare const onRequest: (_context: unknown, next: () => Promise<Response>) => Promise<Response>;
|
|
6
|
-
//#endregion
|
|
7
|
-
export { i18n, onRequest };
|
package/dist/middleware.mjs
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { setKVBinding } from "./index.mjs";
|
|
2
|
-
import { currentLocale } from "./runtime.mjs";
|
|
3
|
-
import { env } from "cloudflare:workers";
|
|
4
|
-
import { defaultLocale, locales } from "virtual:rimelight-i18n-config";
|
|
5
|
-
//#region src/middleware/routing.ts
|
|
6
|
-
const SUPPORTED_LOCALES = new Set(locales);
|
|
7
|
-
const DEFAULT_LOCALE = defaultLocale;
|
|
8
|
-
function getPreferredLocale(acceptLanguage) {
|
|
9
|
-
if (!acceptLanguage) return DEFAULT_LOCALE;
|
|
10
|
-
const parsed = acceptLanguage.split(",").map((lang) => {
|
|
11
|
-
const parts = lang.split(";");
|
|
12
|
-
const code = (parts[0] ?? "").trim().toLowerCase();
|
|
13
|
-
const base = code.split("-")[0] ?? "";
|
|
14
|
-
let q = 1;
|
|
15
|
-
const qualityPart = parts[1];
|
|
16
|
-
if (qualityPart) {
|
|
17
|
-
const qMatch = qualityPart.match(/q=([0-9.]+)/);
|
|
18
|
-
if (qMatch) q = parseFloat(qMatch[1] ?? "1");
|
|
19
|
-
}
|
|
20
|
-
return {
|
|
21
|
-
code,
|
|
22
|
-
base,
|
|
23
|
-
q
|
|
24
|
-
};
|
|
25
|
-
}).toSorted((a, b) => b.q - a.q);
|
|
26
|
-
for (const item of parsed) {
|
|
27
|
-
if (SUPPORTED_LOCALES.has(item.code)) return item.code;
|
|
28
|
-
const base = item.base;
|
|
29
|
-
if (SUPPORTED_LOCALES.has(base)) return base;
|
|
30
|
-
}
|
|
31
|
-
return DEFAULT_LOCALE;
|
|
32
|
-
}
|
|
33
|
-
const i18n = async (context, next) => {
|
|
34
|
-
const url = new URL(context.request.url);
|
|
35
|
-
const pathname = url.pathname;
|
|
36
|
-
const paramLocale = context.params?.locale;
|
|
37
|
-
const firstSegment = pathname.split("/").find(Boolean);
|
|
38
|
-
const activeLocale = (paramLocale && SUPPORTED_LOCALES.has(paramLocale) ? paramLocale : null) || (firstSegment && SUPPORTED_LOCALES.has(firstSegment) ? firstSegment : null) || context.currentLocale || DEFAULT_LOCALE;
|
|
39
|
-
currentLocale.set(activeLocale);
|
|
40
|
-
if (context.request.method !== "GET" && context.request.method !== "HEAD") return next();
|
|
41
|
-
if (pathname.startsWith("/api") || pathname.startsWith("/_") || pathname.includes(".")) return next();
|
|
42
|
-
if (firstSegment && SUPPORTED_LOCALES.has(firstSegment)) return next();
|
|
43
|
-
const locale = getPreferredLocale(context.request.headers.get("accept-language"));
|
|
44
|
-
currentLocale.set(locale);
|
|
45
|
-
const targetPath = `/${locale}${pathname}${url.search}`;
|
|
46
|
-
return context.redirect(targetPath, 302);
|
|
47
|
-
};
|
|
48
|
-
//#endregion
|
|
49
|
-
//#region src/middleware.ts
|
|
50
|
-
const onRequest = async (_context, next) => {
|
|
51
|
-
const kv = Reflect.get(env, "TRANSLATIONS_KV");
|
|
52
|
-
if (kv) setKVBinding(kv);
|
|
53
|
-
return next();
|
|
54
|
-
};
|
|
55
|
-
//#endregion
|
|
56
|
-
export { i18n, onRequest };
|