payload-intl 0.0.7 → 0.1.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 +25 -27
- package/dist/requests/fetchMessages.js +7 -9
- package/dist/requests/fetchMessages.js.map +1 -1
- package/package.json +14 -1
package/README.md
CHANGED
|
@@ -148,24 +148,6 @@ export default {
|
|
|
148
148
|
|
|
149
149
|
Here's a complete example showing how to integrate payload-intl using next-intl and S3 storage:
|
|
150
150
|
|
|
151
|
-
```typescript
|
|
152
|
-
// i18n/messages.ts
|
|
153
|
-
export const messages = {
|
|
154
|
-
UserProfile: {
|
|
155
|
-
title: "Hello {firstName}",
|
|
156
|
-
description:
|
|
157
|
-
"Welcome back, {firstName}! You have {count, plural, =0 {no messages} one {# message} other {# messages}}.",
|
|
158
|
-
},
|
|
159
|
-
Navigation: {
|
|
160
|
-
home: "Home",
|
|
161
|
-
about: "About",
|
|
162
|
-
},
|
|
163
|
-
richText: {
|
|
164
|
-
welcome: "$RICH$",
|
|
165
|
-
},
|
|
166
|
-
} as const;
|
|
167
|
-
```
|
|
168
|
-
|
|
169
151
|
```typescript
|
|
170
152
|
// payload.config.ts
|
|
171
153
|
import { s3Storage } from "@payloadcms/storage-s3";
|
|
@@ -183,7 +165,6 @@ export default buildConfig({
|
|
|
183
165
|
plugins: [
|
|
184
166
|
intlPlugin({
|
|
185
167
|
schema: messages,
|
|
186
|
-
tabs: true,
|
|
187
168
|
hooks: {
|
|
188
169
|
afterUpdate: () => revalidateTag("messages"),
|
|
189
170
|
},
|
|
@@ -191,7 +172,10 @@ export default buildConfig({
|
|
|
191
172
|
s3Storage({
|
|
192
173
|
collections: {
|
|
193
174
|
messages: {
|
|
194
|
-
prefix: "messages",
|
|
175
|
+
prefix: "messages", // or anything else you want
|
|
176
|
+
// generate a publicly available URL to the file
|
|
177
|
+
generateFileURL: async ({ filename, prefix }) =>
|
|
178
|
+
`.../${prefix}/${filename}`,
|
|
195
179
|
},
|
|
196
180
|
},
|
|
197
181
|
}),
|
|
@@ -199,21 +183,30 @@ export default buildConfig({
|
|
|
199
183
|
});
|
|
200
184
|
```
|
|
201
185
|
|
|
202
|
-
|
|
186
|
+
```typescript
|
|
187
|
+
// i18n/messages.ts
|
|
188
|
+
export const messages = {
|
|
189
|
+
UserProfile: {
|
|
190
|
+
title: "Hello {firstName}",
|
|
191
|
+
description:
|
|
192
|
+
"Welcome back, {firstName}! You have {count, plural, =0 {no messages} one {# message} other {# messages}}.",
|
|
193
|
+
},
|
|
194
|
+
// ...
|
|
195
|
+
} as const;
|
|
196
|
+
```
|
|
203
197
|
|
|
204
198
|
```typescript
|
|
205
199
|
// i18n/global.ts
|
|
206
|
-
import type
|
|
200
|
+
import type messages from "./messages";
|
|
207
201
|
|
|
208
202
|
declare module "next-intl" {
|
|
209
203
|
interface AppConfig {
|
|
210
204
|
Messages: typeof messages;
|
|
205
|
+
// ...
|
|
211
206
|
}
|
|
212
207
|
}
|
|
213
208
|
```
|
|
214
209
|
|
|
215
|
-
### 4. Configure next-intl request handler
|
|
216
|
-
|
|
217
210
|
```typescript
|
|
218
211
|
// i18n/request.ts
|
|
219
212
|
import { getRequestConfig } from "next-intl/server";
|
|
@@ -230,10 +223,8 @@ export default getRequestConfig(async ({ locale }) => {
|
|
|
230
223
|
});
|
|
231
224
|
```
|
|
232
225
|
|
|
233
|
-
### 5. Create server-side message fetcher
|
|
234
|
-
|
|
235
226
|
```typescript
|
|
236
|
-
// server
|
|
227
|
+
// server.ts
|
|
237
228
|
"use server";
|
|
238
229
|
|
|
239
230
|
import config from "@payload-config";
|
|
@@ -243,8 +234,15 @@ import { fetchMessages } from "payload-intl";
|
|
|
243
234
|
|
|
244
235
|
export const fetchCachedMessages = unstable_cache(
|
|
245
236
|
async (locale: string) => {
|
|
237
|
+
// Node.js
|
|
246
238
|
const payload = await getPayload({ config });
|
|
247
239
|
return await fetchMessages(payload, locale);
|
|
240
|
+
|
|
241
|
+
// Edge runtime
|
|
242
|
+
const response = await fetch(
|
|
243
|
+
`${process.env.PAYLOAD_API_URL}/intl-plugin/en`,
|
|
244
|
+
);
|
|
245
|
+
return await response.json();
|
|
248
246
|
},
|
|
249
247
|
["messages"],
|
|
250
248
|
{
|
|
@@ -1,29 +1,27 @@
|
|
|
1
1
|
import { getPluginContext as s } from "../utils/config.js";
|
|
2
|
-
async function a(
|
|
2
|
+
async function a(n, e) {
|
|
3
3
|
const {
|
|
4
4
|
docs: [r]
|
|
5
|
-
} = await
|
|
6
|
-
collection: s(
|
|
5
|
+
} = await n.find({
|
|
6
|
+
collection: s(n.config).collectionSlug,
|
|
7
7
|
where: { locale: { equals: e } }
|
|
8
8
|
});
|
|
9
9
|
if (!r)
|
|
10
10
|
return console.warn(`No messages found for locale ${e}`), {};
|
|
11
|
-
const { url: o } = r,
|
|
12
|
-
|
|
13
|
-
});
|
|
14
|
-
if (!n.ok)
|
|
11
|
+
const { url: o } = r, t = await fetch(o);
|
|
12
|
+
if (!t.ok)
|
|
15
13
|
throw new Error(
|
|
16
14
|
`Could not fetch messages for locale "${e}": The page returned an error.
|
|
17
15
|
|
|
18
16
|
${o}`
|
|
19
17
|
);
|
|
20
|
-
if (
|
|
18
|
+
if (t.headers.get("content-type") !== "application/json")
|
|
21
19
|
throw new Error(
|
|
22
20
|
`Could not fetch messages for locale "${e}": The page did not return a JSON file.
|
|
23
21
|
|
|
24
22
|
${o}`
|
|
25
23
|
);
|
|
26
|
-
return await
|
|
24
|
+
return await t.json();
|
|
27
25
|
}
|
|
28
26
|
export {
|
|
29
27
|
a as fetchMessages
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchMessages.js","sources":["../../src/requests/fetchMessages.ts"],"sourcesContent":["import type { BasePayload } from \"payload\";\n\nimport { getPluginContext } from \"@/utils/config\";\n\nimport type { Messages } from \"../types\";\n\nexport async function fetchMessages(\n payload: BasePayload,\n locale: string,\n): Promise<Messages> {\n const {\n docs: [doc],\n } = await payload.find({\n collection: getPluginContext(payload.config).collectionSlug,\n where: { locale: { equals: locale } },\n });\n\n if (!doc) {\n console.warn(`No messages found for locale ${locale}`);\n return {};\n }\n\n const { url } = doc as unknown as { url: string };\n\n const response = await fetch(url
|
|
1
|
+
{"version":3,"file":"fetchMessages.js","sources":["../../src/requests/fetchMessages.ts"],"sourcesContent":["import type { BasePayload } from \"payload\";\n\nimport { getPluginContext } from \"@/utils/config\";\n\nimport type { Messages } from \"../types\";\n\nexport async function fetchMessages(\n payload: BasePayload,\n locale: string,\n): Promise<Messages> {\n const {\n docs: [doc],\n } = await payload.find({\n collection: getPluginContext(payload.config).collectionSlug,\n where: { locale: { equals: locale } },\n });\n\n if (!doc) {\n console.warn(`No messages found for locale ${locale}`);\n return {};\n }\n\n const { url } = doc as unknown as { url: string };\n\n const response = await fetch(url);\n\n if (!response.ok) {\n throw new Error(\n `Could not fetch messages for locale \"${locale}\": The page returned an error.\\n\\n${url}`,\n );\n }\n\n if (response.headers.get(\"content-type\") !== \"application/json\") {\n throw new Error(\n `Could not fetch messages for locale \"${locale}\": The page did not return a JSON file.\\n\\n${url}`,\n );\n }\n\n return await response.json();\n}\n"],"names":["fetchMessages","payload","locale","doc","getPluginContext","url","response"],"mappings":";AAMA,eAAsBA,EACpBC,GACAC,GACmB;AACnB,QAAM;AAAA,IACJ,MAAM,CAACC,CAAG;AAAA,EAAA,IACR,MAAMF,EAAQ,KAAK;AAAA,IACrB,YAAYG,EAAiBH,EAAQ,MAAM,EAAE;AAAA,IAC7C,OAAO,EAAE,QAAQ,EAAE,QAAQC,IAAO;AAAA,EAAE,CACrC;AAED,MAAI,CAACC;AACH,mBAAQ,KAAK,gCAAgCD,CAAM,EAAE,GAC9C,CAAA;AAGT,QAAM,EAAE,KAAAG,MAAQF,GAEVG,IAAW,MAAM,MAAMD,CAAG;AAEhC,MAAI,CAACC,EAAS;AACZ,UAAM,IAAI;AAAA,MACR,wCAAwCJ,CAAM;AAAA;AAAA,EAAqCG,CAAG;AAAA,IAAA;AAI1F,MAAIC,EAAS,QAAQ,IAAI,cAAc,MAAM;AAC3C,UAAM,IAAI;AAAA,MACR,wCAAwCJ,CAAM;AAAA;AAAA,EAA8CG,CAAG;AAAA,IAAA;AAInG,SAAO,MAAMC,EAAS,KAAA;AACxB;"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "payload-intl",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Payload Plugin for I18N using ICU Messages",
|
|
5
5
|
"license": "MIT",
|
|
6
|
+
"author": "Michael Zeltner",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"payload",
|
|
9
|
+
"intl",
|
|
10
|
+
"i18n",
|
|
11
|
+
"internationalization",
|
|
12
|
+
"localization",
|
|
13
|
+
"react",
|
|
14
|
+
"next.js",
|
|
15
|
+
"translation",
|
|
16
|
+
"translations",
|
|
17
|
+
"icu"
|
|
18
|
+
],
|
|
6
19
|
"type": "module",
|
|
7
20
|
"exports": {
|
|
8
21
|
".": {
|