@fluentic/i18n-nextjs 0.0.0-stage → 0.1.0-beta.1

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/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @fluentic/i18n-nextjs
2
+
3
+ ## 0.1.0-beta.1
4
+
5
+ ### Minor Changes
6
+
7
+ - Publish the first public beta of the Next.js App Router and React Server Component bindings.
8
+ - Add request-scoped server state, client hydration, configuration helpers, and source-compilation integration.
9
+
10
+ ### Patch Changes
11
+
12
+ - Updated dependencies
13
+ - @fluentic/i18n@0.1.0-beta.1
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Fluentic
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,12 @@
1
- # Temporary Holding Version
1
+ # @fluentic/i18n-nextjs
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Next.js App Router adapter with conditional RSC/client exports. The app supplies current language and loader through `defineRequestConfig`; routing stays application-owned.
4
+
5
+ - `/plugin`: one Next config wrapper for webpack and Turbopack.
6
+ - `/config`: `defineRequestConfig` and request types.
7
+ - `/server`: `ready`, `resolveI18n`, `runWithI18n`, and server provider.
8
+ - Root entry: server or client `I18nProvider`; client state and renderer binding.
9
+
10
+ Both `t.text` and `t.jsx` remain synchronous. Await `ready()` in each independent server execution path before rendering. Use request snapshots for hydration, and `runWithI18n` for explicit async scopes in Node route handlers/actions. No process-global locale is changed per request. Edge runtime support is not included.
11
+
12
+ See [integration guide](../../docs/docs/runtime/nextjs.md) and `examples/nextjs`.
@@ -0,0 +1,4 @@
1
+
2
+ import { I18nManager, I18nProvider, I18nState, createI18nManager, defineTranslationLoader, useI18nRenderer, useI18nRuntime } from "@fluentic/i18n/react";
3
+ import { I18nSnapshot, ManagedI18n } from "@fluentic/i18n/adapter";
4
+ export { I18nManager, I18nProvider, type I18nSnapshot, I18nState, type ManagedI18n, createI18nManager, defineTranslationLoader, useI18nRenderer, useI18nRuntime };
@@ -0,0 +1,2 @@
1
+ 'use client';
2
+ export { I18nManager, I18nProvider, I18nState, createI18nManager, defineTranslationLoader, useI18nRenderer, useI18nRuntime } from "@fluentic/i18n/react";
@@ -0,0 +1,11 @@
1
+
2
+ import { Formats, I18nManagerOptions, TranslationData, TranslationLoader } from "@fluentic/i18n/adapter";
3
+ type RequestConfig = I18nManagerOptions & {
4
+ translationLoader?: TranslationLoader;
5
+ translations?: TranslationData;
6
+ formats?: Partial<Formats>;
7
+ };
8
+ type RequestConfigFactory = () => RequestConfig | Promise<RequestConfig>;
9
+ declare function defineRequestConfig<T extends RequestConfigFactory>(factory: T): T;
10
+ export { RequestConfig, RequestConfigFactory, defineRequestConfig };
11
+ //# sourceMappingURL=config.d.mts.map
@@ -0,0 +1,8 @@
1
+
2
+ // src/config.ts
3
+ function defineRequestConfig(factory) {
4
+ return factory;
5
+ }
6
+ export { defineRequestConfig };
7
+
8
+ //# sourceMappingURL=config.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.mjs","names":[],"sources":["../src/config.ts"],"sourcesContent":["import type { Formats, I18nManagerOptions, TranslationData, TranslationLoader } from '@fluentic/i18n/adapter';\nexport type RequestConfig = I18nManagerOptions & {\n translationLoader?: TranslationLoader;\n translations?: TranslationData;\n formats?: Partial<Formats>;\n};\nexport type RequestConfigFactory = () => RequestConfig | Promise<RequestConfig>;\nexport function defineRequestConfig<T extends RequestConfigFactory>(factory: T): T {\n return factory;\n}\n"],"mappings":";;AAOA,SAAgB,oBAAoD,SAAe;CACjF,OAAO;AACT"}
@@ -0,0 +1,10 @@
1
+
2
+ type NextPluginOptions = {
3
+ requestConfig: string;
4
+ config?: string;
5
+ compiled?: boolean;
6
+ };
7
+ /** One Next integration; preserves the application's existing webpack hook/rules. */
8
+ declare function withFluentic(options: NextPluginOptions): (nextConfig?: any) => any;
9
+ export { NextPluginOptions, withFluentic as default };
10
+ //# sourceMappingURL=plugin.d.mts.map
@@ -0,0 +1,60 @@
1
+
2
+ import { createRequire } from "node:module";
3
+ import FluenticPlugin from "@fluentic/i18n/plugin/webpack";
4
+ import { relative, resolve } from "node:path";
5
+ // src/plugin.ts
6
+ const require = createRequire(import.meta.url);
7
+ /** One Next integration; preserves the application's existing webpack hook/rules. */
8
+ function withFluentic(options) {
9
+ const request = resolve(options.requestConfig);
10
+ const loader = require.resolve("@fluentic/i18n/plugin/webpack-loader");
11
+ const compilerOptions = {
12
+ ...options.config ? { config: resolve(options.config) } : {},
13
+ ...options.compiled !== void 0 ? { compiled: options.compiled } : {}
14
+ };
15
+ return (nextConfig = {}) => ({
16
+ ...nextConfig,
17
+ turbopack: {
18
+ ...nextConfig.turbopack,
19
+ resolveAlias: {
20
+ ...nextConfig.turbopack?.resolveAlias,
21
+ "@fluentic/i18n-nextjs/request": "./" + relative(process.cwd(), request).replace(/\\/g, "/")
22
+ },
23
+ rules: {
24
+ ...nextConfig.turbopack?.rules,
25
+ ...Object.fromEntries([
26
+ "*.js",
27
+ "*.jsx",
28
+ "*.ts",
29
+ "*.tsx",
30
+ "*.mjs",
31
+ "*.mts"
32
+ ].map((pattern) => [pattern, [...nextConfig.turbopack?.rules?.[pattern] ? [nextConfig.turbopack.rules[pattern]].flat() : [], {
33
+ condition: { not: "foreign" },
34
+ loaders: [{
35
+ loader,
36
+ options: compilerOptions
37
+ }]
38
+ }]])),
39
+ "*.jsonl": {
40
+ loaders: [{
41
+ loader,
42
+ options: compilerOptions
43
+ }],
44
+ as: "*.js"
45
+ }
46
+ }
47
+ },
48
+ webpack(config, context) {
49
+ config.resolve ??= {};
50
+ config.resolve.alias ??= {};
51
+ config.resolve.alias["@fluentic/i18n-nextjs/request$"] = request;
52
+ config.plugins ??= [];
53
+ config.plugins.push(new FluenticPlugin(compilerOptions));
54
+ return nextConfig.webpack ? nextConfig.webpack(config, context) : config;
55
+ }
56
+ });
57
+ }
58
+ export { withFluentic as default };
59
+
60
+ //# sourceMappingURL=plugin.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.mjs","names":[],"sources":["../src/plugin.ts"],"sourcesContent":["import FluenticPlugin from '@fluentic/i18n/plugin/webpack';\nimport { createRequire } from 'node:module';\nimport { relative, resolve } from 'node:path';\nconst require = createRequire(import.meta.url);\nexport type NextPluginOptions = { requestConfig: string; config?: string; compiled?: boolean; };\n/** One Next integration; preserves the application's existing webpack hook/rules. */\nexport default function withFluentic(options: NextPluginOptions) {\n const request = resolve(options.requestConfig);\n const loader = require.resolve('@fluentic/i18n/plugin/webpack-loader');\n const compilerOptions = {\n ...(options.config ? { config: resolve(options.config) } : {}),\n ...(options.compiled !== undefined ? { compiled: options.compiled } : {}),\n };\n return (nextConfig: any = {}) => ({\n ...nextConfig,\n turbopack: {\n ...nextConfig.turbopack,\n resolveAlias: {\n ...nextConfig.turbopack?.resolveAlias,\n '@fluentic/i18n-nextjs/request': './' + relative(process.cwd(), request).replace(/\\\\/g, '/'),\n },\n rules: {\n ...nextConfig.turbopack?.rules,\n ...Object.fromEntries(['*.js', '*.jsx', '*.ts', '*.tsx', '*.mjs', '*.mts'].map((pattern) => [pattern, [\n ...(nextConfig.turbopack?.rules?.[pattern] ? [nextConfig.turbopack.rules[pattern]].flat() : []),\n { condition: { not: 'foreign' }, loaders: [{ loader, options: compilerOptions }] },\n ]])),\n '*.jsonl': { loaders: [{ loader, options: compilerOptions }], as: '*.js' },\n },\n },\n webpack(config: any, context: any) {\n config.resolve ??= {};\n config.resolve.alias ??= {};\n config.resolve.alias['@fluentic/i18n-nextjs/request$'] = request;\n config.plugins ??= [];\n config.plugins.push(new FluenticPlugin(compilerOptions));\n return nextConfig.webpack ? nextConfig.webpack(config, context) : config;\n },\n });\n}\n"],"mappings":";;;;;AAGA,MAAM,UAAU,cAAc,YAAY,GAAG;;AAG7C,SAAwB,aAAa,SAA4B;CAC/D,MAAM,UAAU,QAAQ,QAAQ,aAAa;CAC7C,MAAM,SAAS,QAAQ,QAAQ,sCAAsC;CACrE,MAAM,kBAAkB;EACtB,GAAI,QAAQ,SAAS,EAAE,QAAQ,QAAQ,QAAQ,MAAM,EAAE,IAAI,CAAC;EAC5D,GAAI,QAAQ,aAAa,KAAA,IAAY,EAAE,UAAU,QAAQ,SAAS,IAAI,CAAC;CACzE;CACA,QAAQ,aAAkB,CAAC,OAAO;EAChC,GAAG;EACH,WAAW;GACT,GAAG,WAAW;GACd,cAAc;IACZ,GAAG,WAAW,WAAW;IACzB,iCAAiC,OAAO,SAAS,QAAQ,IAAI,GAAG,OAAO,CAAC,CAAC,QAAQ,OAAO,GAAG;GAC7F;GACA,OAAO;IACL,GAAG,WAAW,WAAW;IACzB,GAAG,OAAO,YAAY;KAAC;KAAQ;KAAS;KAAQ;KAAS;KAAS;IAAO,CAAC,CAAC,KAAK,YAAY,CAAC,SAAS,CACpG,GAAI,WAAW,WAAW,QAAQ,WAAW,CAAC,WAAW,UAAU,MAAM,QAAQ,CAAC,CAAC,KAAK,IAAI,CAAC,GAC7F;KAAE,WAAW,EAAE,KAAK,UAAU;KAAG,SAAS,CAAC;MAAE;MAAQ,SAAS;KAAgB,CAAC;IAAE,CACnF,CAAC,CAAC,CAAC;IACH,WAAW;KAAE,SAAS,CAAC;MAAE;MAAQ,SAAS;KAAgB,CAAC;KAAG,IAAI;IAAO;GAC3E;EACF;EACA,QAAQ,QAAa,SAAc;GACjC,OAAO,YAAY,CAAC;GACpB,OAAO,QAAQ,UAAU,CAAC;GAC1B,OAAO,QAAQ,MAAM,oCAAoC;GACzD,OAAO,YAAY,CAAC;GACpB,OAAO,QAAQ,KAAK,IAAI,eAAe,eAAe,CAAC;GACvD,OAAO,WAAW,UAAU,WAAW,QAAQ,QAAQ,OAAO,IAAI;EACpE;CACF;AACF"}
@@ -0,0 +1,5 @@
1
+
2
+ import { RequestConfigFactory } from "./config.mjs";
3
+ declare const missing: RequestConfigFactory;
4
+ export { missing as default };
5
+ //# sourceMappingURL=request.d.mts.map
@@ -0,0 +1,8 @@
1
+
2
+ // src/request.ts
3
+ const missing = () => {
4
+ throw new Error("[i18n] configure @fluentic/i18n-nextjs/plugin with requestConfig pointing to your request module");
5
+ };
6
+ export { missing as default };
7
+
8
+ //# sourceMappingURL=request.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request.mjs","names":[],"sources":["../src/request.ts"],"sourcesContent":["import type { RequestConfigFactory } from './config';\nconst missing: RequestConfigFactory = () => {\n throw new Error('[i18n] configure @fluentic/i18n-nextjs/plugin with requestConfig pointing to your request module');\n};\nexport default missing;\n"],"mappings":";;AACA,MAAM,gBAAsC;CAC1C,MAAM,IAAI,MAAM,kGAAkG;AACpH"}
@@ -0,0 +1,29 @@
1
+
2
+ import { RequestConfig, RequestConfigFactory, defineRequestConfig } from "./config.mjs";
3
+ import "server-only";
4
+ import { I18nSnapshot, ManagedI18n, createI18nManager, defineTranslationLoader } from "@fluentic/i18n/adapter";
5
+ import React from "react";
6
+ /** Await in the execution path that uses synchronous t.text; layouts do not order siblings. */
7
+ declare function ready(): Promise<I18nSnapshot>;
8
+ declare function resolveI18n(config: RequestConfig): Promise<I18nSnapshot>;
9
+ /** Route handlers, server actions: isolate the entire async operation explicitly. */
10
+ declare function runWithI18n<T>(config: RequestConfig, fn: () => T | Promise<T>): Promise<T>;
11
+ declare function I18nProvider({ children, snapshot }: {
12
+ children?: React.ReactNode;
13
+ snapshot?: I18nSnapshot;
14
+ }): Promise<React.FunctionComponentElement<{
15
+ children?: React.ReactNode;
16
+ snapshot?: I18nSnapshot;
17
+ instance?: ManagedI18n;
18
+ lineBreak?: React.ReactNode;
19
+ }>>;
20
+ declare const I18nManager: ManagedI18n['manager'];
21
+ declare const I18nState: {
22
+ get: () => import("@fluentic/i18n/adapter").StateData;
23
+ subscribe: (_listener: () => void) => () => void;
24
+ use: () => import("@fluentic/i18n/adapter").StateData;
25
+ };
26
+ declare function useI18nRuntime(): import("@fluentic/i18n/adapter").Runtime;
27
+ declare function useI18nRenderer<F extends (...args: any[]) => any>(renderer: F): F;
28
+ export { I18nManager, I18nProvider, I18nState, type RequestConfig, type RequestConfigFactory, createI18nManager, defineRequestConfig, defineTranslationLoader, ready, resolveI18n, runWithI18n, useI18nRenderer, useI18nRuntime };
29
+ //# sourceMappingURL=server.d.mts.map
@@ -0,0 +1,69 @@
1
+
2
+ import { I18nProvider as I18nProvider$1 } from "./client.mjs";
3
+ import { defineRequestConfig } from "./config.mjs";
4
+ import "server-only";
5
+ import requestConfig from "@fluentic/i18n-nextjs/request";
6
+ import { createI18nManager, createI18nManager as createI18nManager$1, defineTranslationLoader, setRuntimeResolver, withRuntime } from "@fluentic/i18n/adapter";
7
+ import { AsyncLocalStorage } from "node:async_hooks";
8
+ import React, { cache } from "react";
9
+ // src/server.ts
10
+ const explicitScope = new AsyncLocalStorage();
11
+ function createServerManaged() {
12
+ const managed = createI18nManager$1();
13
+ managed.runtime.jsxHandler = (_options, render) => React.createElement(ServerMessage, { render });
14
+ managed.runtime.tagElement = (element) => {
15
+ if (!React.isValidElement(element)) throw new TypeError("[i18n] t.tag expects a React element or handler");
16
+ return (chunks) => React.cloneElement(element, void 0, ...chunks);
17
+ };
18
+ return managed;
19
+ }
20
+ const getManaged = cache(createServerManaged);
21
+ setRuntimeResolver(() => (explicitScope.getStore() ?? getManaged()).runtime);
22
+ async function initialize(managed, config) {
23
+ managed.manager.configure(config);
24
+ if (config.formats) managed.runtime.formats = config.formats;
25
+ managed.manager.setTranslationLoader(config.translationLoader ?? (({ languageId }) => languageId === config.languageId ? config.translations ?? [] : []));
26
+ await managed.manager.init();
27
+ return managed;
28
+ }
29
+ const getRequestManaged = cache(async () => initialize(getManaged(), await requestConfig()));
30
+ /** Await in the execution path that uses synchronous t.text; layouts do not order siblings. */
31
+ async function ready() {
32
+ return (explicitScope.getStore() ?? await getRequestManaged()).manager.getSnapshot();
33
+ }
34
+ async function resolveI18n(config) {
35
+ return (await initialize(createServerManaged(), config)).manager.getSnapshot();
36
+ }
37
+ /** Route handlers, server actions: isolate the entire async operation explicitly. */
38
+ async function runWithI18n(config, fn) {
39
+ const managed = await initialize(createServerManaged(), config);
40
+ return explicitScope.run(managed, fn);
41
+ }
42
+ function ServerMessage({ render }) {
43
+ const runtime = (explicitScope.getStore() ?? getManaged()).runtime;
44
+ return React.createElement(React.Fragment, null, ...render(runtime).map((part, index) => React.createElement(React.Fragment, { key: index }, part)));
45
+ }
46
+ async function I18nProvider({ children, snapshot }) {
47
+ return React.createElement(I18nProvider$1, { snapshot: snapshot ?? await ready() }, children);
48
+ }
49
+ const I18nManager = new Proxy({}, { get(_target, key) {
50
+ return (...args) => {
51
+ const manager = (explicitScope.getStore() ?? getManaged()).manager;
52
+ return Reflect.apply(manager[key], manager, args);
53
+ };
54
+ } });
55
+ const I18nState = {
56
+ get: () => (explicitScope.getStore() ?? getManaged()).state.get(),
57
+ subscribe: (_listener) => () => {},
58
+ use: () => (explicitScope.getStore() ?? getManaged()).state.get()
59
+ };
60
+ function useI18nRuntime() {
61
+ return (explicitScope.getStore() ?? getManaged()).runtime;
62
+ }
63
+ function useI18nRenderer(renderer) {
64
+ const runtime = useI18nRuntime();
65
+ return ((...args) => withRuntime(runtime, () => renderer(...args)));
66
+ }
67
+ export { I18nManager, I18nProvider, I18nState, createI18nManager, defineRequestConfig, defineTranslationLoader, ready, resolveI18n, runWithI18n, useI18nRenderer, useI18nRuntime };
68
+
69
+ //# sourceMappingURL=server.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.mjs","names":["createI18nManager","ClientProvider"],"sources":["../src/server.ts"],"sourcesContent":["import 'server-only';\nimport requestConfig from '@fluentic/i18n-nextjs/request';\nimport {\n createI18nManager,\n type I18nSnapshot,\n type ManagedI18n,\n type PartsRenderer,\n setRuntimeResolver,\n withRuntime,\n} from '@fluentic/i18n/adapter';\nimport { AsyncLocalStorage } from 'node:async_hooks';\nimport React, { cache } from 'react';\nimport { I18nProvider as ClientProvider } from './client';\nimport type { RequestConfig } from './config';\nexport { defineRequestConfig } from './config';\nexport type { RequestConfig, RequestConfigFactory } from './config';\nconst explicitScope = new AsyncLocalStorage<ManagedI18n>();\nfunction createServerManaged() {\n const managed = createI18nManager();\n managed.runtime.jsxHandler = (_options, render) => React.createElement(ServerMessage, { render });\n managed.runtime.tagElement = (element) => {\n if (!React.isValidElement(element)) throw new TypeError('[i18n] t.tag expects a React element or handler');\n return (chunks) => React.cloneElement(element, undefined, ...chunks as React.ReactNode[]);\n };\n return managed;\n}\nconst getManaged = cache(createServerManaged);\nsetRuntimeResolver(() => (explicitScope.getStore() ?? getManaged()).runtime);\nasync function initialize(managed: ManagedI18n, config: RequestConfig) {\n managed.manager.configure(config);\n if (config.formats) managed.runtime.formats = config.formats;\n managed.manager.setTranslationLoader(\n config.translationLoader ?? (({ languageId }) => languageId === config.languageId ? config.translations ?? [] : []),\n );\n await managed.manager.init();\n return managed;\n}\nconst getRequestManaged = cache(async () => initialize(getManaged(), await requestConfig()));\n/** Await in the execution path that uses synchronous t.text; layouts do not order siblings. */\nexport async function ready(): Promise<I18nSnapshot> {\n const current = explicitScope.getStore() ?? await getRequestManaged();\n return current.manager.getSnapshot();\n}\nexport async function resolveI18n(config: RequestConfig): Promise<I18nSnapshot> {\n return (await initialize(createServerManaged(), config)).manager.getSnapshot();\n}\n/** Route handlers, server actions: isolate the entire async operation explicitly. */\nexport async function runWithI18n<T>(config: RequestConfig, fn: () => T | Promise<T>): Promise<T> {\n const managed = await initialize(createServerManaged(), config);\n return explicitScope.run(managed, fn);\n}\nfunction ServerMessage({ render }: { render: PartsRenderer; }) {\n const runtime = (explicitScope.getStore() ?? getManaged()).runtime;\n // t.jsx is synchronous. The caller/provider must initialize before actual rendering.\n return React.createElement(\n React.Fragment,\n null,\n ...render(runtime).map((part, index) =>\n React.createElement(React.Fragment, { key: index }, part as React.ReactNode)\n ),\n );\n}\nexport async function I18nProvider({ children, snapshot }: { children?: React.ReactNode; snapshot?: I18nSnapshot; }) {\n return React.createElement(ClientProvider, { snapshot: snapshot ?? await ready() }, children);\n}\n// Resolve at invocation time, never capture a request in a module-level singleton.\nexport const I18nManager: ManagedI18n['manager'] = new Proxy({} as ManagedI18n['manager'], {\n get(_target, key: keyof ManagedI18n['manager']) {\n return (...args: unknown[]) => {\n const manager = (explicitScope.getStore() ?? getManaged()).manager;\n return Reflect.apply(manager[key], manager, args);\n };\n },\n});\nexport const I18nState = {\n get: () => (explicitScope.getStore() ?? getManaged()).state.get(),\n subscribe: (_listener: () => void) => () => {},\n use: () => (explicitScope.getStore() ?? getManaged()).state.get(),\n};\nexport function useI18nRuntime() {\n return (explicitScope.getStore() ?? getManaged()).runtime;\n}\nexport function useI18nRenderer<F extends (...args: any[]) => any>(renderer: F): F {\n const runtime = useI18nRuntime();\n return ((...args: Parameters<F>) => withRuntime(runtime, () => renderer(...args))) as F;\n}\nexport { createI18nManager, defineTranslationLoader } from '@fluentic/i18n/adapter';\n"],"mappings":";;;;;;;;;AAgBA,MAAM,gBAAgB,IAAI,kBAA+B;AACzD,SAAS,sBAAsB;CAC7B,MAAM,UAAUA,oBAAkB;CAClC,QAAQ,QAAQ,cAAc,UAAU,WAAW,MAAM,cAAc,eAAe,EAAE,OAAO,CAAC;CAChG,QAAQ,QAAQ,cAAc,YAAY;EACxC,IAAI,CAAC,MAAM,eAAe,OAAO,GAAG,MAAM,IAAI,UAAU,iDAAiD;EACzG,QAAQ,WAAW,MAAM,aAAa,SAAS,KAAA,GAAW,GAAG,MAA2B;CAC1F;CACA,OAAO;AACT;AACA,MAAM,aAAa,MAAM,mBAAmB;AAC5C,0BAA0B,cAAc,SAAS,KAAK,WAAW,EAAA,CAAG,OAAO;AAC3E,eAAe,WAAW,SAAsB,QAAuB;CACrE,QAAQ,QAAQ,UAAU,MAAM;CAChC,IAAI,OAAO,SAAS,QAAQ,QAAQ,UAAU,OAAO;CACrD,QAAQ,QAAQ,qBACd,OAAO,uBAAuB,EAAE,iBAAiB,eAAe,OAAO,aAAa,OAAO,gBAAgB,CAAC,IAAI,CAAC,EACnH;CACA,MAAM,QAAQ,QAAQ,KAAK;CAC3B,OAAO;AACT;AACA,MAAM,oBAAoB,MAAM,YAAY,WAAW,WAAW,GAAG,MAAM,cAAc,CAAC,CAAC;;AAE3F,eAAsB,QAA+B;CAEnD,QADgB,cAAc,SAAS,KAAK,MAAM,kBAAkB,EAAA,CACrD,QAAQ,YAAY;AACrC;AACA,eAAsB,YAAY,QAA8C;CAC9E,QAAQ,MAAM,WAAW,oBAAoB,GAAG,MAAM,EAAA,CAAG,QAAQ,YAAY;AAC/E;;AAEA,eAAsB,YAAe,QAAuB,IAAsC;CAChG,MAAM,UAAU,MAAM,WAAW,oBAAoB,GAAG,MAAM;CAC9D,OAAO,cAAc,IAAI,SAAS,EAAE;AACtC;AACA,SAAS,cAAc,EAAE,UAAsC;CAC7D,MAAM,WAAW,cAAc,SAAS,KAAK,WAAW,EAAA,CAAG;CAE3D,OAAO,MAAM,cACX,MAAM,UACN,MACA,GAAG,OAAO,OAAO,CAAC,CAAC,KAAK,MAAM,UAC5B,MAAM,cAAc,MAAM,UAAU,EAAE,KAAK,MAAM,GAAG,IAAuB,CAC7E,CACF;AACF;AACA,eAAsB,aAAa,EAAE,UAAU,YAAsE;CACnH,OAAO,MAAM,cAAcC,gBAAgB,EAAE,UAAU,YAAY,MAAM,MAAM,EAAE,GAAG,QAAQ;AAC9F;AAEA,MAAa,cAAsC,IAAI,MAAM,CAAC,GAA6B,EACzF,IAAI,SAAS,KAAmC;CAC9C,QAAQ,GAAG,SAAoB;EAC7B,MAAM,WAAW,cAAc,SAAS,KAAK,WAAW,EAAA,CAAG;EAC3D,OAAO,QAAQ,MAAM,QAAQ,MAAM,SAAS,IAAI;CAClD;AACF,EACF,CAAC;AACD,MAAa,YAAY;CACvB,YAAY,cAAc,SAAS,KAAK,WAAW,EAAA,CAAG,MAAM,IAAI;CAChE,YAAY,oBAAgC,CAAC;CAC7C,YAAY,cAAc,SAAS,KAAK,WAAW,EAAA,CAAG,MAAM,IAAI;AAClE;AACA,SAAgB,iBAAiB;CAC/B,QAAQ,cAAc,SAAS,KAAK,WAAW,EAAA,CAAG;AACpD;AACA,SAAgB,gBAAmD,UAAgB;CACjF,MAAM,UAAU,eAAe;CAC/B,SAAS,GAAG,SAAwB,YAAY,eAAe,SAAS,GAAG,IAAI,CAAC;AAClF"}
@@ -0,0 +1,2 @@
1
+
2
+ export {}
@@ -0,0 +1,6 @@
1
+
2
+ // src/unavailable.ts
3
+ throw new Error("[i18n] @fluentic/i18n-nextjs/server is only available on the server");
4
+ export {};
5
+
6
+ //# sourceMappingURL=unavailable.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unavailable.mjs","names":[],"sources":["../src/unavailable.ts"],"sourcesContent":["throw new Error('[i18n] @fluentic/i18n-nextjs/server is only available on the server');\nexport {};\n"],"mappings":";;AAAA,MAAM,IAAI,MAAM,qEAAqE"}
package/package.json CHANGED
@@ -1,6 +1,75 @@
1
1
  {
2
2
  "name": "@fluentic/i18n-nextjs",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0-beta.1",
4
+ "description": "Next.js App Router and React Server Component bindings for Fluentic i18n.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/fluentic-dev/i18n.git",
10
+ "directory": "packages/nextjs"
11
+ },
12
+ "homepage": "https://fluenticstack.com/i18n/",
13
+ "bugs": {
14
+ "url": "https://github.com/fluentic-dev/i18n/issues"
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "files": [
20
+ "dist",
21
+ "src",
22
+ "README.md",
23
+ "CHANGELOG.md",
24
+ "LICENSE"
25
+ ],
26
+ "sideEffects": [
27
+ "./dist/server.mjs"
28
+ ],
29
+ "exports": {
30
+ ".": {
31
+ "types": "./src/client.ts",
32
+ "react-server": "./dist/server.mjs",
33
+ "default": "./dist/client.mjs"
34
+ },
35
+ "./server": {
36
+ "types": "./src/server.ts",
37
+ "react-server": "./dist/server.mjs",
38
+ "node": "./dist/server.mjs",
39
+ "default": "./dist/unavailable.mjs"
40
+ },
41
+ "./config": {
42
+ "types": "./src/config.ts",
43
+ "default": "./dist/config.mjs"
44
+ },
45
+ "./request": {
46
+ "types": "./src/request.ts",
47
+ "default": "./dist/request.mjs"
48
+ },
49
+ "./plugin": {
50
+ "types": "./src/plugin.ts",
51
+ "default": "./dist/plugin.mjs"
52
+ }
53
+ },
54
+ "dependencies": {
55
+ "server-only": "^0.0.1"
56
+ },
57
+ "peerDependencies": {
58
+ "react": ">=19",
59
+ "next": ">=15",
60
+ "@fluentic/i18n": "0.1.0-beta.1"
61
+ },
62
+ "devDependencies": {
63
+ "react": "^19.2.0",
64
+ "@types/react": "^19.2.14",
65
+ "@types/node": "^22.15.0",
66
+ "next": "^16.0.0",
67
+ "tsdown": "^0.22.1",
68
+ "typescript": "^5.9.3",
69
+ "@fluentic/i18n": "0.1.0-beta.1"
70
+ },
71
+ "scripts": {
72
+ "build": "tsdown",
73
+ "typecheck": "tsc --noEmit"
74
+ }
6
75
  }
package/src/client.ts ADDED
@@ -0,0 +1,10 @@
1
+ export type { I18nSnapshot, ManagedI18n } from '@fluentic/i18n/adapter';
2
+ export {
3
+ createI18nManager,
4
+ defineTranslationLoader,
5
+ I18nManager,
6
+ I18nProvider,
7
+ I18nState,
8
+ useI18nRenderer,
9
+ useI18nRuntime,
10
+ } from '@fluentic/i18n/react';
package/src/config.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { Formats, I18nManagerOptions, TranslationData, TranslationLoader } from '@fluentic/i18n/adapter';
2
+ export type RequestConfig = I18nManagerOptions & {
3
+ translationLoader?: TranslationLoader;
4
+ translations?: TranslationData;
5
+ formats?: Partial<Formats>;
6
+ };
7
+ export type RequestConfigFactory = () => RequestConfig | Promise<RequestConfig>;
8
+ export function defineRequestConfig<T extends RequestConfigFactory>(factory: T): T {
9
+ return factory;
10
+ }
package/src/plugin.ts ADDED
@@ -0,0 +1,40 @@
1
+ import FluenticPlugin from '@fluentic/i18n/plugin/webpack';
2
+ import { createRequire } from 'node:module';
3
+ import { relative, resolve } from 'node:path';
4
+ const require = createRequire(import.meta.url);
5
+ export type NextPluginOptions = { requestConfig: string; config?: string; compiled?: boolean; };
6
+ /** One Next integration; preserves the application's existing webpack hook/rules. */
7
+ export default function withFluentic(options: NextPluginOptions) {
8
+ const request = resolve(options.requestConfig);
9
+ const loader = require.resolve('@fluentic/i18n/plugin/webpack-loader');
10
+ const compilerOptions = {
11
+ ...(options.config ? { config: resolve(options.config) } : {}),
12
+ ...(options.compiled !== undefined ? { compiled: options.compiled } : {}),
13
+ };
14
+ return (nextConfig: any = {}) => ({
15
+ ...nextConfig,
16
+ turbopack: {
17
+ ...nextConfig.turbopack,
18
+ resolveAlias: {
19
+ ...nextConfig.turbopack?.resolveAlias,
20
+ '@fluentic/i18n-nextjs/request': './' + relative(process.cwd(), request).replace(/\\/g, '/'),
21
+ },
22
+ rules: {
23
+ ...nextConfig.turbopack?.rules,
24
+ ...Object.fromEntries(['*.js', '*.jsx', '*.ts', '*.tsx', '*.mjs', '*.mts'].map((pattern) => [pattern, [
25
+ ...(nextConfig.turbopack?.rules?.[pattern] ? [nextConfig.turbopack.rules[pattern]].flat() : []),
26
+ { condition: { not: 'foreign' }, loaders: [{ loader, options: compilerOptions }] },
27
+ ]])),
28
+ '*.jsonl': { loaders: [{ loader, options: compilerOptions }], as: '*.js' },
29
+ },
30
+ },
31
+ webpack(config: any, context: any) {
32
+ config.resolve ??= {};
33
+ config.resolve.alias ??= {};
34
+ config.resolve.alias['@fluentic/i18n-nextjs/request$'] = request;
35
+ config.plugins ??= [];
36
+ config.plugins.push(new FluenticPlugin(compilerOptions));
37
+ return nextConfig.webpack ? nextConfig.webpack(config, context) : config;
38
+ },
39
+ });
40
+ }
package/src/request.ts ADDED
@@ -0,0 +1,5 @@
1
+ import type { RequestConfigFactory } from './config';
2
+ const missing: RequestConfigFactory = () => {
3
+ throw new Error('[i18n] configure @fluentic/i18n-nextjs/plugin with requestConfig pointing to your request module');
4
+ };
5
+ export default missing;
package/src/server.ts ADDED
@@ -0,0 +1,87 @@
1
+ import 'server-only';
2
+ import requestConfig from '@fluentic/i18n-nextjs/request';
3
+ import {
4
+ createI18nManager,
5
+ type I18nSnapshot,
6
+ type ManagedI18n,
7
+ type PartsRenderer,
8
+ setRuntimeResolver,
9
+ withRuntime,
10
+ } from '@fluentic/i18n/adapter';
11
+ import { AsyncLocalStorage } from 'node:async_hooks';
12
+ import React, { cache } from 'react';
13
+ import { I18nProvider as ClientProvider } from './client';
14
+ import type { RequestConfig } from './config';
15
+ export { defineRequestConfig } from './config';
16
+ export type { RequestConfig, RequestConfigFactory } from './config';
17
+ const explicitScope = new AsyncLocalStorage<ManagedI18n>();
18
+ function createServerManaged() {
19
+ const managed = createI18nManager();
20
+ managed.runtime.jsxHandler = (_options, render) => React.createElement(ServerMessage, { render });
21
+ managed.runtime.tagElement = (element) => {
22
+ if (!React.isValidElement(element)) throw new TypeError('[i18n] t.tag expects a React element or handler');
23
+ return (chunks) => React.cloneElement(element, undefined, ...chunks as React.ReactNode[]);
24
+ };
25
+ return managed;
26
+ }
27
+ const getManaged = cache(createServerManaged);
28
+ setRuntimeResolver(() => (explicitScope.getStore() ?? getManaged()).runtime);
29
+ async function initialize(managed: ManagedI18n, config: RequestConfig) {
30
+ managed.manager.configure(config);
31
+ if (config.formats) managed.runtime.formats = config.formats;
32
+ managed.manager.setTranslationLoader(
33
+ config.translationLoader ?? (({ languageId }) => languageId === config.languageId ? config.translations ?? [] : []),
34
+ );
35
+ await managed.manager.init();
36
+ return managed;
37
+ }
38
+ const getRequestManaged = cache(async () => initialize(getManaged(), await requestConfig()));
39
+ /** Await in the execution path that uses synchronous t.text; layouts do not order siblings. */
40
+ export async function ready(): Promise<I18nSnapshot> {
41
+ const current = explicitScope.getStore() ?? await getRequestManaged();
42
+ return current.manager.getSnapshot();
43
+ }
44
+ export async function resolveI18n(config: RequestConfig): Promise<I18nSnapshot> {
45
+ return (await initialize(createServerManaged(), config)).manager.getSnapshot();
46
+ }
47
+ /** Route handlers, server actions: isolate the entire async operation explicitly. */
48
+ export async function runWithI18n<T>(config: RequestConfig, fn: () => T | Promise<T>): Promise<T> {
49
+ const managed = await initialize(createServerManaged(), config);
50
+ return explicitScope.run(managed, fn);
51
+ }
52
+ function ServerMessage({ render }: { render: PartsRenderer; }) {
53
+ const runtime = (explicitScope.getStore() ?? getManaged()).runtime;
54
+ // t.jsx is synchronous. The caller/provider must initialize before actual rendering.
55
+ return React.createElement(
56
+ React.Fragment,
57
+ null,
58
+ ...render(runtime).map((part, index) =>
59
+ React.createElement(React.Fragment, { key: index }, part as React.ReactNode)
60
+ ),
61
+ );
62
+ }
63
+ export async function I18nProvider({ children, snapshot }: { children?: React.ReactNode; snapshot?: I18nSnapshot; }) {
64
+ return React.createElement(ClientProvider, { snapshot: snapshot ?? await ready() }, children);
65
+ }
66
+ // Resolve at invocation time, never capture a request in a module-level singleton.
67
+ export const I18nManager: ManagedI18n['manager'] = new Proxy({} as ManagedI18n['manager'], {
68
+ get(_target, key: keyof ManagedI18n['manager']) {
69
+ return (...args: unknown[]) => {
70
+ const manager = (explicitScope.getStore() ?? getManaged()).manager;
71
+ return Reflect.apply(manager[key], manager, args);
72
+ };
73
+ },
74
+ });
75
+ export const I18nState = {
76
+ get: () => (explicitScope.getStore() ?? getManaged()).state.get(),
77
+ subscribe: (_listener: () => void) => () => {},
78
+ use: () => (explicitScope.getStore() ?? getManaged()).state.get(),
79
+ };
80
+ export function useI18nRuntime() {
81
+ return (explicitScope.getStore() ?? getManaged()).runtime;
82
+ }
83
+ export function useI18nRenderer<F extends (...args: any[]) => any>(renderer: F): F {
84
+ const runtime = useI18nRuntime();
85
+ return ((...args: Parameters<F>) => withRuntime(runtime, () => renderer(...args))) as F;
86
+ }
87
+ export { createI18nManager, defineTranslationLoader } from '@fluentic/i18n/adapter';
@@ -0,0 +1,2 @@
1
+ throw new Error('[i18n] @fluentic/i18n-nextjs/server is only available on the server');
2
+ export {};