@better-intl/next 0.1.3 → 0.2.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/dist/{chunk-4EXOYGAS.js → chunk-Q7QSMIA4.js} +2 -9
- package/dist/chunk-Q7QSMIA4.js.map +1 -0
- package/dist/index.cjs +0 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -1
- package/dist/plugin.cjs +0 -7
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.d.cts +0 -1
- package/dist/plugin.d.ts +0 -1
- package/dist/plugin.js +1 -1
- package/dist/rsc.cjs +33 -0
- package/dist/rsc.cjs.map +1 -0
- package/dist/rsc.d.cts +45 -0
- package/dist/rsc.d.ts +45 -0
- package/dist/rsc.js +29 -0
- package/dist/rsc.js.map +1 -0
- package/package.json +6 -1
- package/dist/chunk-4EXOYGAS.js.map +0 -1
|
@@ -7,18 +7,11 @@ function withBetterIntl(intlConfig) {
|
|
|
7
7
|
...nextConfig.env,
|
|
8
8
|
BETTER_INTL_LOCALES: JSON.stringify(intlConfig.locales),
|
|
9
9
|
BETTER_INTL_DEFAULT_LOCALE: intlConfig.defaultLocale
|
|
10
|
-
},
|
|
11
|
-
webpack(config, options) {
|
|
12
|
-
const existingWebpack = nextConfig.webpack;
|
|
13
|
-
if (existingWebpack) {
|
|
14
|
-
return existingWebpack(config, options);
|
|
15
|
-
}
|
|
16
|
-
return config;
|
|
17
10
|
}
|
|
18
11
|
};
|
|
19
12
|
};
|
|
20
13
|
}
|
|
21
14
|
|
|
22
15
|
export { withBetterIntl };
|
|
23
|
-
//# sourceMappingURL=chunk-
|
|
24
|
-
//# sourceMappingURL=chunk-
|
|
16
|
+
//# sourceMappingURL=chunk-Q7QSMIA4.js.map
|
|
17
|
+
//# sourceMappingURL=chunk-Q7QSMIA4.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/plugin.ts"],"names":[],"mappings":";AAqBO,SAAS,eAAe,UAAA,EAAoC;AACjE,EAAA,OAAO,CAAC,UAAA,GAAsC,EAAC,KAAM;AACnD,IAAA,OAAO;AAAA,MACL,GAAG,UAAA;AAAA,MACH,GAAA,EAAK;AAAA,QACH,GAAI,UAAA,CAAW,GAAA;AAAA,QACf,mBAAA,EAAqB,IAAA,CAAK,SAAA,CAAU,UAAA,CAAW,OAAO,CAAA;AAAA,QACtD,4BAA4B,UAAA,CAAW;AAAA;AACzC,KACF;AAAA,EACF,CAAA;AACF","file":"chunk-Q7QSMIA4.js","sourcesContent":["/**\n * Next.js plugin (next.config.js integration).\n *\n * @example\n * // next.config.js\n * import { withBetterIntl } from \"@better-intl/next/plugin\";\n *\n * export default withBetterIntl({\n * locales: [\"en\", \"pt-BR\"],\n * defaultLocale: \"en\",\n * })({\n * // your next config\n * });\n */\n\nexport interface BetterIntlPluginConfig {\n locales: string[];\n defaultLocale: string;\n catalogsDir?: string;\n}\n\nexport function withBetterIntl(intlConfig: BetterIntlPluginConfig) {\n return (nextConfig: Record<string, unknown> = {}) => {\n return {\n ...nextConfig,\n env: {\n ...(nextConfig.env as Record<string, string> | undefined),\n BETTER_INTL_LOCALES: JSON.stringify(intlConfig.locales),\n BETTER_INTL_DEFAULT_LOCALE: intlConfig.defaultLocale,\n },\n };\n };\n}\n"]}
|
package/dist/index.cjs
CHANGED
|
@@ -66,13 +66,6 @@ function withBetterIntl(intlConfig) {
|
|
|
66
66
|
...nextConfig.env,
|
|
67
67
|
BETTER_INTL_LOCALES: JSON.stringify(intlConfig.locales),
|
|
68
68
|
BETTER_INTL_DEFAULT_LOCALE: intlConfig.defaultLocale
|
|
69
|
-
},
|
|
70
|
-
webpack(config, options) {
|
|
71
|
-
const existingWebpack = nextConfig.webpack;
|
|
72
|
-
if (existingWebpack) {
|
|
73
|
-
return existingWebpack(config, options);
|
|
74
|
-
}
|
|
75
|
-
return config;
|
|
76
69
|
}
|
|
77
70
|
};
|
|
78
71
|
};
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client-provider.tsx","../src/middleware.ts","../src/plugin.ts","../src/server.ts"],"names":["jsx","IntlProvider","parseAcceptLanguage","negotiateLocale","createIntlRuntime"],"mappings":";;;;;;;AA+BO,SAAS,sBAAA,CAAuB;AAAA,EACrC,MAAA;AAAA,EACA,QAAA;AAAA,EACA;AACF,CAAA,EAAgC;AAC9B,EAAA,uBACEA,cAAA,CAACC,kBAAA,EAAA,EAAa,MAAA,EAAgB,QAAA,EAC3B,QAAA,EACH,CAAA;AAEJ;ACfO,SAAS,qBAAqB,MAAA,EAA8B;AACjE,EAAA,MAAM;AAAA,IACJ,OAAA;AAAA,IACA,aAAA;AAAA,IACA,UAAA,GAAa,aAAA;AAAA,IACb,YAAA,GAAe;AAAA,GACjB,GAAI,MAAA;AAEJ,EAAA,OAAO,eAAe,WAAW,OAAA,EAAqC;AACpE,IAAA,MAAM,GAAA,GAAM,IAAI,GAAA,CAAI,OAAA,CAAQ,GAAG,CAAA;AAC/B,IAAA,MAAM,WAAW,GAAA,CAAI,QAAA;AAGrB,IAAA,MAAM,iBAAiB,OAAA,CAAQ,IAAA;AAAA,MAC7B,CAAC,CAAA,KAAM,QAAA,CAAS,UAAA,CAAW,CAAA,CAAA,EAAI,CAAC,CAAA,CAAA,CAAG,CAAA,IAAK,QAAA,KAAa,CAAA,CAAA,EAAI,CAAC,CAAA;AAAA,KAC5D;AAEA,IAAA,IAAI,cAAA,EAAgB;AAElB,MAAA,MAAM,QAAA,GAAW,IAAI,QAAA,CAAS,IAAA,EAAM;AAAA,QAClC,MAAA,EAAQ,GAAA;AAAA,QACR,OAAA,EAAS;AAAA,UACP,sBAAA,EAAwB;AAAA;AAC1B,OACD,CAAA;AACD,MAAA,OAAO,QAAA;AAAA,IACT;AAGA,IAAA,MAAM,YAAA,GAAe,OAAA,CAAQ,OAAA,CAAQ,GAAA,CAAI,QAAQ,CAAA,IAAK,EAAA;AACtD,IAAA,MA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AAW,GAAA;AAAA,QACf,mBAAA,EAAqB,IAAA,CAAK,SAAA,CAAU,UAAA,CAAW,OAAO,CAAA;AAAA,QACtD,4BAA4B,UAAA,CAAW;AAAA,OACzC;AAAA,MACA,OAAA,CACE,QACA,OAAA,EACA;AAGA,QAAA,MAAM,kBAAkB,UAAA,CAAW,OAAA;AAOnC,QAAA,IAAI,eAAA,EAAiB;AACnB,UAAA,OAAO,eAAA,CAAgB,QAAQ,OAAO,CAAA;AAAA,QACxC;AAEA,QAAA,OAAO,MAAA;AAAA,MACT;AAAA,KACF;AAAA,EACF,CAAA;AACF;ACxBA,eAAsB,YAAY,MAAA,EAA0C;AAC1E,EAAA,MAAM,GAAA,GAAM,MAAM,MAAA,EAAO;AACzB,EAAA,OAAO,SAAA,IAAa,GAAA,GACf,GAAA,CAA8B,OAAA,GAC9B,GAAA;AACP;AAKA,eAAsB,aAAA,CACpB,QACA,MAAA,EAGA;AACA,EAAA,MAAM,QAAA,GAAW,MAAM,WAAA,CAAY,MAAM,CAAA;AACzC,EAAA,MAAM,OAAA,GAAUC,sBAAA,CAAkB,MAAA,EAAQ,QAAQ,CAAA;AAClD,EAAA,OAAA,CAAQ,UAAU,MAAM,CAAA;AACxB,EAAA,OAAO,CAAC,IAAI,MAAA,EAAQ,cAAA,KAAmB,QAAQ,CAAA,CAAE,EAAA,EAAI,QAAQ,cAAc,CAAA;AAC7E","file":"index.cjs","sourcesContent":["\"use client\";\n\nimport type { Messages } from \"@better-intl/core\";\nimport { IntlProvider } from \"@better-intl/react\";\nimport type { ReactNode } from \"react\";\n\nexport interface NextIntlClientProviderProps {\n locale: string;\n messages: Messages;\n children: ReactNode;\n}\n\n/**\n * Client-side IntlProvider for Next.js App Router.\n *\n * Use in your layout to wrap client components:\n *\n * @example\n * // app/[locale]/layout.tsx\n * import { NextIntlClientProvider } from \"@better-intl/next\";\n *\n * export default async function Layout({ children, params }) {\n * const { locale } = await params;\n * const messages = (await import(`../../locales/${locale}.json`)).default;\n * return (\n * <NextIntlClientProvider locale={locale} messages={messages}>\n * {children}\n * </NextIntlClientProvider>\n * );\n * }\n */\nexport function NextIntlClientProvider({\n locale,\n messages,\n children,\n}: NextIntlClientProviderProps) {\n return (\n <IntlProvider locale={locale} messages={messages}>\n {children}\n </IntlProvider>\n );\n}\n","/**\n * Next.js middleware helper for locale detection and routing.\n *\n * @example\n * // middleware.ts\n * import { createIntlMiddleware } from \"@better-intl/next/middleware\";\n *\n * export default createIntlMiddleware({\n * locales: [\"en\", \"pt-BR\", \"es\"],\n * defaultLocale: \"en\",\n * });\n *\n * export const config = { matcher: [\"/((?!api|_next|.*\\\\..*).*)\"] };\n */\n\nimport { negotiateLocale, parseAcceptLanguage } from \"@better-intl/core\";\n\nexport interface IntlMiddlewareConfig {\n locales: string[];\n defaultLocale: string;\n /** Cookie name to persist locale choice (default: \"NEXT_LOCALE\") */\n cookieName?: string;\n /** URL prefix strategy: \"always\" | \"as-needed\" (default: \"as-needed\") */\n localePrefix?: \"always\" | \"as-needed\";\n}\n\nexport function createIntlMiddleware(config: IntlMiddlewareConfig) {\n const {\n locales,\n defaultLocale,\n cookieName = \"NEXT_LOCALE\",\n localePrefix = \"as-needed\",\n } = config;\n\n return async function middleware(request: Request): Promise<Response> {\n const url = new URL(request.url);\n const pathname = url.pathname;\n\n // Check if path already has locale prefix\n const pathnameLocale = locales.find(\n (l) => pathname.startsWith(`/${l}/`) || pathname === `/${l}`,\n );\n\n if (pathnameLocale) {\n // Path has locale — set cookie and continue\n const response = new Response(null, {\n status: 200,\n headers: {\n \"x-better-intl-locale\": pathnameLocale,\n },\n });\n return response;\n }\n\n // Detect locale from cookie, then Accept-Language header\n const cookieHeader = request.headers.get(\"cookie\") ?? \"\";\n const cookieLocale = parseCookie(cookieHeader, cookieName);\n\n const acceptLang = request.headers.get(\"accept-language\") ?? \"\";\n const preferred = cookieLocale\n ? [cookieLocale, ...parseAcceptLanguage(acceptLang)]\n : parseAcceptLanguage(acceptLang);\n\n const detectedLocale = negotiateLocale(preferred, locales, defaultLocale);\n\n // Redirect to locale-prefixed path\n if (localePrefix === \"always\" || detectedLocale !== defaultLocale) {\n const redirectUrl = new URL(`/${detectedLocale}${pathname}`, request.url);\n redirectUrl.search = url.search;\n return Response.redirect(redirectUrl.toString(), 307);\n }\n\n // Default locale with \"as-needed\" — pass through with locale header\n return new Response(null, {\n status: 200,\n headers: {\n \"x-better-intl-locale\": detectedLocale,\n },\n });\n };\n}\n\nfunction parseCookie(cookieHeader: string, name: string): string | undefined {\n const match = cookieHeader.match(new RegExp(`(?:^|;\\\\s*)${name}=([^;]*)`));\n return match?.[1];\n}\n","/**\n * Next.js plugin (next.config.js integration).\n *\n * @example\n * // next.config.js\n * import { withBetterIntl } from \"@better-intl/next/plugin\";\n *\n * export default withBetterIntl({\n * locales: [\"en\", \"pt-BR\"],\n * defaultLocale: \"en\",\n * })({\n * // your next config\n * });\n */\n\nexport interface BetterIntlPluginConfig {\n locales: string[];\n defaultLocale: string;\n catalogsDir?: string;\n}\n\nexport function withBetterIntl(intlConfig: BetterIntlPluginConfig) {\n return (nextConfig: Record<string, unknown> = {}) => {\n return {\n ...nextConfig,\n env: {\n ...(nextConfig.env as Record<string, string> | undefined),\n BETTER_INTL_LOCALES: JSON.stringify(intlConfig.locales),\n BETTER_INTL_DEFAULT_LOCALE: intlConfig.defaultLocale,\n },\n webpack(\n config: Record<string, unknown>,\n options: Record<string, unknown>,\n ) {\n // Future: add Webpack plugin for compile-time transforms\n\n const existingWebpack = nextConfig.webpack as\n | ((\n config: Record<string, unknown>,\n options: Record<string, unknown>,\n ) => Record<string, unknown>)\n | undefined;\n\n if (existingWebpack) {\n return existingWebpack(config, options);\n }\n\n return config;\n },\n };\n };\n}\n","/**\n * Server-side utilities for Next.js App Router.\n *\n * Works in Server Components and Route Handlers.\n *\n * @example\n * // app/[locale]/page.tsx\n * import { getMessages, getTranslator } from \"@better-intl/next/server\";\n *\n * export default async function Page({ params }: { params: { locale: string } }) {\n * const { locale } = await params;\n * const t = await getTranslator(locale, () => import(`../../locales/${locale}.json`));\n * return <h1>{t(\"greeting\")}</h1>;\n * }\n */\n\nimport {\n createIntlRuntime,\n type InterpolationValues,\n type Messages,\n} from \"@better-intl/core\";\n\nexport type MessageLoader = () => Promise<{ default: Messages } | Messages>;\n\n/**\n * Load messages for a locale using a dynamic import function.\n */\nexport async function getMessages(loader: MessageLoader): Promise<Messages> {\n const mod = await loader();\n return \"default\" in mod\n ? (mod as { default: Messages }).default\n : (mod as Messages);\n}\n\n/**\n * Create a `t()` function for server-side translation.\n */\nexport async function getTranslator(\n locale: string,\n loader: MessageLoader,\n): Promise<\n (id: string, values?: InterpolationValues, defaultMessage?: string) => string\n> {\n const messages = await getMessages(loader);\n const runtime = createIntlRuntime(locale, messages);\n runtime.setLocale(locale);\n return (id, values, defaultMessage) => runtime.t(id, values, defaultMessage);\n}\n\n/**\n * Generate metadata translations for Next.js `generateMetadata`.\n *\n * @example\n * export async function generateMetadata({ params }) {\n * const { locale } = await params;\n * const t = await getTranslator(locale, () => import(`../../locales/${locale}.json`));\n * return { title: t(\"meta.title\"), description: t(\"meta.description\") };\n * }\n */\nexport { getTranslator as getMetadataTranslator };\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/client-provider.tsx","../src/middleware.ts","../src/plugin.ts","../src/server.ts"],"names":["jsx","IntlProvider","parseAcceptLanguage","negotiateLocale","createIntlRuntime"],"mappings":";;;;;;;AA+BO,SAAS,sBAAA,CAAuB;AAAA,EACrC,MAAA;AAAA,EACA,QAAA;AAAA,EACA;AACF,CAAA,EAAgC;AAC9B,EAAA,uBACEA,cAAA,CAACC,kBAAA,EAAA,EAAa,MAAA,EAAgB,QAAA,EAC3B,QAAA,EACH,CAAA;AAEJ;ACfO,SAAS,qBAAqB,MAAA,EAA8B;AACjE,EAAA,MAAM;AAAA,IACJ,OAAA;AAAA,IACA,aAAA;AAAA,IACA,UAAA,GAAa,aAAA;AAAA,IACb,YAAA,GAAe;AAAA,GACjB,GAAI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AAU,MAAM,CAAA;AACxB,EAAA,OAAO,CAAC,IAAI,MAAA,EAAQ,cAAA,KAAmB,QAAQ,CAAA,CAAE,EAAA,EAAI,QAAQ,cAAc,CAAA;AAC7E","file":"index.cjs","sourcesContent":["\"use client\";\n\nimport type { Messages } from \"@better-intl/core\";\nimport { IntlProvider } from \"@better-intl/react\";\nimport type { ReactNode } from \"react\";\n\nexport interface NextIntlClientProviderProps {\n locale: string;\n messages: Messages;\n children: ReactNode;\n}\n\n/**\n * Client-side IntlProvider for Next.js App Router.\n *\n * Use in your layout to wrap client components:\n *\n * @example\n * // app/[locale]/layout.tsx\n * import { NextIntlClientProvider } from \"@better-intl/next\";\n *\n * export default async function Layout({ children, params }) {\n * const { locale } = await params;\n * const messages = (await import(`../../locales/${locale}.json`)).default;\n * return (\n * <NextIntlClientProvider locale={locale} messages={messages}>\n * {children}\n * </NextIntlClientProvider>\n * );\n * }\n */\nexport function NextIntlClientProvider({\n locale,\n messages,\n children,\n}: NextIntlClientProviderProps) {\n return (\n <IntlProvider locale={locale} messages={messages}>\n {children}\n </IntlProvider>\n );\n}\n","/**\n * Next.js middleware helper for locale detection and routing.\n *\n * @example\n * // middleware.ts\n * import { createIntlMiddleware } from \"@better-intl/next/middleware\";\n *\n * export default createIntlMiddleware({\n * locales: [\"en\", \"pt-BR\", \"es\"],\n * defaultLocale: \"en\",\n * });\n *\n * export const config = { matcher: [\"/((?!api|_next|.*\\\\..*).*)\"] };\n */\n\nimport { negotiateLocale, parseAcceptLanguage } from \"@better-intl/core\";\n\nexport interface IntlMiddlewareConfig {\n locales: string[];\n defaultLocale: string;\n /** Cookie name to persist locale choice (default: \"NEXT_LOCALE\") */\n cookieName?: string;\n /** URL prefix strategy: \"always\" | \"as-needed\" (default: \"as-needed\") */\n localePrefix?: \"always\" | \"as-needed\";\n}\n\nexport function createIntlMiddleware(config: IntlMiddlewareConfig) {\n const {\n locales,\n defaultLocale,\n cookieName = \"NEXT_LOCALE\",\n localePrefix = \"as-needed\",\n } = config;\n\n return async function middleware(request: Request): Promise<Response> {\n const url = new URL(request.url);\n const pathname = url.pathname;\n\n // Check if path already has locale prefix\n const pathnameLocale = locales.find(\n (l) => pathname.startsWith(`/${l}/`) || pathname === `/${l}`,\n );\n\n if (pathnameLocale) {\n // Path has locale — set cookie and continue\n const response = new Response(null, {\n status: 200,\n headers: {\n \"x-better-intl-locale\": pathnameLocale,\n },\n });\n return response;\n }\n\n // Detect locale from cookie, then Accept-Language header\n const cookieHeader = request.headers.get(\"cookie\") ?? \"\";\n const cookieLocale = parseCookie(cookieHeader, cookieName);\n\n const acceptLang = request.headers.get(\"accept-language\") ?? \"\";\n const preferred = cookieLocale\n ? [cookieLocale, ...parseAcceptLanguage(acceptLang)]\n : parseAcceptLanguage(acceptLang);\n\n const detectedLocale = negotiateLocale(preferred, locales, defaultLocale);\n\n // Redirect to locale-prefixed path\n if (localePrefix === \"always\" || detectedLocale !== defaultLocale) {\n const redirectUrl = new URL(`/${detectedLocale}${pathname}`, request.url);\n redirectUrl.search = url.search;\n return Response.redirect(redirectUrl.toString(), 307);\n }\n\n // Default locale with \"as-needed\" — pass through with locale header\n return new Response(null, {\n status: 200,\n headers: {\n \"x-better-intl-locale\": detectedLocale,\n },\n });\n };\n}\n\nfunction parseCookie(cookieHeader: string, name: string): string | undefined {\n const match = cookieHeader.match(new RegExp(`(?:^|;\\\\s*)${name}=([^;]*)`));\n return match?.[1];\n}\n","/**\n * Next.js plugin (next.config.js integration).\n *\n * @example\n * // next.config.js\n * import { withBetterIntl } from \"@better-intl/next/plugin\";\n *\n * export default withBetterIntl({\n * locales: [\"en\", \"pt-BR\"],\n * defaultLocale: \"en\",\n * })({\n * // your next config\n * });\n */\n\nexport interface BetterIntlPluginConfig {\n locales: string[];\n defaultLocale: string;\n catalogsDir?: string;\n}\n\nexport function withBetterIntl(intlConfig: BetterIntlPluginConfig) {\n return (nextConfig: Record<string, unknown> = {}) => {\n return {\n ...nextConfig,\n env: {\n ...(nextConfig.env as Record<string, string> | undefined),\n BETTER_INTL_LOCALES: JSON.stringify(intlConfig.locales),\n BETTER_INTL_DEFAULT_LOCALE: intlConfig.defaultLocale,\n },\n };\n };\n}\n","/**\n * Server-side utilities for Next.js App Router.\n *\n * Works in Server Components and Route Handlers.\n *\n * @example\n * // app/[locale]/page.tsx\n * import { getMessages, getTranslator } from \"@better-intl/next/server\";\n *\n * export default async function Page({ params }: { params: { locale: string } }) {\n * const { locale } = await params;\n * const t = await getTranslator(locale, () => import(`../../locales/${locale}.json`));\n * return <h1>{t(\"greeting\")}</h1>;\n * }\n */\n\nimport {\n createIntlRuntime,\n type InterpolationValues,\n type Messages,\n} from \"@better-intl/core\";\n\nexport type MessageLoader = () => Promise<{ default: Messages } | Messages>;\n\n/**\n * Load messages for a locale using a dynamic import function.\n */\nexport async function getMessages(loader: MessageLoader): Promise<Messages> {\n const mod = await loader();\n return \"default\" in mod\n ? (mod as { default: Messages }).default\n : (mod as Messages);\n}\n\n/**\n * Create a `t()` function for server-side translation.\n */\nexport async function getTranslator(\n locale: string,\n loader: MessageLoader,\n): Promise<\n (id: string, values?: InterpolationValues, defaultMessage?: string) => string\n> {\n const messages = await getMessages(loader);\n const runtime = createIntlRuntime(locale, messages);\n runtime.setLocale(locale);\n return (id, values, defaultMessage) => runtime.t(id, values, defaultMessage);\n}\n\n/**\n * Generate metadata translations for Next.js `generateMetadata`.\n *\n * @example\n * export async function generateMetadata({ params }) {\n * const { locale } = await params;\n * const t = await getTranslator(locale, () => import(`../../locales/${locale}.json`));\n * return { title: t(\"meta.title\"), description: t(\"meta.description\") };\n * }\n */\nexport { getTranslator as getMetadataTranslator };\n"]}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createIntlMiddleware } from './chunk-XM4CBDU4.js';
|
|
2
|
-
export { withBetterIntl } from './chunk-
|
|
2
|
+
export { withBetterIntl } from './chunk-Q7QSMIA4.js';
|
|
3
3
|
export { getMessages, getTranslator as getMetadataTranslator, getTranslator } from './chunk-MQVSQ3VH.js';
|
|
4
4
|
import { IntlProvider } from '@better-intl/react';
|
|
5
5
|
import { jsx } from 'react/jsx-runtime';
|
package/dist/plugin.cjs
CHANGED
|
@@ -9,13 +9,6 @@ function withBetterIntl(intlConfig) {
|
|
|
9
9
|
...nextConfig.env,
|
|
10
10
|
BETTER_INTL_LOCALES: JSON.stringify(intlConfig.locales),
|
|
11
11
|
BETTER_INTL_DEFAULT_LOCALE: intlConfig.defaultLocale
|
|
12
|
-
},
|
|
13
|
-
webpack(config, options) {
|
|
14
|
-
const existingWebpack = nextConfig.webpack;
|
|
15
|
-
if (existingWebpack) {
|
|
16
|
-
return existingWebpack(config, options);
|
|
17
|
-
}
|
|
18
|
-
return config;
|
|
19
12
|
}
|
|
20
13
|
};
|
|
21
14
|
};
|
package/dist/plugin.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/plugin.ts"],"names":[],"mappings":";;;AAqBO,SAAS,eAAe,UAAA,EAAoC;AACjE,EAAA,OAAO,CAAC,UAAA,GAAsC,EAAC,KAAM;AACnD,IAAA,OAAO;AAAA,MACL,GAAG,UAAA;AAAA,MACH,GAAA,EAAK;AAAA,QACH,GAAI,UAAA,CAAW,GAAA;AAAA,QACf,mBAAA,EAAqB,IAAA,CAAK,SAAA,CAAU,UAAA,CAAW,OAAO,CAAA;AAAA,QACtD,4BAA4B,UAAA,CAAW;AAAA
|
|
1
|
+
{"version":3,"sources":["../src/plugin.ts"],"names":[],"mappings":";;;AAqBO,SAAS,eAAe,UAAA,EAAoC;AACjE,EAAA,OAAO,CAAC,UAAA,GAAsC,EAAC,KAAM;AACnD,IAAA,OAAO;AAAA,MACL,GAAG,UAAA;AAAA,MACH,GAAA,EAAK;AAAA,QACH,GAAI,UAAA,CAAW,GAAA;AAAA,QACf,mBAAA,EAAqB,IAAA,CAAK,SAAA,CAAU,UAAA,CAAW,OAAO,CAAA;AAAA,QACtD,4BAA4B,UAAA,CAAW;AAAA;AACzC,KACF;AAAA,EACF,CAAA;AACF","file":"plugin.cjs","sourcesContent":["/**\n * Next.js plugin (next.config.js integration).\n *\n * @example\n * // next.config.js\n * import { withBetterIntl } from \"@better-intl/next/plugin\";\n *\n * export default withBetterIntl({\n * locales: [\"en\", \"pt-BR\"],\n * defaultLocale: \"en\",\n * })({\n * // your next config\n * });\n */\n\nexport interface BetterIntlPluginConfig {\n locales: string[];\n defaultLocale: string;\n catalogsDir?: string;\n}\n\nexport function withBetterIntl(intlConfig: BetterIntlPluginConfig) {\n return (nextConfig: Record<string, unknown> = {}) => {\n return {\n ...nextConfig,\n env: {\n ...(nextConfig.env as Record<string, string> | undefined),\n BETTER_INTL_LOCALES: JSON.stringify(intlConfig.locales),\n BETTER_INTL_DEFAULT_LOCALE: intlConfig.defaultLocale,\n },\n };\n };\n}\n"]}
|
package/dist/plugin.d.cts
CHANGED
|
@@ -22,7 +22,6 @@ declare function withBetterIntl(intlConfig: BetterIntlPluginConfig): (nextConfig
|
|
|
22
22
|
BETTER_INTL_LOCALES: string;
|
|
23
23
|
BETTER_INTL_DEFAULT_LOCALE: string;
|
|
24
24
|
};
|
|
25
|
-
webpack(config: Record<string, unknown>, options: Record<string, unknown>): Record<string, unknown>;
|
|
26
25
|
};
|
|
27
26
|
|
|
28
27
|
export { type BetterIntlPluginConfig, withBetterIntl };
|
package/dist/plugin.d.ts
CHANGED
|
@@ -22,7 +22,6 @@ declare function withBetterIntl(intlConfig: BetterIntlPluginConfig): (nextConfig
|
|
|
22
22
|
BETTER_INTL_LOCALES: string;
|
|
23
23
|
BETTER_INTL_DEFAULT_LOCALE: string;
|
|
24
24
|
};
|
|
25
|
-
webpack(config: Record<string, unknown>, options: Record<string, unknown>): Record<string, unknown>;
|
|
26
25
|
};
|
|
27
26
|
|
|
28
27
|
export { type BetterIntlPluginConfig, withBetterIntl };
|
package/dist/plugin.js
CHANGED
package/dist/rsc.cjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@better-intl/core');
|
|
4
|
+
var react = require('react');
|
|
5
|
+
|
|
6
|
+
// src/rsc.ts
|
|
7
|
+
var getRequestIntl = react.cache(
|
|
8
|
+
() => ({
|
|
9
|
+
locale: "",
|
|
10
|
+
messages: {}
|
|
11
|
+
})
|
|
12
|
+
);
|
|
13
|
+
function setRequestIntl(locale, messages) {
|
|
14
|
+
const intl = getRequestIntl();
|
|
15
|
+
intl.locale = locale;
|
|
16
|
+
intl.messages = messages;
|
|
17
|
+
}
|
|
18
|
+
function rsc() {
|
|
19
|
+
const { messages } = getRequestIntl();
|
|
20
|
+
return (id, values, defaultMessage) => {
|
|
21
|
+
const message = messages[id] ?? defaultMessage ?? id;
|
|
22
|
+
return values ? core.interpolate(message, values) : message;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function getRequestLocale() {
|
|
26
|
+
return getRequestIntl().locale;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
exports.getRequestLocale = getRequestLocale;
|
|
30
|
+
exports.rsc = rsc;
|
|
31
|
+
exports.setRequestIntl = setRequestIntl;
|
|
32
|
+
//# sourceMappingURL=rsc.cjs.map
|
|
33
|
+
//# sourceMappingURL=rsc.cjs.map
|
package/dist/rsc.cjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/rsc.ts"],"names":["cache","interpolate"],"mappings":";;;;;;AAoCA,IAAM,cAAA,GAAiBA,WAAA;AAAA,EACrB,OAAoB;AAAA,IAClB,MAAA,EAAQ,EAAA;AAAA,IACR,UAAU;AAAC,GACb;AACF,CAAA;AAOO,SAAS,cAAA,CAAe,QAAgB,QAAA,EAA0B;AACvE,EAAA,MAAM,OAAO,cAAA,EAAe;AAC5B,EAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AACd,EAAA,IAAA,CAAK,QAAA,GAAW,QAAA;AAClB;AAQO,SAAS,GAAA,GAIJ;AACV,EAAA,MAAM,EAAE,QAAA,EAAS,GAAI,cAAA,EAAe;AAEpC,EAAA,OAAO,CACL,EAAA,EACA,MAAA,EACA,cAAA,KACW;AACX,IAAA,MAAM,OAAA,GAAU,QAAA,CAAS,EAAE,CAAA,IAAK,cAAA,IAAkB,EAAA;AAClD,IAAA,OAAO,MAAA,GAASC,gBAAA,CAAY,OAAA,EAAS,MAAM,CAAA,GAAI,OAAA;AAAA,EACjD,CAAA;AACF;AAKO,SAAS,gBAAA,GAA2B;AACzC,EAAA,OAAO,gBAAe,CAAE,MAAA;AAC1B","file":"rsc.cjs","sourcesContent":["/**\n * React Server Component support for better-intl.\n *\n * Uses React.cache() to store locale/messages per request.\n * Call setRequestIntl() once in your layout, then rsc() in any server component.\n *\n * @example\n * // app/[locale]/layout.tsx\n * import { setRequestIntl } from \"@better-intl/next/rsc\";\n *\n * export default async function Layout({ params, children }) {\n * const { locale } = await params;\n * const messages = (await import(`../../locales/${locale}.json`)).default;\n * setRequestIntl(locale, messages);\n * return <>{children}</>;\n * }\n *\n * @example\n * // app/[locale]/page.tsx — no imports needed, Babel handles it\n * export default function Page() {\n * return <h1>Hello world</h1>;\n * }\n */\n\nimport {\n type InterpolationValues,\n interpolate,\n type Messages,\n} from \"@better-intl/core\";\nimport { cache } from \"react\";\n\ninterface RequestIntl {\n locale: string;\n messages: Messages;\n}\n\nconst getRequestIntl = cache(\n (): RequestIntl => ({\n locale: \"\",\n messages: {},\n }),\n);\n\n/**\n * Set the locale and messages for the current request.\n * Call this once in your root layout — all server components\n * rendered in the same request will have access.\n */\nexport function setRequestIntl(locale: string, messages: Messages): void {\n const intl = getRequestIntl();\n intl.locale = locale;\n intl.messages = messages;\n}\n\n/**\n * Get a synchronous t() function for server components.\n * Reads from the request-scoped cache set by setRequestIntl().\n *\n * The Babel plugin injects this automatically in server components.\n */\nexport function rsc(): (\n id: string,\n values?: InterpolationValues,\n defaultMessage?: string,\n) => string {\n const { messages } = getRequestIntl();\n\n return (\n id: string,\n values?: InterpolationValues,\n defaultMessage?: string,\n ): string => {\n const message = messages[id] ?? defaultMessage ?? id;\n return values ? interpolate(message, values) : message;\n };\n}\n\n/**\n * Get the current request locale.\n */\nexport function getRequestLocale(): string {\n return getRequestIntl().locale;\n}\n"]}
|
package/dist/rsc.d.cts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { InterpolationValues, Messages } from '@better-intl/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* React Server Component support for better-intl.
|
|
5
|
+
*
|
|
6
|
+
* Uses React.cache() to store locale/messages per request.
|
|
7
|
+
* Call setRequestIntl() once in your layout, then rsc() in any server component.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // app/[locale]/layout.tsx
|
|
11
|
+
* import { setRequestIntl } from "@better-intl/next/rsc";
|
|
12
|
+
*
|
|
13
|
+
* export default async function Layout({ params, children }) {
|
|
14
|
+
* const { locale } = await params;
|
|
15
|
+
* const messages = (await import(`../../locales/${locale}.json`)).default;
|
|
16
|
+
* setRequestIntl(locale, messages);
|
|
17
|
+
* return <>{children}</>;
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // app/[locale]/page.tsx — no imports needed, Babel handles it
|
|
22
|
+
* export default function Page() {
|
|
23
|
+
* return <h1>Hello world</h1>;
|
|
24
|
+
* }
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Set the locale and messages for the current request.
|
|
29
|
+
* Call this once in your root layout — all server components
|
|
30
|
+
* rendered in the same request will have access.
|
|
31
|
+
*/
|
|
32
|
+
declare function setRequestIntl(locale: string, messages: Messages): void;
|
|
33
|
+
/**
|
|
34
|
+
* Get a synchronous t() function for server components.
|
|
35
|
+
* Reads from the request-scoped cache set by setRequestIntl().
|
|
36
|
+
*
|
|
37
|
+
* The Babel plugin injects this automatically in server components.
|
|
38
|
+
*/
|
|
39
|
+
declare function rsc(): (id: string, values?: InterpolationValues, defaultMessage?: string) => string;
|
|
40
|
+
/**
|
|
41
|
+
* Get the current request locale.
|
|
42
|
+
*/
|
|
43
|
+
declare function getRequestLocale(): string;
|
|
44
|
+
|
|
45
|
+
export { getRequestLocale, rsc, setRequestIntl };
|
package/dist/rsc.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { InterpolationValues, Messages } from '@better-intl/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* React Server Component support for better-intl.
|
|
5
|
+
*
|
|
6
|
+
* Uses React.cache() to store locale/messages per request.
|
|
7
|
+
* Call setRequestIntl() once in your layout, then rsc() in any server component.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // app/[locale]/layout.tsx
|
|
11
|
+
* import { setRequestIntl } from "@better-intl/next/rsc";
|
|
12
|
+
*
|
|
13
|
+
* export default async function Layout({ params, children }) {
|
|
14
|
+
* const { locale } = await params;
|
|
15
|
+
* const messages = (await import(`../../locales/${locale}.json`)).default;
|
|
16
|
+
* setRequestIntl(locale, messages);
|
|
17
|
+
* return <>{children}</>;
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // app/[locale]/page.tsx — no imports needed, Babel handles it
|
|
22
|
+
* export default function Page() {
|
|
23
|
+
* return <h1>Hello world</h1>;
|
|
24
|
+
* }
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Set the locale and messages for the current request.
|
|
29
|
+
* Call this once in your root layout — all server components
|
|
30
|
+
* rendered in the same request will have access.
|
|
31
|
+
*/
|
|
32
|
+
declare function setRequestIntl(locale: string, messages: Messages): void;
|
|
33
|
+
/**
|
|
34
|
+
* Get a synchronous t() function for server components.
|
|
35
|
+
* Reads from the request-scoped cache set by setRequestIntl().
|
|
36
|
+
*
|
|
37
|
+
* The Babel plugin injects this automatically in server components.
|
|
38
|
+
*/
|
|
39
|
+
declare function rsc(): (id: string, values?: InterpolationValues, defaultMessage?: string) => string;
|
|
40
|
+
/**
|
|
41
|
+
* Get the current request locale.
|
|
42
|
+
*/
|
|
43
|
+
declare function getRequestLocale(): string;
|
|
44
|
+
|
|
45
|
+
export { getRequestLocale, rsc, setRequestIntl };
|
package/dist/rsc.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { interpolate } from '@better-intl/core';
|
|
2
|
+
import { cache } from 'react';
|
|
3
|
+
|
|
4
|
+
// src/rsc.ts
|
|
5
|
+
var getRequestIntl = cache(
|
|
6
|
+
() => ({
|
|
7
|
+
locale: "",
|
|
8
|
+
messages: {}
|
|
9
|
+
})
|
|
10
|
+
);
|
|
11
|
+
function setRequestIntl(locale, messages) {
|
|
12
|
+
const intl = getRequestIntl();
|
|
13
|
+
intl.locale = locale;
|
|
14
|
+
intl.messages = messages;
|
|
15
|
+
}
|
|
16
|
+
function rsc() {
|
|
17
|
+
const { messages } = getRequestIntl();
|
|
18
|
+
return (id, values, defaultMessage) => {
|
|
19
|
+
const message = messages[id] ?? defaultMessage ?? id;
|
|
20
|
+
return values ? interpolate(message, values) : message;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function getRequestLocale() {
|
|
24
|
+
return getRequestIntl().locale;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { getRequestLocale, rsc, setRequestIntl };
|
|
28
|
+
//# sourceMappingURL=rsc.js.map
|
|
29
|
+
//# sourceMappingURL=rsc.js.map
|
package/dist/rsc.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/rsc.ts"],"names":[],"mappings":";;;;AAoCA,IAAM,cAAA,GAAiB,KAAA;AAAA,EACrB,OAAoB;AAAA,IAClB,MAAA,EAAQ,EAAA;AAAA,IACR,UAAU;AAAC,GACb;AACF,CAAA;AAOO,SAAS,cAAA,CAAe,QAAgB,QAAA,EAA0B;AACvE,EAAA,MAAM,OAAO,cAAA,EAAe;AAC5B,EAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AACd,EAAA,IAAA,CAAK,QAAA,GAAW,QAAA;AAClB;AAQO,SAAS,GAAA,GAIJ;AACV,EAAA,MAAM,EAAE,QAAA,EAAS,GAAI,cAAA,EAAe;AAEpC,EAAA,OAAO,CACL,EAAA,EACA,MAAA,EACA,cAAA,KACW;AACX,IAAA,MAAM,OAAA,GAAU,QAAA,CAAS,EAAE,CAAA,IAAK,cAAA,IAAkB,EAAA;AAClD,IAAA,OAAO,MAAA,GAAS,WAAA,CAAY,OAAA,EAAS,MAAM,CAAA,GAAI,OAAA;AAAA,EACjD,CAAA;AACF;AAKO,SAAS,gBAAA,GAA2B;AACzC,EAAA,OAAO,gBAAe,CAAE,MAAA;AAC1B","file":"rsc.js","sourcesContent":["/**\n * React Server Component support for better-intl.\n *\n * Uses React.cache() to store locale/messages per request.\n * Call setRequestIntl() once in your layout, then rsc() in any server component.\n *\n * @example\n * // app/[locale]/layout.tsx\n * import { setRequestIntl } from \"@better-intl/next/rsc\";\n *\n * export default async function Layout({ params, children }) {\n * const { locale } = await params;\n * const messages = (await import(`../../locales/${locale}.json`)).default;\n * setRequestIntl(locale, messages);\n * return <>{children}</>;\n * }\n *\n * @example\n * // app/[locale]/page.tsx — no imports needed, Babel handles it\n * export default function Page() {\n * return <h1>Hello world</h1>;\n * }\n */\n\nimport {\n type InterpolationValues,\n interpolate,\n type Messages,\n} from \"@better-intl/core\";\nimport { cache } from \"react\";\n\ninterface RequestIntl {\n locale: string;\n messages: Messages;\n}\n\nconst getRequestIntl = cache(\n (): RequestIntl => ({\n locale: \"\",\n messages: {},\n }),\n);\n\n/**\n * Set the locale and messages for the current request.\n * Call this once in your root layout — all server components\n * rendered in the same request will have access.\n */\nexport function setRequestIntl(locale: string, messages: Messages): void {\n const intl = getRequestIntl();\n intl.locale = locale;\n intl.messages = messages;\n}\n\n/**\n * Get a synchronous t() function for server components.\n * Reads from the request-scoped cache set by setRequestIntl().\n *\n * The Babel plugin injects this automatically in server components.\n */\nexport function rsc(): (\n id: string,\n values?: InterpolationValues,\n defaultMessage?: string,\n) => string {\n const { messages } = getRequestIntl();\n\n return (\n id: string,\n values?: InterpolationValues,\n defaultMessage?: string,\n ): string => {\n const message = messages[id] ?? defaultMessage ?? id;\n return values ? interpolate(message, values) : message;\n };\n}\n\n/**\n * Get the current request locale.\n */\nexport function getRequestLocale(): string {\n return getRequestIntl().locale;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-intl/next",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Next.js App Router integration for better-intl",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -26,6 +26,11 @@
|
|
|
26
26
|
"import": "./dist/server.js",
|
|
27
27
|
"require": "./dist/server.cjs",
|
|
28
28
|
"types": "./dist/server.d.ts"
|
|
29
|
+
},
|
|
30
|
+
"./rsc": {
|
|
31
|
+
"import": "./dist/rsc.js",
|
|
32
|
+
"require": "./dist/rsc.cjs",
|
|
33
|
+
"types": "./dist/rsc.d.ts"
|
|
29
34
|
}
|
|
30
35
|
},
|
|
31
36
|
"files": [
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/plugin.ts"],"names":[],"mappings":";AAqBO,SAAS,eAAe,UAAA,EAAoC;AACjE,EAAA,OAAO,CAAC,UAAA,GAAsC,EAAC,KAAM;AACnD,IAAA,OAAO;AAAA,MACL,GAAG,UAAA;AAAA,MACH,GAAA,EAAK;AAAA,QACH,GAAI,UAAA,CAAW,GAAA;AAAA,QACf,mBAAA,EAAqB,IAAA,CAAK,SAAA,CAAU,UAAA,CAAW,OAAO,CAAA;AAAA,QACtD,4BAA4B,UAAA,CAAW;AAAA,OACzC;AAAA,MACA,OAAA,CACE,QACA,OAAA,EACA;AAGA,QAAA,MAAM,kBAAkB,UAAA,CAAW,OAAA;AAOnC,QAAA,IAAI,eAAA,EAAiB;AACnB,UAAA,OAAO,eAAA,CAAgB,QAAQ,OAAO,CAAA;AAAA,QACxC;AAEA,QAAA,OAAO,MAAA;AAAA,MACT;AAAA,KACF;AAAA,EACF,CAAA;AACF","file":"chunk-4EXOYGAS.js","sourcesContent":["/**\n * Next.js plugin (next.config.js integration).\n *\n * @example\n * // next.config.js\n * import { withBetterIntl } from \"@better-intl/next/plugin\";\n *\n * export default withBetterIntl({\n * locales: [\"en\", \"pt-BR\"],\n * defaultLocale: \"en\",\n * })({\n * // your next config\n * });\n */\n\nexport interface BetterIntlPluginConfig {\n locales: string[];\n defaultLocale: string;\n catalogsDir?: string;\n}\n\nexport function withBetterIntl(intlConfig: BetterIntlPluginConfig) {\n return (nextConfig: Record<string, unknown> = {}) => {\n return {\n ...nextConfig,\n env: {\n ...(nextConfig.env as Record<string, string> | undefined),\n BETTER_INTL_LOCALES: JSON.stringify(intlConfig.locales),\n BETTER_INTL_DEFAULT_LOCALE: intlConfig.defaultLocale,\n },\n webpack(\n config: Record<string, unknown>,\n options: Record<string, unknown>,\n ) {\n // Future: add Webpack plugin for compile-time transforms\n\n const existingWebpack = nextConfig.webpack as\n | ((\n config: Record<string, unknown>,\n options: Record<string, unknown>,\n ) => Record<string, unknown>)\n | undefined;\n\n if (existingWebpack) {\n return existingWebpack(config, options);\n }\n\n return config;\n },\n };\n };\n}\n"]}
|