@nckdv/translation-nextjs 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +145 -0
- package/dist/index.cjs +19 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -0
- package/dist/server.cjs +69 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.cts +39 -0
- package/dist/server.d.ts +39 -0
- package/dist/server.js +41 -0
- package/dist/server.js.map +1 -0
- package/package.json +59 -0
package/README.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# `@nckdv/translation-nextjs`
|
|
2
|
+
|
|
3
|
+
Server-side Next.js bindings for nck-translation. This package configures the
|
|
4
|
+
SDK from server-only environment variables and exposes catalog and translator
|
|
5
|
+
helpers for Server Components.
|
|
6
|
+
|
|
7
|
+
React context and hooks live in the framework-neutral
|
|
8
|
+
[`@nckdv/translation-react`](../react) package. Keeping the runtime entries
|
|
9
|
+
separate makes the credential boundary visible in every import:
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
@nckdv/translation-nextjs/server <- server only; reads the project API key
|
|
13
|
+
@nckdv/translation-react <- client safe; receives plain catalogs
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @nckdv/translation-nextjs
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The Next.js package installs `@nckdv/translation-react` with it. Import the
|
|
23
|
+
React bindings directly from that package rather than through a Next.js client
|
|
24
|
+
entry.
|
|
25
|
+
|
|
26
|
+
## Configure
|
|
27
|
+
|
|
28
|
+
The server helper reads two environment variables:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
NCK_TRANSLATION_API_URL=https://nck-translation.example.com
|
|
32
|
+
NCK_TRANSLATION_API_KEY=nck_... # server-side only
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The key is a bearer credential for every catalog in the project. Never expose
|
|
36
|
+
it through a `NEXT_PUBLIC_*` variable or pass it to a Client Component.
|
|
37
|
+
|
|
38
|
+
Configuration can instead be supplied once in code, which is also useful in
|
|
39
|
+
tests:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { configureTranslations } from "@nckdv/translation-nextjs/server";
|
|
43
|
+
|
|
44
|
+
configureTranslations({
|
|
45
|
+
apiUrl: "https://nck-translation.example.com",
|
|
46
|
+
apiKey: process.env.NCK_TRANSLATION_API_KEY!,
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Server Components
|
|
51
|
+
|
|
52
|
+
`getTranslator(language)` returns a translator bound to one language:
|
|
53
|
+
|
|
54
|
+
```tsx
|
|
55
|
+
import { getTranslator } from "@nckdv/translation-nextjs/server";
|
|
56
|
+
|
|
57
|
+
export default async function Page({
|
|
58
|
+
params,
|
|
59
|
+
}: {
|
|
60
|
+
params: Promise<{ lang: string }>;
|
|
61
|
+
}) {
|
|
62
|
+
const { lang } = await params;
|
|
63
|
+
const t = await getTranslator(lang);
|
|
64
|
+
return <h1>{t("home.title")}</h1>;
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The SDK caches successful catalogs and shares concurrent requests for the same
|
|
69
|
+
language. A failed request is not cached.
|
|
70
|
+
|
|
71
|
+
Use `getCatalog(language)` when the client tree needs the raw serializable map:
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
// app/[lang]/layout.tsx -- Server Component
|
|
75
|
+
import { getCatalog } from "@nckdv/translation-nextjs/server";
|
|
76
|
+
import { Providers } from "./providers";
|
|
77
|
+
|
|
78
|
+
export default async function Layout({ params, children }: LayoutProps) {
|
|
79
|
+
const { lang } = await params;
|
|
80
|
+
const catalog = await getCatalog(lang);
|
|
81
|
+
return (
|
|
82
|
+
<Providers language={lang} catalog={catalog}>
|
|
83
|
+
{children}
|
|
84
|
+
</Providers>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
```tsx
|
|
90
|
+
// app/[lang]/providers.tsx -- Client Component
|
|
91
|
+
"use client";
|
|
92
|
+
|
|
93
|
+
import {
|
|
94
|
+
TranslationProvider,
|
|
95
|
+
type TranslationMap,
|
|
96
|
+
} from "@nckdv/translation-react";
|
|
97
|
+
|
|
98
|
+
export function Providers({
|
|
99
|
+
language,
|
|
100
|
+
catalog,
|
|
101
|
+
children,
|
|
102
|
+
}: {
|
|
103
|
+
language: string;
|
|
104
|
+
catalog: TranslationMap;
|
|
105
|
+
children: React.ReactNode;
|
|
106
|
+
}) {
|
|
107
|
+
return (
|
|
108
|
+
<TranslationProvider language={language} catalog={catalog}>
|
|
109
|
+
{children}
|
|
110
|
+
</TranslationProvider>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Client Components beneath that provider call `useTranslations()` or
|
|
116
|
+
`useLanguage()` from `@nckdv/translation-react`.
|
|
117
|
+
|
|
118
|
+
After an edit lands, invalidate the SDK cache so the next read goes to the
|
|
119
|
+
platform:
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
import { invalidate } from "@nckdv/translation-nextjs/server";
|
|
123
|
+
|
|
124
|
+
invalidate("de"); // or invalidate() for every language
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## API
|
|
128
|
+
|
|
129
|
+
### `@nckdv/translation-nextjs/server`
|
|
130
|
+
|
|
131
|
+
| export | signature |
|
|
132
|
+
| --- | --- |
|
|
133
|
+
| `getTranslator` | `(language: string) => Promise<Translate>` |
|
|
134
|
+
| `getCatalog` | `(language: string) => Promise<Catalog>` |
|
|
135
|
+
| `invalidate` | `(language?: string) => void` |
|
|
136
|
+
| `configureTranslations` | `({ apiUrl, apiKey, cacheMs? }) => void` |
|
|
137
|
+
| `ENV_API_URL` / `ENV_API_KEY` | `string` |
|
|
138
|
+
|
|
139
|
+
Missing configuration throws at the first helper call. The server entry imports
|
|
140
|
+
`server-only`, so importing it from a Client Component fails the Next.js build.
|
|
141
|
+
|
|
142
|
+
### `@nckdv/translation-nextjs`
|
|
143
|
+
|
|
144
|
+
Types only: `Catalog` and `Translate`. Importing the root cannot initialize the
|
|
145
|
+
client or read environment variables.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
|
|
16
|
+
// src/index.ts
|
|
17
|
+
var index_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(index_exports);
|
|
19
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Shared, runtime-agnostic surface for `@nckdv/translation-nextjs`.\n *\n * Only types live here, so importing the package root does not pull in the\n * server code that reads the API key. Import server and React behavior from\n * their explicit runtime entries:\n *\n * ```ts\n * import { getTranslator } from \"@nckdv/translation-nextjs/server\";\n * import { TranslationProvider } from \"@nckdv/translation-react\";\n * ```\n */\nexport type { Catalog, Translate } from \"@nckdv/translation-sdk\";\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Catalog, Translate } from '@nckdv/translation-sdk';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Catalog, Translate } from '@nckdv/translation-sdk';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/server.cjs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/server.ts
|
|
21
|
+
var server_exports = {};
|
|
22
|
+
__export(server_exports, {
|
|
23
|
+
ENV_API_KEY: () => ENV_API_KEY,
|
|
24
|
+
ENV_API_URL: () => ENV_API_URL,
|
|
25
|
+
configureTranslations: () => configureTranslations,
|
|
26
|
+
getCatalog: () => getCatalog,
|
|
27
|
+
getTranslator: () => getTranslator,
|
|
28
|
+
invalidate: () => invalidate
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(server_exports);
|
|
31
|
+
var import_server_only = require("server-only");
|
|
32
|
+
var import_translation_sdk = require("@nckdv/translation-sdk");
|
|
33
|
+
var ENV_API_URL = "NCK_TRANSLATION_API_URL";
|
|
34
|
+
var ENV_API_KEY = "NCK_TRANSLATION_API_KEY";
|
|
35
|
+
var configured;
|
|
36
|
+
function configureTranslations(options) {
|
|
37
|
+
configured = (0, import_translation_sdk.createClient)(options);
|
|
38
|
+
}
|
|
39
|
+
function getClient() {
|
|
40
|
+
if (configured) return configured;
|
|
41
|
+
const apiUrl = process.env[ENV_API_URL];
|
|
42
|
+
const apiKey = process.env[ENV_API_KEY];
|
|
43
|
+
if (!apiUrl || !apiKey) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
`@nckdv/translation-nextjs: set ${ENV_API_URL} and ${ENV_API_KEY}, or call configureTranslations() before rendering.`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
configured = (0, import_translation_sdk.createClient)({ apiUrl, apiKey });
|
|
49
|
+
return configured;
|
|
50
|
+
}
|
|
51
|
+
function getCatalog(language) {
|
|
52
|
+
return getClient().getCatalog(language);
|
|
53
|
+
}
|
|
54
|
+
function getTranslator(language) {
|
|
55
|
+
return getClient().getTranslator(language);
|
|
56
|
+
}
|
|
57
|
+
function invalidate(language) {
|
|
58
|
+
getClient().invalidate(language);
|
|
59
|
+
}
|
|
60
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
61
|
+
0 && (module.exports = {
|
|
62
|
+
ENV_API_KEY,
|
|
63
|
+
ENV_API_URL,
|
|
64
|
+
configureTranslations,
|
|
65
|
+
getCatalog,
|
|
66
|
+
getTranslator,
|
|
67
|
+
invalidate
|
|
68
|
+
});
|
|
69
|
+
//# sourceMappingURL=server.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/server.ts"],"sourcesContent":["// Poisons this module for the client bundle: importing it from a Client\n// Component is a build error rather than a leaked API key.\nimport \"server-only\";\nimport {\n createClient,\n type Catalog,\n type Client,\n type Translate,\n} from \"@nckdv/translation-sdk\";\n\n/** Env vars the default client reads. Both are server-only secrets. */\nexport const ENV_API_URL = \"NCK_TRANSLATION_API_URL\";\nexport const ENV_API_KEY = \"NCK_TRANSLATION_API_KEY\";\n\nlet configured: Client | undefined;\n\n/**\n * Point the server helpers at a platform explicitly, instead of via env.\n *\n * Optional — call it once at startup if you would rather pass configuration in\n * code (or in a test). Without it, the first server read builds a client from\n * `NCK_TRANSLATION_API_URL` and `NCK_TRANSLATION_API_KEY`.\n */\nexport function configureTranslations(options: {\n apiUrl: string;\n apiKey: string;\n cacheMs?: number;\n}): void {\n configured = createClient(options);\n}\n\nfunction getClient(): Client {\n if (configured) return configured;\n\n const apiUrl = process.env[ENV_API_URL];\n const apiKey = process.env[ENV_API_KEY];\n if (!apiUrl || !apiKey) {\n throw new Error(\n `@nckdv/translation-nextjs: set ${ENV_API_URL} and ${ENV_API_KEY}, or call ` +\n `configureTranslations() before rendering.`,\n );\n }\n\n // Kept across requests so the SDK's TTL cache spans them. The SDK also\n // shares concurrent in-flight reads for the same language.\n configured = createClient({ apiUrl, apiKey });\n return configured;\n}\n\n/**\n * One language's catalog. The SDK caches successful catalogs and shares an\n * in-flight request, so concurrent Server Components make one network call.\n */\nexport function getCatalog(language: string): Promise<Catalog> {\n return getClient().getCatalog(language);\n}\n\n/**\n * A `t()` bound to `language`, for use in Server Components.\n *\n * ```tsx\n * const t = await getTranslator(lang);\n * return <h1>{t(\"home.title\")}</h1>;\n * ```\n */\nexport function getTranslator(language: string): Promise<Translate> {\n return getClient().getTranslator(language);\n}\n\n/**\n * Drop the cached catalog for a language (or all of them). Call after an edit\n * lands so the next render re-reads from the platform.\n */\nexport function invalidate(language?: string): void {\n getClient().invalidate(language);\n}\n\nexport type { Catalog, Translate } from \"@nckdv/translation-sdk\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,yBAAO;AACP,6BAKO;AAGA,IAAM,cAAc;AACpB,IAAM,cAAc;AAE3B,IAAI;AASG,SAAS,sBAAsB,SAI7B;AACP,mBAAa,qCAAa,OAAO;AACnC;AAEA,SAAS,YAAoB;AAC3B,MAAI,WAAY,QAAO;AAEvB,QAAM,SAAS,QAAQ,IAAI,WAAW;AACtC,QAAM,SAAS,QAAQ,IAAI,WAAW;AACtC,MAAI,CAAC,UAAU,CAAC,QAAQ;AACtB,UAAM,IAAI;AAAA,MACR,kCAAkC,WAAW,QAAQ,WAAW;AAAA,IAElE;AAAA,EACF;AAIA,mBAAa,qCAAa,EAAE,QAAQ,OAAO,CAAC;AAC5C,SAAO;AACT;AAMO,SAAS,WAAW,UAAoC;AAC7D,SAAO,UAAU,EAAE,WAAW,QAAQ;AACxC;AAUO,SAAS,cAAc,UAAsC;AAClE,SAAO,UAAU,EAAE,cAAc,QAAQ;AAC3C;AAMO,SAAS,WAAW,UAAyB;AAClD,YAAU,EAAE,WAAW,QAAQ;AACjC;","names":[]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Catalog, Translate } from '@nckdv/translation-sdk';
|
|
2
|
+
export { Catalog, Translate } from '@nckdv/translation-sdk';
|
|
3
|
+
|
|
4
|
+
/** Env vars the default client reads. Both are server-only secrets. */
|
|
5
|
+
declare const ENV_API_URL = "NCK_TRANSLATION_API_URL";
|
|
6
|
+
declare const ENV_API_KEY = "NCK_TRANSLATION_API_KEY";
|
|
7
|
+
/**
|
|
8
|
+
* Point the server helpers at a platform explicitly, instead of via env.
|
|
9
|
+
*
|
|
10
|
+
* Optional — call it once at startup if you would rather pass configuration in
|
|
11
|
+
* code (or in a test). Without it, the first server read builds a client from
|
|
12
|
+
* `NCK_TRANSLATION_API_URL` and `NCK_TRANSLATION_API_KEY`.
|
|
13
|
+
*/
|
|
14
|
+
declare function configureTranslations(options: {
|
|
15
|
+
apiUrl: string;
|
|
16
|
+
apiKey: string;
|
|
17
|
+
cacheMs?: number;
|
|
18
|
+
}): void;
|
|
19
|
+
/**
|
|
20
|
+
* One language's catalog. The SDK caches successful catalogs and shares an
|
|
21
|
+
* in-flight request, so concurrent Server Components make one network call.
|
|
22
|
+
*/
|
|
23
|
+
declare function getCatalog(language: string): Promise<Catalog>;
|
|
24
|
+
/**
|
|
25
|
+
* A `t()` bound to `language`, for use in Server Components.
|
|
26
|
+
*
|
|
27
|
+
* ```tsx
|
|
28
|
+
* const t = await getTranslator(lang);
|
|
29
|
+
* return <h1>{t("home.title")}</h1>;
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
declare function getTranslator(language: string): Promise<Translate>;
|
|
33
|
+
/**
|
|
34
|
+
* Drop the cached catalog for a language (or all of them). Call after an edit
|
|
35
|
+
* lands so the next render re-reads from the platform.
|
|
36
|
+
*/
|
|
37
|
+
declare function invalidate(language?: string): void;
|
|
38
|
+
|
|
39
|
+
export { ENV_API_KEY, ENV_API_URL, configureTranslations, getCatalog, getTranslator, invalidate };
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Catalog, Translate } from '@nckdv/translation-sdk';
|
|
2
|
+
export { Catalog, Translate } from '@nckdv/translation-sdk';
|
|
3
|
+
|
|
4
|
+
/** Env vars the default client reads. Both are server-only secrets. */
|
|
5
|
+
declare const ENV_API_URL = "NCK_TRANSLATION_API_URL";
|
|
6
|
+
declare const ENV_API_KEY = "NCK_TRANSLATION_API_KEY";
|
|
7
|
+
/**
|
|
8
|
+
* Point the server helpers at a platform explicitly, instead of via env.
|
|
9
|
+
*
|
|
10
|
+
* Optional — call it once at startup if you would rather pass configuration in
|
|
11
|
+
* code (or in a test). Without it, the first server read builds a client from
|
|
12
|
+
* `NCK_TRANSLATION_API_URL` and `NCK_TRANSLATION_API_KEY`.
|
|
13
|
+
*/
|
|
14
|
+
declare function configureTranslations(options: {
|
|
15
|
+
apiUrl: string;
|
|
16
|
+
apiKey: string;
|
|
17
|
+
cacheMs?: number;
|
|
18
|
+
}): void;
|
|
19
|
+
/**
|
|
20
|
+
* One language's catalog. The SDK caches successful catalogs and shares an
|
|
21
|
+
* in-flight request, so concurrent Server Components make one network call.
|
|
22
|
+
*/
|
|
23
|
+
declare function getCatalog(language: string): Promise<Catalog>;
|
|
24
|
+
/**
|
|
25
|
+
* A `t()` bound to `language`, for use in Server Components.
|
|
26
|
+
*
|
|
27
|
+
* ```tsx
|
|
28
|
+
* const t = await getTranslator(lang);
|
|
29
|
+
* return <h1>{t("home.title")}</h1>;
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
declare function getTranslator(language: string): Promise<Translate>;
|
|
33
|
+
/**
|
|
34
|
+
* Drop the cached catalog for a language (or all of them). Call after an edit
|
|
35
|
+
* lands so the next render re-reads from the platform.
|
|
36
|
+
*/
|
|
37
|
+
declare function invalidate(language?: string): void;
|
|
38
|
+
|
|
39
|
+
export { ENV_API_KEY, ENV_API_URL, configureTranslations, getCatalog, getTranslator, invalidate };
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// src/server.ts
|
|
2
|
+
import "server-only";
|
|
3
|
+
import {
|
|
4
|
+
createClient
|
|
5
|
+
} from "@nckdv/translation-sdk";
|
|
6
|
+
var ENV_API_URL = "NCK_TRANSLATION_API_URL";
|
|
7
|
+
var ENV_API_KEY = "NCK_TRANSLATION_API_KEY";
|
|
8
|
+
var configured;
|
|
9
|
+
function configureTranslations(options) {
|
|
10
|
+
configured = createClient(options);
|
|
11
|
+
}
|
|
12
|
+
function getClient() {
|
|
13
|
+
if (configured) return configured;
|
|
14
|
+
const apiUrl = process.env[ENV_API_URL];
|
|
15
|
+
const apiKey = process.env[ENV_API_KEY];
|
|
16
|
+
if (!apiUrl || !apiKey) {
|
|
17
|
+
throw new Error(
|
|
18
|
+
`@nckdv/translation-nextjs: set ${ENV_API_URL} and ${ENV_API_KEY}, or call configureTranslations() before rendering.`
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
configured = createClient({ apiUrl, apiKey });
|
|
22
|
+
return configured;
|
|
23
|
+
}
|
|
24
|
+
function getCatalog(language) {
|
|
25
|
+
return getClient().getCatalog(language);
|
|
26
|
+
}
|
|
27
|
+
function getTranslator(language) {
|
|
28
|
+
return getClient().getTranslator(language);
|
|
29
|
+
}
|
|
30
|
+
function invalidate(language) {
|
|
31
|
+
getClient().invalidate(language);
|
|
32
|
+
}
|
|
33
|
+
export {
|
|
34
|
+
ENV_API_KEY,
|
|
35
|
+
ENV_API_URL,
|
|
36
|
+
configureTranslations,
|
|
37
|
+
getCatalog,
|
|
38
|
+
getTranslator,
|
|
39
|
+
invalidate
|
|
40
|
+
};
|
|
41
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/server.ts"],"sourcesContent":["// Poisons this module for the client bundle: importing it from a Client\n// Component is a build error rather than a leaked API key.\nimport \"server-only\";\nimport {\n createClient,\n type Catalog,\n type Client,\n type Translate,\n} from \"@nckdv/translation-sdk\";\n\n/** Env vars the default client reads. Both are server-only secrets. */\nexport const ENV_API_URL = \"NCK_TRANSLATION_API_URL\";\nexport const ENV_API_KEY = \"NCK_TRANSLATION_API_KEY\";\n\nlet configured: Client | undefined;\n\n/**\n * Point the server helpers at a platform explicitly, instead of via env.\n *\n * Optional — call it once at startup if you would rather pass configuration in\n * code (or in a test). Without it, the first server read builds a client from\n * `NCK_TRANSLATION_API_URL` and `NCK_TRANSLATION_API_KEY`.\n */\nexport function configureTranslations(options: {\n apiUrl: string;\n apiKey: string;\n cacheMs?: number;\n}): void {\n configured = createClient(options);\n}\n\nfunction getClient(): Client {\n if (configured) return configured;\n\n const apiUrl = process.env[ENV_API_URL];\n const apiKey = process.env[ENV_API_KEY];\n if (!apiUrl || !apiKey) {\n throw new Error(\n `@nckdv/translation-nextjs: set ${ENV_API_URL} and ${ENV_API_KEY}, or call ` +\n `configureTranslations() before rendering.`,\n );\n }\n\n // Kept across requests so the SDK's TTL cache spans them. The SDK also\n // shares concurrent in-flight reads for the same language.\n configured = createClient({ apiUrl, apiKey });\n return configured;\n}\n\n/**\n * One language's catalog. The SDK caches successful catalogs and shares an\n * in-flight request, so concurrent Server Components make one network call.\n */\nexport function getCatalog(language: string): Promise<Catalog> {\n return getClient().getCatalog(language);\n}\n\n/**\n * A `t()` bound to `language`, for use in Server Components.\n *\n * ```tsx\n * const t = await getTranslator(lang);\n * return <h1>{t(\"home.title\")}</h1>;\n * ```\n */\nexport function getTranslator(language: string): Promise<Translate> {\n return getClient().getTranslator(language);\n}\n\n/**\n * Drop the cached catalog for a language (or all of them). Call after an edit\n * lands so the next render re-reads from the platform.\n */\nexport function invalidate(language?: string): void {\n getClient().invalidate(language);\n}\n\nexport type { Catalog, Translate } from \"@nckdv/translation-sdk\";\n"],"mappings":";AAEA,OAAO;AACP;AAAA,EACE;AAAA,OAIK;AAGA,IAAM,cAAc;AACpB,IAAM,cAAc;AAE3B,IAAI;AASG,SAAS,sBAAsB,SAI7B;AACP,eAAa,aAAa,OAAO;AACnC;AAEA,SAAS,YAAoB;AAC3B,MAAI,WAAY,QAAO;AAEvB,QAAM,SAAS,QAAQ,IAAI,WAAW;AACtC,QAAM,SAAS,QAAQ,IAAI,WAAW;AACtC,MAAI,CAAC,UAAU,CAAC,QAAQ;AACtB,UAAM,IAAI;AAAA,MACR,kCAAkC,WAAW,QAAQ,WAAW;AAAA,IAElE;AAAA,EACF;AAIA,eAAa,aAAa,EAAE,QAAQ,OAAO,CAAC;AAC5C,SAAO;AACT;AAMO,SAAS,WAAW,UAAoC;AAC7D,SAAO,UAAU,EAAE,WAAW,QAAQ;AACxC;AAUO,SAAS,cAAc,UAAsC;AAClE,SAAO,UAAU,EAAE,cAAc,QAAQ;AAC3C;AAMO,SAAS,WAAW,UAAyB;AAClD,YAAU,EAAE,WAAW,QAAQ;AACjC;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nckdv/translation-nextjs",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Server-side Next.js bindings for nck-translation catalogs.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.cjs"
|
|
12
|
+
},
|
|
13
|
+
"./server": {
|
|
14
|
+
"types": "./dist/server.d.ts",
|
|
15
|
+
"import": "./dist/server.js",
|
|
16
|
+
"require": "./dist/server.cjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"main": "./dist/index.cjs",
|
|
20
|
+
"module": "./dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsup",
|
|
27
|
+
"dev": "tsup --watch",
|
|
28
|
+
"typecheck": "tsc --noEmit"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"next": "^14.2.0 || ^15.0.0 || ^16.0.0"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@nckdv/translation-react": "^0.2.0",
|
|
35
|
+
"@nckdv/translation-sdk": "^0.2.0",
|
|
36
|
+
"server-only": "^0.0.1"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"next": "^16.0.0",
|
|
40
|
+
"tsup": "^8.3.5",
|
|
41
|
+
"typescript": "^5.7.2"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/NiklasErath/nck-translation.git",
|
|
49
|
+
"directory": "packages/nextjs"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://github.com/NiklasErath/nck-translation#readme",
|
|
52
|
+
"keywords": [
|
|
53
|
+
"i18n",
|
|
54
|
+
"translation",
|
|
55
|
+
"nextjs",
|
|
56
|
+
"internationalization",
|
|
57
|
+
"localization"
|
|
58
|
+
]
|
|
59
|
+
}
|