@jgb-solutions/kit 0.3.0 → 0.4.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/CHANGELOG.md +4 -0
- package/README.md +43 -0
- package/dist/i18n.d.ts +104 -0
- package/dist/i18n.js +218 -0
- package/package.json +5 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to `@jgb-solutions/kit`. Versions follow semver.
|
|
4
4
|
|
|
5
|
+
## 0.4.0
|
|
6
|
+
|
|
7
|
+
- `@jgb-solutions/kit/i18n`: four languages (`en`, `fr`, `es`, `ht`) with one set of rules, for websites and Expo apps. No dependencies. Locale paths (English unprefixed; `/fr`, `/es`, `/ht`): `localeFromPath`, `stripLocale`, `localizedPath`, `alternates` (hreflang with x-default). Negotiation: `negotiate` (Accept-Language with q-values) and `suggestLocale` for a "view this page in X?" bar, with `LOCALE_COOKIE` remembering the choice (no automatic redirects). Messages: typed nested catalogs (`Catalog`, `defineCatalog`, `MessageKey`, `Params`), `createTranslator` with `{placeholders}`, plurals through `Intl.PluralRules`, fallback to English and then to the key, and `checkCatalog` for tests.
|
|
8
|
+
|
|
5
9
|
## 0.3.0
|
|
6
10
|
|
|
7
11
|
- `@jgb-solutions/kit/admin`: the admin panel kit, moved in from `@jgb-solutions/admin` 0.1.5 unchanged (the shell, data blocks with sortable columns and rows per page, forms, charts, the light/dark/system theme switch). Its stylesheet is `@jgb-solutions/kit/admin/styles.css`. To move: replace `@jgb-solutions/admin` with `@jgb-solutions/kit/admin` in imports (and `/styles.css`), then remove `@jgb-solutions/admin`.
|
package/README.md
CHANGED
|
@@ -143,6 +143,49 @@ new RPCLink({ url, headers: auth.authHeaders }) // Bearer on every RPC call
|
|
|
143
143
|
|
|
144
144
|
Failures come back as codes (`INVALID_USERNAME_OR_PASSWORD`, `ACCOUNT_NOT_LINKED`, `GOOGLE_ALREADY_LINKED`, `TOO_MANY_REQUESTS`, `NETWORK`, `CANCELLED`…), and the app words them. The module has no dependencies: the app passes in its secure storage and passkey modules.
|
|
145
145
|
|
|
146
|
+
### `@jgb-solutions/kit/i18n`: four languages, one set of rules
|
|
147
|
+
|
|
148
|
+
English, French, Spanish and Haitian Creole (`en`, `fr`, `es`, `ht`) for websites and Expo apps. No dependencies and no framework: it runs in browsers, Workers, Bun and Hermes. English has no URL prefix; the others live under `/fr`, `/es` and `/ht`.
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
import {
|
|
152
|
+
createTranslator,
|
|
153
|
+
defineCatalog,
|
|
154
|
+
localizedPath,
|
|
155
|
+
alternates,
|
|
156
|
+
negotiate,
|
|
157
|
+
suggestLocale,
|
|
158
|
+
LOCALE_COOKIE,
|
|
159
|
+
} from '@jgb-solutions/kit/i18n'
|
|
160
|
+
|
|
161
|
+
// English is the source: write it `as const`, so keys and {placeholders} are typed.
|
|
162
|
+
export const en = {
|
|
163
|
+
home: { title: 'Welcome', greeting: 'Hello, {name}!' },
|
|
164
|
+
items: { count: { one: '{count} item', other: '{count} items' } },
|
|
165
|
+
} as const
|
|
166
|
+
|
|
167
|
+
// A translation has the same shape. A missing key, or an extra key, is a type error.
|
|
168
|
+
export const fr = defineCatalog<typeof en>()({
|
|
169
|
+
home: { title: 'Bienvenue', greeting: 'Bonjour, {name} !' },
|
|
170
|
+
items: { count: { one: '{count} article', other: '{count} articles' } },
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
const t = createTranslator('fr', fr, en) // en answers when a fr leaf is empty
|
|
174
|
+
t('home.greeting', { name: 'Ana' }) // 'Bonjour, Ana !'
|
|
175
|
+
t('items.count', { count: 1200 }) // '1 200 articles' (plural rules and number format of the locale)
|
|
176
|
+
t('home.greeting') // type error: needs { name }
|
|
177
|
+
|
|
178
|
+
// SEO: one URL per language, and the hreflang links for <head>.
|
|
179
|
+
localizedPath('/about', 'es') // '/es/about'
|
|
180
|
+
alternates('/about', 'https://example.com') // en, fr, es, ht and x-default
|
|
181
|
+
|
|
182
|
+
// Suggest, never redirect: Google advises against redirecting by browser language.
|
|
183
|
+
const offer = suggestLocale({ acceptLanguage: request.headers.get('accept-language'), current: 'fr', chosen: cookieValue })
|
|
184
|
+
// null: no bar. 'es': show "View this page in Español?", and on a click set the LOCALE_COOKIE cookie and go to localizedPath(path, 'es').
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Also: `localeFromPath`, `stripLocale`, `isLocale`, `negotiate`, `LOCALES`, `LOCALE_NAMES` (each language in its own words). Placeholders are `{name}`; a plural leaf has `one` and `other` (plus `zero`, `two`, `few`, `many` where a language needs them) and takes `count`. Keep the catalogs honest in a test with `checkCatalog(en, fr)`: it lists missing and extra keys, empty strings, plurals without `other`, and placeholders that differ from English.
|
|
188
|
+
|
|
146
189
|
### `@jgb-solutions/kit/admin`: the admin panel (React 19)
|
|
147
190
|
|
|
148
191
|
The admin panel kit for JGB Solutions apps: a React shell, data blocks (sortable tables, rows per page), forms and SVG charts, styled once, accessible (WCAG 2.1 AA), phone-ready, light and dark. Each app builds its own pages from it. It was `@jgb-solutions/admin` until 0.1.5; from kit 0.3.0 it lives here (`import … from '@jgb-solutions/kit/admin'`).
|
package/dist/i18n.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* kit/i18n: four languages, one set of rules, for websites and Expo apps alike.
|
|
3
|
+
* No imports and no runtime APIs beyond `Intl`, so it runs in browsers, Workers, Bun and Hermes.
|
|
4
|
+
*
|
|
5
|
+
* - Locales and paths: English has no prefix, the others live under /fr, /es and /ht.
|
|
6
|
+
* - Negotiation: from an Accept-Language header, to offer (never force) a language.
|
|
7
|
+
* - Messages: nested catalogs, `{name}` placeholders, plurals, typed keys and typed params.
|
|
8
|
+
*/
|
|
9
|
+
export declare const LOCALES: readonly ["en", "fr", "es", "ht"];
|
|
10
|
+
export type Locale = (typeof LOCALES)[number];
|
|
11
|
+
export declare const DEFAULT_LOCALE: Locale;
|
|
12
|
+
/** Each language in its own words. */
|
|
13
|
+
export declare const LOCALE_NAMES: Record<Locale, string>;
|
|
14
|
+
/** Name of the cookie that remembers a visitor's choice. */
|
|
15
|
+
export declare const LOCALE_COOKIE = "locale";
|
|
16
|
+
export declare function isLocale(value: unknown): value is Locale;
|
|
17
|
+
/** The language of a path: its first segment when that is /fr, /es or /ht, else English. */
|
|
18
|
+
export declare function localeFromPath(path: string): Locale;
|
|
19
|
+
/** The path without its /fr, /es or /ht prefix (query and hash kept). Always starts with "/". */
|
|
20
|
+
export declare function stripLocale(path: string): string;
|
|
21
|
+
/** The path in another language: any prefix is replaced; English has none. Query and hash kept. */
|
|
22
|
+
export declare function localizedPath(path: string, locale: Locale): string;
|
|
23
|
+
/** hreflang alternates for a page's <head>: one per language, then x-default (English). */
|
|
24
|
+
export declare function alternates(path: string, origin: string): {
|
|
25
|
+
hreflang: string;
|
|
26
|
+
href: string;
|
|
27
|
+
}[];
|
|
28
|
+
/** The supported language an Accept-Language header prefers (q-values; ties keep header order). */
|
|
29
|
+
export declare function negotiate(acceptLanguage: string | null | undefined): Locale;
|
|
30
|
+
/**
|
|
31
|
+
* The language a "view this page in X?" bar should offer, or null for no bar: the visitor already
|
|
32
|
+
* chose (the cookie holds a valid locale), or their browser's language is the page's.
|
|
33
|
+
*/
|
|
34
|
+
export declare function suggestLocale(opts: {
|
|
35
|
+
acceptLanguage: string | null | undefined;
|
|
36
|
+
current: Locale;
|
|
37
|
+
chosen: string | null | undefined;
|
|
38
|
+
}): Locale | null;
|
|
39
|
+
/** A plural leaf: `one` and `other` always, the other CLDR categories when the language needs them. */
|
|
40
|
+
export type PluralMessage = {
|
|
41
|
+
one: string;
|
|
42
|
+
other: string;
|
|
43
|
+
zero?: string;
|
|
44
|
+
two?: string;
|
|
45
|
+
few?: string;
|
|
46
|
+
many?: string;
|
|
47
|
+
};
|
|
48
|
+
/** A catalog: nested plain objects whose leaves are strings or plural objects. */
|
|
49
|
+
export type Messages = {
|
|
50
|
+
readonly [key: string]: string | PluralMessage | Messages;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* The shape of a translation, from the English source: same keys, `string` leaves.
|
|
54
|
+
* Plural leaves become full `PluralMessage`s, so a language may add categories English lacks
|
|
55
|
+
* (French `many`). A missing key is a type error, and so is an extra key in an object literal
|
|
56
|
+
* (TypeScript's excess property check; see `defineCatalog`).
|
|
57
|
+
*/
|
|
58
|
+
export type Catalog<M> = {
|
|
59
|
+
-readonly [K in keyof M]: M[K] extends string ? string : M[K] extends {
|
|
60
|
+
readonly other: string;
|
|
61
|
+
} ? PluralMessage : Catalog<M[K]>;
|
|
62
|
+
};
|
|
63
|
+
type Join<P extends string, K extends string> = P extends '' ? K : `${P}.${K}`;
|
|
64
|
+
/** The dotted paths to every leaf: 'home.title' | 'items.count'. */
|
|
65
|
+
export type MessageKey<M, P extends string = ''> = {
|
|
66
|
+
[K in keyof M & string]: M[K] extends string | {
|
|
67
|
+
readonly other: string;
|
|
68
|
+
} ? Join<P, K> : MessageKey<M[K], Join<P, K>>;
|
|
69
|
+
}[keyof M & string];
|
|
70
|
+
type PlaceholderNames<S extends string> = S extends `${string}{${infer N}}${infer Rest}` ? N | PlaceholderNames<Rest> : never;
|
|
71
|
+
/** The params a source string needs: each `{name}` required; none (undefined) when it has no placeholder. */
|
|
72
|
+
export type Params<S> = S extends string ? [PlaceholderNames<S>] extends [never] ? undefined : {
|
|
73
|
+
[N in PlaceholderNames<S>]: string | number;
|
|
74
|
+
} : S extends {
|
|
75
|
+
readonly other: string;
|
|
76
|
+
} ? {
|
|
77
|
+
count: number;
|
|
78
|
+
} & {
|
|
79
|
+
[N in Exclude<PlaceholderNames<Extract<S[keyof S], string>>, 'count'>]: string | number;
|
|
80
|
+
} : undefined;
|
|
81
|
+
type ValueAt<M, K extends string> = K extends `${infer Head}.${infer Tail}` ? Head extends keyof M ? ValueAt<M[Head], Tail> : never : K extends keyof M ? M[K] : never;
|
|
82
|
+
type TranslateArgs<M, K extends string> = undefined extends Params<ValueAt<M, K>> ? [params?: Params<ValueAt<M, K>>] : [params: Params<ValueAt<M, K>>];
|
|
83
|
+
/** `t(key, params?)`: the key must exist in the source catalog, and params are required exactly when it needs them. */
|
|
84
|
+
export type Translator<M> = <K extends MessageKey<M>>(key: K, ...args: TranslateArgs<M, K>) => string;
|
|
85
|
+
/**
|
|
86
|
+
* Types a translation against the English source and rejects extra keys:
|
|
87
|
+
* `export const fr = defineCatalog<typeof en>()({ … })`. A plain `const fr: Catalog<typeof en> = { … }`
|
|
88
|
+
* does the same for object literals; this helper just names the intent and keeps the typed result.
|
|
89
|
+
*/
|
|
90
|
+
export declare function defineCatalog<M>(): (catalog: Catalog<M>) => Catalog<M>;
|
|
91
|
+
export declare function createTranslator<M extends Messages>(locale: Locale, messages: M, fallback?: M): Translator<M>;
|
|
92
|
+
/**
|
|
93
|
+
* A translation typed by its source: `createTranslator<typeof en>('fr', fr)` (a website sends only the
|
|
94
|
+
* active language's catalog, so there may be no English fallback in the browser), or
|
|
95
|
+
* `createTranslator('fr', fr, en)` with English as the fallback for missing or empty strings.
|
|
96
|
+
*/
|
|
97
|
+
export declare function createTranslator<M extends Messages>(locale: Locale, messages: Catalog<NoInfer<M>>, fallback?: M): Translator<M>;
|
|
98
|
+
/**
|
|
99
|
+
* Problems in a translation, as readable strings naming the dotted key: missing keys, extra keys,
|
|
100
|
+
* empty strings, plural objects without `other`, and placeholder sets that differ from the source's
|
|
101
|
+
* (for plurals, per category: the source `other`'s placeholders, plus `count`). Empty list means fine.
|
|
102
|
+
*/
|
|
103
|
+
export declare function checkCatalog(source: object, translation: object): string[];
|
|
104
|
+
export {};
|
package/dist/i18n.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// src/i18n.ts
|
|
2
|
+
var LOCALES = ["en", "fr", "es", "ht"];
|
|
3
|
+
var DEFAULT_LOCALE = "en";
|
|
4
|
+
var LOCALE_NAMES = {
|
|
5
|
+
en: "English",
|
|
6
|
+
fr: "Français",
|
|
7
|
+
es: "Español",
|
|
8
|
+
ht: "Kreyòl ayisyen"
|
|
9
|
+
};
|
|
10
|
+
var LOCALE_COOKIE = "locale";
|
|
11
|
+
function isLocale(value) {
|
|
12
|
+
return typeof value === "string" && LOCALES.some((locale) => locale === value);
|
|
13
|
+
}
|
|
14
|
+
function prefixed(segment) {
|
|
15
|
+
return segment !== undefined && segment !== DEFAULT_LOCALE && isLocale(segment);
|
|
16
|
+
}
|
|
17
|
+
function splitPath(path) {
|
|
18
|
+
const cut = path.search(/[?#]/);
|
|
19
|
+
const pathname = cut === -1 ? path : path.slice(0, cut);
|
|
20
|
+
const rest = cut === -1 ? "" : path.slice(cut);
|
|
21
|
+
return { pathname: pathname.startsWith("/") ? pathname : `/${pathname}`, rest };
|
|
22
|
+
}
|
|
23
|
+
function firstSegment(pathname) {
|
|
24
|
+
return pathname.split("/")[1];
|
|
25
|
+
}
|
|
26
|
+
function localeFromPath(path) {
|
|
27
|
+
const segment = firstSegment(splitPath(path).pathname);
|
|
28
|
+
return prefixed(segment) ? segment : DEFAULT_LOCALE;
|
|
29
|
+
}
|
|
30
|
+
function stripLocale(path) {
|
|
31
|
+
const { pathname, rest } = splitPath(path);
|
|
32
|
+
const segment = firstSegment(pathname);
|
|
33
|
+
if (!prefixed(segment))
|
|
34
|
+
return pathname + rest;
|
|
35
|
+
return (pathname.slice(segment.length + 1) || "/") + rest;
|
|
36
|
+
}
|
|
37
|
+
function localizedPath(path, locale) {
|
|
38
|
+
const stripped = stripLocale(path);
|
|
39
|
+
if (locale === DEFAULT_LOCALE)
|
|
40
|
+
return stripped;
|
|
41
|
+
const { pathname, rest } = splitPath(stripped);
|
|
42
|
+
return `/${locale}${pathname === "/" ? "" : pathname}${rest}`;
|
|
43
|
+
}
|
|
44
|
+
function alternates(path, origin) {
|
|
45
|
+
const base = origin.replace(/\/+$/, "");
|
|
46
|
+
return [
|
|
47
|
+
...LOCALES.map((locale) => ({ hreflang: locale, href: base + localizedPath(path, locale) })),
|
|
48
|
+
{ hreflang: "x-default", href: base + localizedPath(path, DEFAULT_LOCALE) }
|
|
49
|
+
];
|
|
50
|
+
}
|
|
51
|
+
var LANGUAGE_TAG = /^[A-Za-z]{1,8}(?:-[A-Za-z0-9]{1,8})*$/;
|
|
52
|
+
function negotiate(acceptLanguage) {
|
|
53
|
+
if (!acceptLanguage)
|
|
54
|
+
return DEFAULT_LOCALE;
|
|
55
|
+
let best = DEFAULT_LOCALE;
|
|
56
|
+
let bestQuality = 0;
|
|
57
|
+
for (const part of acceptLanguage.split(",")) {
|
|
58
|
+
const [tag = "", ...params] = part.split(";").map((piece) => piece.trim());
|
|
59
|
+
if (!LANGUAGE_TAG.test(tag))
|
|
60
|
+
continue;
|
|
61
|
+
let quality = 1;
|
|
62
|
+
let malformed = false;
|
|
63
|
+
for (const param of params) {
|
|
64
|
+
const match = /^q=(\d(?:\.\d{0,3})?)$/i.exec(param);
|
|
65
|
+
const value = match?.[1] === undefined ? Number.NaN : Number(match[1]);
|
|
66
|
+
if (Number.isNaN(value) || value > 1)
|
|
67
|
+
malformed = true;
|
|
68
|
+
else
|
|
69
|
+
quality = value;
|
|
70
|
+
}
|
|
71
|
+
if (malformed || quality <= 0)
|
|
72
|
+
continue;
|
|
73
|
+
const primary = tag.split("-")[0]?.toLowerCase();
|
|
74
|
+
if (isLocale(primary) && quality > bestQuality) {
|
|
75
|
+
best = primary;
|
|
76
|
+
bestQuality = quality;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return best;
|
|
80
|
+
}
|
|
81
|
+
function suggestLocale(opts) {
|
|
82
|
+
if (isLocale(opts.chosen))
|
|
83
|
+
return null;
|
|
84
|
+
const wanted = negotiate(opts.acceptLanguage);
|
|
85
|
+
return wanted === opts.current ? null : wanted;
|
|
86
|
+
}
|
|
87
|
+
var PLURAL_CATEGORIES = ["zero", "one", "two", "few", "many", "other"];
|
|
88
|
+
function defineCatalog() {
|
|
89
|
+
return (catalog) => catalog;
|
|
90
|
+
}
|
|
91
|
+
function isRecord(value) {
|
|
92
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
93
|
+
}
|
|
94
|
+
function isPlural(value) {
|
|
95
|
+
return isRecord(value) && typeof value.other === "string";
|
|
96
|
+
}
|
|
97
|
+
function leafAt(messages, key) {
|
|
98
|
+
let node = messages;
|
|
99
|
+
for (const part of key.split(".")) {
|
|
100
|
+
if (!isRecord(node))
|
|
101
|
+
return;
|
|
102
|
+
node = node[part];
|
|
103
|
+
}
|
|
104
|
+
if (typeof node === "string")
|
|
105
|
+
return node || undefined;
|
|
106
|
+
if (isPlural(node) && node.other)
|
|
107
|
+
return node;
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
function createTranslator(locale, messages, fallback) {
|
|
111
|
+
const rules = new Intl.PluralRules(locale);
|
|
112
|
+
const numbers = new Intl.NumberFormat(locale);
|
|
113
|
+
const fill = (template, params, formatNumbers) => template.replace(/\{(\w+)\}/g, (placeholder, name) => {
|
|
114
|
+
const value = params?.[name];
|
|
115
|
+
if (value === undefined)
|
|
116
|
+
return placeholder;
|
|
117
|
+
return typeof value === "number" && formatNumbers ? numbers.format(value) : String(value);
|
|
118
|
+
});
|
|
119
|
+
return (key, params) => {
|
|
120
|
+
const leaf = leafAt(messages, key) ?? leafAt(fallback, key);
|
|
121
|
+
if (leaf === undefined)
|
|
122
|
+
return key;
|
|
123
|
+
if (typeof leaf === "string")
|
|
124
|
+
return fill(leaf, params, false);
|
|
125
|
+
const count = params?.count;
|
|
126
|
+
const category = typeof count === "number" ? rules.select(count) : "other";
|
|
127
|
+
const chosen = leaf[category];
|
|
128
|
+
return fill(typeof chosen === "string" && chosen ? chosen : leaf.other === undefined ? key : String(leaf.other), params, true);
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
var placeholdersOf = (text) => new Set([...text.matchAll(/\{(\w+)\}/g)].map((match) => match[1] ?? ""));
|
|
132
|
+
var list = (names) => names.size === 0 ? "none" : [...names].sort().map((name) => `{${name}}`).join(", ");
|
|
133
|
+
var sameSet = (a, b) => a.size === b.size && [...a].every((name) => b.has(name));
|
|
134
|
+
function describe(value) {
|
|
135
|
+
if (typeof value === "string")
|
|
136
|
+
return "a string";
|
|
137
|
+
if (isPlural(value))
|
|
138
|
+
return "a plural object";
|
|
139
|
+
if (isRecord(value))
|
|
140
|
+
return "a group of messages";
|
|
141
|
+
return typeof value;
|
|
142
|
+
}
|
|
143
|
+
function compare(source, translation, path, problems) {
|
|
144
|
+
const sourceIsGroup = isRecord(source) && !isPlural(source);
|
|
145
|
+
if (sourceIsGroup) {
|
|
146
|
+
if (!isRecord(translation) || isPlural(translation)) {
|
|
147
|
+
problems.push(`${path}: expected a group of messages, found ${describe(translation)}`);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
for (const key of Object.keys(source)) {
|
|
151
|
+
const child = path ? `${path}.${key}` : key;
|
|
152
|
+
if (!(key in translation))
|
|
153
|
+
problems.push(`${child}: missing key`);
|
|
154
|
+
else
|
|
155
|
+
compare(source[key], translation[key], child, problems);
|
|
156
|
+
}
|
|
157
|
+
for (const key of Object.keys(translation))
|
|
158
|
+
if (!(key in source))
|
|
159
|
+
problems.push(`${path ? `${path}.${key}` : key}: extra key`);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (typeof source === "string") {
|
|
163
|
+
if (typeof translation !== "string")
|
|
164
|
+
return void problems.push(`${path}: expected a string, found ${describe(translation)}`);
|
|
165
|
+
if (translation.trim() === "")
|
|
166
|
+
return void problems.push(`${path}: empty string`);
|
|
167
|
+
const wanted = placeholdersOf(source);
|
|
168
|
+
const found = placeholdersOf(translation);
|
|
169
|
+
if (!sameSet(wanted, found))
|
|
170
|
+
problems.push(`${path}: placeholders differ (source ${list(wanted)}, translation ${list(found)})`);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (isPlural(source)) {
|
|
174
|
+
if (!isRecord(translation) || typeof translation.other !== "string") {
|
|
175
|
+
problems.push(`${path}: plural object must have an "other" string, found ${describe(translation)}`);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const required = placeholdersOf(source.other);
|
|
179
|
+
required.delete("count");
|
|
180
|
+
const allowed = new Set([...required, "count"]);
|
|
181
|
+
for (const [category, text] of Object.entries(translation)) {
|
|
182
|
+
const at = `${path}.${category}`;
|
|
183
|
+
if (!PLURAL_CATEGORIES.some((name) => name === category))
|
|
184
|
+
problems.push(`${at}: unknown plural category`);
|
|
185
|
+
else if (typeof text !== "string" || text.trim() === "")
|
|
186
|
+
problems.push(`${at}: empty string`);
|
|
187
|
+
else {
|
|
188
|
+
const found = placeholdersOf(text);
|
|
189
|
+
const missing = [...required].filter((name) => !found.has(name));
|
|
190
|
+
const unknown = [...found].filter((name) => !allowed.has(name));
|
|
191
|
+
if (missing.length || unknown.length)
|
|
192
|
+
problems.push(`${at}: placeholders differ (source allows ${list(allowed)}, translation has ${list(found)})`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function checkCatalog(source, translation) {
|
|
199
|
+
const problems = [];
|
|
200
|
+
compare(source, translation, "", problems);
|
|
201
|
+
return problems;
|
|
202
|
+
}
|
|
203
|
+
export {
|
|
204
|
+
DEFAULT_LOCALE,
|
|
205
|
+
LOCALES,
|
|
206
|
+
LOCALE_COOKIE,
|
|
207
|
+
LOCALE_NAMES,
|
|
208
|
+
alternates,
|
|
209
|
+
checkCatalog,
|
|
210
|
+
createTranslator,
|
|
211
|
+
defineCatalog,
|
|
212
|
+
isLocale,
|
|
213
|
+
localeFromPath,
|
|
214
|
+
localizedPath,
|
|
215
|
+
negotiate,
|
|
216
|
+
stripLocale,
|
|
217
|
+
suggestLocale
|
|
218
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jgb-solutions/kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "One package for JGB Solutions apps, in tree-shakable namespaces: Cloudflare Access, Better Auth (kit/access, kit/auth\u2026) and the admin panel kit (kit/admin, React 19). An app bundles only the subpaths it imports.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "JGB Solutions",
|
|
@@ -61,6 +61,10 @@
|
|
|
61
61
|
"types": "./dist/auth/expo.d.ts",
|
|
62
62
|
"import": "./dist/auth/expo.js"
|
|
63
63
|
},
|
|
64
|
+
"./i18n": {
|
|
65
|
+
"types": "./dist/i18n.d.ts",
|
|
66
|
+
"import": "./dist/i18n.js"
|
|
67
|
+
},
|
|
64
68
|
"./admin": {
|
|
65
69
|
"types": "./dist/admin.d.ts",
|
|
66
70
|
"import": "./dist/admin.js"
|