brease-client 0.4.0 → 0.5.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 +13 -14
- package/dist/{chunk-N6C2X6KA.js → chunk-ULU4ABCQ.js} +19 -1
- package/dist/index.cjs +21 -0
- package/dist/index.d.cts +11 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +1 -1
- package/dist/next.js +1 -1
- package/package.json +2 -15
- package/dist/react.cjs +0 -110
- package/dist/react.d.cts +0 -43
- package/dist/react.d.ts +0 -43
- package/dist/react.js +0 -81
package/README.md
CHANGED
|
@@ -114,7 +114,7 @@ Brease works with any routing: `getPage()` and friends don't care how a site is
|
|
|
114
114
|
```
|
|
115
115
|
app/
|
|
116
116
|
[[...path]]/
|
|
117
|
-
layout.tsx root layout: <html lang>,
|
|
117
|
+
layout.tsx root layout: <html lang>, the locale context for the page
|
|
118
118
|
page.tsx every page in every locale; the page's content picks its template
|
|
119
119
|
not-found.tsx
|
|
120
120
|
og/[[...path]]/route.tsx share images (image files can't sit inside an optional catch-all)
|
|
@@ -124,8 +124,8 @@ brease/templates.tsx { home: …, article: … }, the site's own page
|
|
|
124
124
|
|
|
125
125
|
```tsx
|
|
126
126
|
// app/[[...path]]/layout.tsx (replaces app/layout.tsx)
|
|
127
|
-
import { BreaseLocaleProvider } from 'brease-client/react'
|
|
128
127
|
import { brease } from '@/lib/brease'
|
|
128
|
+
import { LocaleProvider } from '@/components/locale' // the site's own React context around the value
|
|
129
129
|
|
|
130
130
|
type Props = { children: React.ReactNode; params: Promise<{ path?: string[] }> }
|
|
131
131
|
|
|
@@ -140,7 +140,7 @@ export default async function RootLayout({ children, params }: Props) {
|
|
|
140
140
|
return (
|
|
141
141
|
<html lang={context.locale}>
|
|
142
142
|
<body>
|
|
143
|
-
<
|
|
143
|
+
<LocaleProvider value={context}>{children}</LocaleProvider>
|
|
144
144
|
</body>
|
|
145
145
|
</html>
|
|
146
146
|
)
|
|
@@ -180,22 +180,21 @@ export default async function Page({ params }: Props) {
|
|
|
180
180
|
}
|
|
181
181
|
```
|
|
182
182
|
|
|
183
|
-
|
|
183
|
+
The package stays framework-free: it gives the data and the logic, and the site renders them with its own framework.
|
|
184
184
|
|
|
185
|
-
```
|
|
186
|
-
import {
|
|
187
|
-
|
|
188
|
-
const { locale, href } = useLocale()
|
|
189
|
-
href('') // this locale's home: '/', '/en', …
|
|
190
|
-
href('about', 'de') // '/de/about'
|
|
185
|
+
```ts
|
|
186
|
+
import { localeHref, localeLinks } from 'brease-client'
|
|
191
187
|
|
|
192
|
-
|
|
193
|
-
|
|
188
|
+
const context = await brease.localeContext(path) // { locale, settings, alternates, homes }
|
|
189
|
+
localeLinks(context) // [{ locale: 'hu', label: 'Magyar', href: '/rolunk', current: true }, …]
|
|
190
|
+
localeHref(context.settings, 'about', 'de') // '/de/about'
|
|
194
191
|
```
|
|
195
192
|
|
|
196
|
-
|
|
193
|
+
A locale switcher is then a few lines in any framework: a list of `<a href={l.href} hreflang={l.locale}>{l.label}</a>`, with `aria-current` on the current one, hidden while `localeLinks()` returns fewer than two links. Share `context` with the components that need it the framework's own way (a React context, Astro props, a template variable).
|
|
194
|
+
|
|
195
|
+
- `localeLinks()` links each locale's version of the current page, else that locale's home page; locales without content yet are left out.
|
|
197
196
|
- Everything that read a fixed locale (`getPage(…, { locale: 'hu' })`, `getNavigation`, `site.seo.hu`) takes it from the route instead.
|
|
198
|
-
- Links inside content are already locale URLs. Build other internal links with `
|
|
197
|
+
- Links inside content are already locale URLs. Build other internal links with `localeHref(settings, slug, locale)`.
|
|
199
198
|
- Text written into components (button labels, captions) stays as it is; moving it into Brease content is a separate change.
|
|
200
199
|
- `next.config`: `redirects: breaseRedirects()` (from `brease-client/next`) serves the redirects managed in Brease.
|
|
201
200
|
|
|
@@ -73,6 +73,24 @@ function resolvePath(settings, pathname) {
|
|
|
73
73
|
return { slug: clean, locale: settings.defaultLocale };
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
// src/locales.ts
|
|
77
|
+
function localeLinks(context) {
|
|
78
|
+
const { settings, alternates, homes, locale: current } = context;
|
|
79
|
+
return settings.locales.flatMap((l) => {
|
|
80
|
+
const href = alternates[l] ?? homes[l] ?? (l === current ? buildPath(settings, "", l) : null);
|
|
81
|
+
return href ? [{ locale: l, label: nativeName(l), href, current: l === current }] : [];
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function nativeName(locale) {
|
|
85
|
+
try {
|
|
86
|
+
const name = new Intl.DisplayNames([locale], { type: "language" }).of(locale) ?? locale;
|
|
87
|
+
return name.charAt(0).toLocaleUpperCase(locale) + name.slice(1);
|
|
88
|
+
} catch {
|
|
89
|
+
return locale;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
var localeHref = (settings, slug, locale) => buildPath(settings, slug, locale);
|
|
93
|
+
|
|
76
94
|
// src/index.ts
|
|
77
95
|
var RETRY_AFTER_CAP_MS = 1e4;
|
|
78
96
|
var SITE_TTL_MS = 6e4;
|
|
@@ -201,4 +219,4 @@ function pathOf(url) {
|
|
|
201
219
|
}
|
|
202
220
|
}
|
|
203
221
|
|
|
204
|
-
export { BreaseError, buildPath, createBrease, env, normalizeSlug, resolvePath };
|
|
222
|
+
export { BreaseError, buildPath, createBrease, env, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath };
|
package/dist/index.cjs
CHANGED
|
@@ -77,6 +77,24 @@ function resolvePath(settings, pathname) {
|
|
|
77
77
|
return { slug: clean, locale: settings.defaultLocale };
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
// src/locales.ts
|
|
81
|
+
function localeLinks(context) {
|
|
82
|
+
const { settings, alternates, homes, locale: current } = context;
|
|
83
|
+
return settings.locales.flatMap((l) => {
|
|
84
|
+
const href = alternates[l] ?? homes[l] ?? (l === current ? buildPath(settings, "", l) : null);
|
|
85
|
+
return href ? [{ locale: l, label: nativeName(l), href, current: l === current }] : [];
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function nativeName(locale) {
|
|
89
|
+
try {
|
|
90
|
+
const name = new Intl.DisplayNames([locale], { type: "language" }).of(locale) ?? locale;
|
|
91
|
+
return name.charAt(0).toLocaleUpperCase(locale) + name.slice(1);
|
|
92
|
+
} catch {
|
|
93
|
+
return locale;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
var localeHref = (settings, slug, locale) => buildPath(settings, slug, locale);
|
|
97
|
+
|
|
80
98
|
// src/index.ts
|
|
81
99
|
var RETRY_AFTER_CAP_MS = 1e4;
|
|
82
100
|
var SITE_TTL_MS = 6e4;
|
|
@@ -208,5 +226,8 @@ function pathOf(url) {
|
|
|
208
226
|
exports.BreaseError = BreaseError;
|
|
209
227
|
exports.buildPath = buildPath;
|
|
210
228
|
exports.createBrease = createBrease;
|
|
229
|
+
exports.localeHref = localeHref;
|
|
230
|
+
exports.localeLinks = localeLinks;
|
|
231
|
+
exports.nativeName = nativeName;
|
|
211
232
|
exports.normalizeSlug = normalizeSlug;
|
|
212
233
|
exports.resolvePath = resolvePath;
|
package/dist/index.d.cts
CHANGED
|
@@ -11,6 +11,16 @@ declare class BreaseError extends Error {
|
|
|
11
11
|
static fromResponse(res: Response): Promise<BreaseError>;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
type LocaleLink = {
|
|
15
|
+
locale: string;
|
|
16
|
+
label: string;
|
|
17
|
+
href: string;
|
|
18
|
+
current: boolean;
|
|
19
|
+
};
|
|
20
|
+
declare function localeLinks(context: LocaleContext): LocaleLink[];
|
|
21
|
+
declare function nativeName(locale: string): string;
|
|
22
|
+
declare const localeHref: (settings: UrlSettings, slug: string, locale: string) => string;
|
|
23
|
+
|
|
14
24
|
declare function normalizeSlug(input: string): string;
|
|
15
25
|
declare function buildPath(settings: UrlSettings, slug: string, locale: string): string;
|
|
16
26
|
declare function resolvePath(settings: UrlSettings, pathname: string): {
|
|
@@ -61,4 +71,4 @@ interface Brease {
|
|
|
61
71
|
}
|
|
62
72
|
declare function createBrease(opts?: BreaseOptions): Brease;
|
|
63
73
|
|
|
64
|
-
export { type Brease, BreaseError, type BreaseOptions, type LocaleContext, Navigation, Page, PageRef, Redirect, Release, Site, SitemapEntry, UrlSettings, buildPath, createBrease, normalizeSlug, resolvePath };
|
|
74
|
+
export { type Brease, BreaseError, type BreaseOptions, type LocaleContext, type LocaleLink, Navigation, Page, PageRef, Redirect, Release, Site, SitemapEntry, UrlSettings, buildPath, createBrease, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath };
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,16 @@ declare class BreaseError extends Error {
|
|
|
11
11
|
static fromResponse(res: Response): Promise<BreaseError>;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
type LocaleLink = {
|
|
15
|
+
locale: string;
|
|
16
|
+
label: string;
|
|
17
|
+
href: string;
|
|
18
|
+
current: boolean;
|
|
19
|
+
};
|
|
20
|
+
declare function localeLinks(context: LocaleContext): LocaleLink[];
|
|
21
|
+
declare function nativeName(locale: string): string;
|
|
22
|
+
declare const localeHref: (settings: UrlSettings, slug: string, locale: string) => string;
|
|
23
|
+
|
|
14
24
|
declare function normalizeSlug(input: string): string;
|
|
15
25
|
declare function buildPath(settings: UrlSettings, slug: string, locale: string): string;
|
|
16
26
|
declare function resolvePath(settings: UrlSettings, pathname: string): {
|
|
@@ -61,4 +71,4 @@ interface Brease {
|
|
|
61
71
|
}
|
|
62
72
|
declare function createBrease(opts?: BreaseOptions): Brease;
|
|
63
73
|
|
|
64
|
-
export { type Brease, BreaseError, type BreaseOptions, type LocaleContext, Navigation, Page, PageRef, Redirect, Release, Site, SitemapEntry, UrlSettings, buildPath, createBrease, normalizeSlug, resolvePath };
|
|
74
|
+
export { type Brease, BreaseError, type BreaseOptions, type LocaleContext, type LocaleLink, Navigation, Page, PageRef, Redirect, Release, Site, SitemapEntry, UrlSettings, buildPath, createBrease, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { BreaseError, buildPath, createBrease, normalizeSlug, resolvePath } from './chunk-
|
|
1
|
+
export { BreaseError, buildPath, createBrease, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath } from './chunk-ULU4ABCQ.js';
|
|
2
2
|
import './chunk-PKBMQBKP.js';
|
package/dist/next.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "brease-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Brease delivery API client for Next.js, Astro and any fetch-capable runtime",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -46,11 +46,6 @@
|
|
|
46
46
|
"types": "./dist/image.d.ts",
|
|
47
47
|
"import": "./dist/image.js",
|
|
48
48
|
"require": "./dist/image.cjs"
|
|
49
|
-
},
|
|
50
|
-
"./react": {
|
|
51
|
-
"types": "./dist/react.d.ts",
|
|
52
|
-
"import": "./dist/react.js",
|
|
53
|
-
"require": "./dist/react.cjs"
|
|
54
49
|
}
|
|
55
50
|
},
|
|
56
51
|
"files": [
|
|
@@ -61,24 +56,16 @@
|
|
|
61
56
|
"node": ">=18"
|
|
62
57
|
},
|
|
63
58
|
"peerDependencies": {
|
|
64
|
-
"next": ">=14"
|
|
65
|
-
"react": ">=18"
|
|
59
|
+
"next": ">=14"
|
|
66
60
|
},
|
|
67
61
|
"peerDependenciesMeta": {
|
|
68
62
|
"next": {
|
|
69
63
|
"optional": true
|
|
70
|
-
},
|
|
71
|
-
"react": {
|
|
72
|
-
"optional": true
|
|
73
64
|
}
|
|
74
65
|
},
|
|
75
66
|
"devDependencies": {
|
|
76
67
|
"@biomejs/biome": "^2.5.14",
|
|
77
68
|
"@types/node": "^22.20.4",
|
|
78
|
-
"@types/react": "^19.3.0",
|
|
79
|
-
"@types/react-dom": "^19.3.0",
|
|
80
|
-
"react": "^19.3.0",
|
|
81
|
-
"react-dom": "^19.3.0",
|
|
82
69
|
"tsup": "^8.5.1",
|
|
83
70
|
"typescript": "^5.9.3",
|
|
84
71
|
"vitest": "^5.0.1"
|
package/dist/react.cjs
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
"use strict";
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
-
|
|
21
|
-
// src/react.tsx
|
|
22
|
-
var react_exports = {};
|
|
23
|
-
__export(react_exports, {
|
|
24
|
-
BreaseLocaleProvider: () => BreaseLocaleProvider,
|
|
25
|
-
LocaleSwitcher: () => LocaleSwitcher,
|
|
26
|
-
localeLinks: () => localeLinks,
|
|
27
|
-
nativeName: () => nativeName,
|
|
28
|
-
useLocale: () => useLocale,
|
|
29
|
-
useLocaleLinks: () => useLocaleLinks
|
|
30
|
-
});
|
|
31
|
-
module.exports = __toCommonJS(react_exports);
|
|
32
|
-
var import_react = require("react");
|
|
33
|
-
|
|
34
|
-
// src/urls.ts
|
|
35
|
-
function normalizeSlug(input) {
|
|
36
|
-
return input.trim().replace(/^\/+|\/+$/g, "").replace(/\/{2,}/g, "/");
|
|
37
|
-
}
|
|
38
|
-
function buildPath(settings, slug, locale) {
|
|
39
|
-
const clean = normalizeSlug(slug);
|
|
40
|
-
if (settings.localeStrategy === "CUSTOM" && settings.urlTemplate) {
|
|
41
|
-
const path = settings.urlTemplate.replace("{locale}", locale).replace("{slug}", clean);
|
|
42
|
-
return `/${normalizeSlug(path)}`;
|
|
43
|
-
}
|
|
44
|
-
const prefixed = settings.localeStrategy === "PREFIX_ALL" || locale !== settings.defaultLocale;
|
|
45
|
-
const parts = [prefixed ? locale : "", clean].filter(Boolean);
|
|
46
|
-
return `/${parts.join("/")}`;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// src/react.tsx
|
|
50
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
51
|
-
var Context = (0, import_react.createContext)(null);
|
|
52
|
-
function BreaseLocaleProvider({ value, children }) {
|
|
53
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
|
|
54
|
-
}
|
|
55
|
-
function useLocale() {
|
|
56
|
-
const value = (0, import_react.useContext)(Context);
|
|
57
|
-
if (!value) throw new Error("useLocale() needs a <BreaseLocaleProvider> above it");
|
|
58
|
-
return {
|
|
59
|
-
...value,
|
|
60
|
-
locales: value.settings.locales,
|
|
61
|
-
href: (slug, locale = value.locale) => buildPath(value.settings, slug, locale)
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
function useLocaleLinks() {
|
|
65
|
-
const { locale, settings, alternates, homes } = useLocale();
|
|
66
|
-
return localeLinks(settings, { alternates, homes }, locale);
|
|
67
|
-
}
|
|
68
|
-
function localeLinks(settings, paths, current) {
|
|
69
|
-
return settings.locales.flatMap((l) => {
|
|
70
|
-
const href = paths.alternates[l] ?? paths.homes[l] ?? (l === current ? buildPath(settings, "", l) : null);
|
|
71
|
-
return href ? [{ locale: l, label: nativeName(l), href, current: l === current }] : [];
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
function nativeName(locale) {
|
|
75
|
-
try {
|
|
76
|
-
const name = new Intl.DisplayNames([locale], { type: "language" }).of(locale) ?? locale;
|
|
77
|
-
return name.charAt(0).toLocaleUpperCase(locale) + name.slice(1);
|
|
78
|
-
} catch {
|
|
79
|
-
return locale;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
function LocaleSwitcher({
|
|
83
|
-
className,
|
|
84
|
-
linkClassName,
|
|
85
|
-
label = "Language"
|
|
86
|
-
}) {
|
|
87
|
-
const links = useLocaleLinks();
|
|
88
|
-
if (links.length < 2) return null;
|
|
89
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("nav", { "aria-label": label, className, children: links.map((l) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
90
|
-
"a",
|
|
91
|
-
{
|
|
92
|
-
href: l.href,
|
|
93
|
-
hrefLang: l.locale,
|
|
94
|
-
lang: l.locale,
|
|
95
|
-
"aria-current": l.current ? "page" : void 0,
|
|
96
|
-
className: linkClassName,
|
|
97
|
-
children: l.label
|
|
98
|
-
},
|
|
99
|
-
l.locale
|
|
100
|
-
)) });
|
|
101
|
-
}
|
|
102
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
103
|
-
0 && (module.exports = {
|
|
104
|
-
BreaseLocaleProvider,
|
|
105
|
-
LocaleSwitcher,
|
|
106
|
-
localeLinks,
|
|
107
|
-
nativeName,
|
|
108
|
-
useLocale,
|
|
109
|
-
useLocaleLinks
|
|
110
|
-
});
|
package/dist/react.d.cts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import * as react from 'react';
|
|
2
|
-
import { ReactNode } from 'react';
|
|
3
|
-
|
|
4
|
-
type LocaleStrategy = 'PREFIX_EXCEPT_DEFAULT' | 'PREFIX_ALL' | 'CUSTOM';
|
|
5
|
-
type UrlSettings = {
|
|
6
|
-
locales: string[];
|
|
7
|
-
defaultLocale: string;
|
|
8
|
-
localeStrategy: LocaleStrategy;
|
|
9
|
-
urlTemplate?: string | null;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
type LocaleContext = {
|
|
13
|
-
locale: string;
|
|
14
|
-
settings: UrlSettings;
|
|
15
|
-
alternates: Record<string, string>;
|
|
16
|
-
homes: Record<string, string>;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
declare function BreaseLocaleProvider({ value, children }: {
|
|
20
|
-
value: LocaleContext;
|
|
21
|
-
children: ReactNode;
|
|
22
|
-
}): react.JSX.Element;
|
|
23
|
-
type LocaleInfo = LocaleContext & {
|
|
24
|
-
locales: string[];
|
|
25
|
-
href: (slug: string, locale?: string) => string;
|
|
26
|
-
};
|
|
27
|
-
declare function useLocale(): LocaleInfo;
|
|
28
|
-
type LocaleLink = {
|
|
29
|
-
locale: string;
|
|
30
|
-
label: string;
|
|
31
|
-
href: string;
|
|
32
|
-
current: boolean;
|
|
33
|
-
};
|
|
34
|
-
declare function useLocaleLinks(): LocaleLink[];
|
|
35
|
-
declare function localeLinks(settings: UrlSettings, paths: Pick<LocaleContext, 'alternates' | 'homes'>, current: string): LocaleLink[];
|
|
36
|
-
declare function nativeName(locale: string): string;
|
|
37
|
-
declare function LocaleSwitcher({ className, linkClassName, label }: {
|
|
38
|
-
className?: string;
|
|
39
|
-
linkClassName?: string;
|
|
40
|
-
label?: string;
|
|
41
|
-
}): react.JSX.Element | null;
|
|
42
|
-
|
|
43
|
-
export { BreaseLocaleProvider, type LocaleInfo, type LocaleLink, LocaleSwitcher, localeLinks, nativeName, useLocale, useLocaleLinks };
|
package/dist/react.d.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import * as react from 'react';
|
|
2
|
-
import { ReactNode } from 'react';
|
|
3
|
-
|
|
4
|
-
type LocaleStrategy = 'PREFIX_EXCEPT_DEFAULT' | 'PREFIX_ALL' | 'CUSTOM';
|
|
5
|
-
type UrlSettings = {
|
|
6
|
-
locales: string[];
|
|
7
|
-
defaultLocale: string;
|
|
8
|
-
localeStrategy: LocaleStrategy;
|
|
9
|
-
urlTemplate?: string | null;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
type LocaleContext = {
|
|
13
|
-
locale: string;
|
|
14
|
-
settings: UrlSettings;
|
|
15
|
-
alternates: Record<string, string>;
|
|
16
|
-
homes: Record<string, string>;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
declare function BreaseLocaleProvider({ value, children }: {
|
|
20
|
-
value: LocaleContext;
|
|
21
|
-
children: ReactNode;
|
|
22
|
-
}): react.JSX.Element;
|
|
23
|
-
type LocaleInfo = LocaleContext & {
|
|
24
|
-
locales: string[];
|
|
25
|
-
href: (slug: string, locale?: string) => string;
|
|
26
|
-
};
|
|
27
|
-
declare function useLocale(): LocaleInfo;
|
|
28
|
-
type LocaleLink = {
|
|
29
|
-
locale: string;
|
|
30
|
-
label: string;
|
|
31
|
-
href: string;
|
|
32
|
-
current: boolean;
|
|
33
|
-
};
|
|
34
|
-
declare function useLocaleLinks(): LocaleLink[];
|
|
35
|
-
declare function localeLinks(settings: UrlSettings, paths: Pick<LocaleContext, 'alternates' | 'homes'>, current: string): LocaleLink[];
|
|
36
|
-
declare function nativeName(locale: string): string;
|
|
37
|
-
declare function LocaleSwitcher({ className, linkClassName, label }: {
|
|
38
|
-
className?: string;
|
|
39
|
-
linkClassName?: string;
|
|
40
|
-
label?: string;
|
|
41
|
-
}): react.JSX.Element | null;
|
|
42
|
-
|
|
43
|
-
export { BreaseLocaleProvider, type LocaleInfo, type LocaleLink, LocaleSwitcher, localeLinks, nativeName, useLocale, useLocaleLinks };
|
package/dist/react.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
|
|
3
|
-
// src/react.tsx
|
|
4
|
-
import { createContext, useContext } from "react";
|
|
5
|
-
|
|
6
|
-
// src/urls.ts
|
|
7
|
-
function normalizeSlug(input) {
|
|
8
|
-
return input.trim().replace(/^\/+|\/+$/g, "").replace(/\/{2,}/g, "/");
|
|
9
|
-
}
|
|
10
|
-
function buildPath(settings, slug, locale) {
|
|
11
|
-
const clean = normalizeSlug(slug);
|
|
12
|
-
if (settings.localeStrategy === "CUSTOM" && settings.urlTemplate) {
|
|
13
|
-
const path = settings.urlTemplate.replace("{locale}", locale).replace("{slug}", clean);
|
|
14
|
-
return `/${normalizeSlug(path)}`;
|
|
15
|
-
}
|
|
16
|
-
const prefixed = settings.localeStrategy === "PREFIX_ALL" || locale !== settings.defaultLocale;
|
|
17
|
-
const parts = [prefixed ? locale : "", clean].filter(Boolean);
|
|
18
|
-
return `/${parts.join("/")}`;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// src/react.tsx
|
|
22
|
-
import { jsx } from "react/jsx-runtime";
|
|
23
|
-
var Context = createContext(null);
|
|
24
|
-
function BreaseLocaleProvider({ value, children }) {
|
|
25
|
-
return /* @__PURE__ */ jsx(Context.Provider, { value, children });
|
|
26
|
-
}
|
|
27
|
-
function useLocale() {
|
|
28
|
-
const value = useContext(Context);
|
|
29
|
-
if (!value) throw new Error("useLocale() needs a <BreaseLocaleProvider> above it");
|
|
30
|
-
return {
|
|
31
|
-
...value,
|
|
32
|
-
locales: value.settings.locales,
|
|
33
|
-
href: (slug, locale = value.locale) => buildPath(value.settings, slug, locale)
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
function useLocaleLinks() {
|
|
37
|
-
const { locale, settings, alternates, homes } = useLocale();
|
|
38
|
-
return localeLinks(settings, { alternates, homes }, locale);
|
|
39
|
-
}
|
|
40
|
-
function localeLinks(settings, paths, current) {
|
|
41
|
-
return settings.locales.flatMap((l) => {
|
|
42
|
-
const href = paths.alternates[l] ?? paths.homes[l] ?? (l === current ? buildPath(settings, "", l) : null);
|
|
43
|
-
return href ? [{ locale: l, label: nativeName(l), href, current: l === current }] : [];
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
function nativeName(locale) {
|
|
47
|
-
try {
|
|
48
|
-
const name = new Intl.DisplayNames([locale], { type: "language" }).of(locale) ?? locale;
|
|
49
|
-
return name.charAt(0).toLocaleUpperCase(locale) + name.slice(1);
|
|
50
|
-
} catch {
|
|
51
|
-
return locale;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
function LocaleSwitcher({
|
|
55
|
-
className,
|
|
56
|
-
linkClassName,
|
|
57
|
-
label = "Language"
|
|
58
|
-
}) {
|
|
59
|
-
const links = useLocaleLinks();
|
|
60
|
-
if (links.length < 2) return null;
|
|
61
|
-
return /* @__PURE__ */ jsx("nav", { "aria-label": label, className, children: links.map((l) => /* @__PURE__ */ jsx(
|
|
62
|
-
"a",
|
|
63
|
-
{
|
|
64
|
-
href: l.href,
|
|
65
|
-
hrefLang: l.locale,
|
|
66
|
-
lang: l.locale,
|
|
67
|
-
"aria-current": l.current ? "page" : void 0,
|
|
68
|
-
className: linkClassName,
|
|
69
|
-
children: l.label
|
|
70
|
-
},
|
|
71
|
-
l.locale
|
|
72
|
-
)) });
|
|
73
|
-
}
|
|
74
|
-
export {
|
|
75
|
-
BreaseLocaleProvider,
|
|
76
|
-
LocaleSwitcher,
|
|
77
|
-
localeLinks,
|
|
78
|
-
nativeName,
|
|
79
|
-
useLocale,
|
|
80
|
-
useLocaleLinks
|
|
81
|
-
};
|