@jgb-solutions/kit 0.4.2 → 0.4.4
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 +9 -0
- package/README.md +2 -1
- package/dist/i18n.d.ts +15 -0
- package/dist/i18n.js +56 -4
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to `@jgb-solutions/kit`. Versions follow semver.
|
|
4
4
|
|
|
5
|
+
## 0.4.4
|
|
6
|
+
|
|
7
|
+
- `@jgb-solutions/kit/i18n`: `createTranslator` no longer crashes where `Intl.PluralRules` is missing (Hermes on Android, so every Expo app on Android). It then picks plural forms with the new `cldrPluralCategory(locale, count)`, CLDR's rules for en, fr, es, ht, pt (and pt-PT) and ar, tested to give the same answers as `Intl.PluralRules`. Without `Intl.NumberFormat`, numbers print plainly.
|
|
8
|
+
- Haitian Creole now counts the French way (0 and 1 take `one`), with or without `Intl`. CLDR has no Creole plural data, so `Intl` answered `other` for every number and a catalog's `one` text was never shown.
|
|
9
|
+
|
|
10
|
+
## 0.4.3
|
|
11
|
+
|
|
12
|
+
- `@jgb-solutions/kit/i18n`: `defaultLocaleRedirect(path)` gives the unprefixed address for a path that spells out English (`/en/privacy?x=1` → `/privacy?x=1`, `/en` → `/`), for the site's 301; null for every other path.
|
|
13
|
+
|
|
5
14
|
## 0.4.2
|
|
6
15
|
|
|
7
16
|
- Dependency update: `jose` 6.2.18 to 6.2.19 (patch).
|
package/README.md
CHANGED
|
@@ -173,6 +173,7 @@ export const fr = defineCatalog<typeof en>()({
|
|
|
173
173
|
const t = createTranslator('fr', fr, en) // en answers when a fr leaf is empty
|
|
174
174
|
t('home.greeting', { name: 'Ana' }) // 'Bonjour, Ana !'
|
|
175
175
|
t('items.count', { count: 1200 }) // '1 200 articles' (plural rules and number format of the locale)
|
|
176
|
+
// Without Intl.PluralRules (Hermes on Android), the same plural forms come from cldrPluralCategory.
|
|
176
177
|
t('home.greeting') // type error: needs { name }
|
|
177
178
|
|
|
178
179
|
// SEO: one URL per language, and the hreflang links for <head>.
|
|
@@ -184,7 +185,7 @@ const offer = suggestLocale({ acceptLanguage: request.headers.get('accept-langua
|
|
|
184
185
|
// 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
|
```
|
|
186
187
|
|
|
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
|
+
Also: `localeFromPath`, `stripLocale`, `defaultLocaleRedirect` (the 301 target for an `/en/…` address), `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
189
|
|
|
189
190
|
### `@jgb-solutions/kit/admin`: the admin panel (React 19)
|
|
190
191
|
|
package/dist/i18n.d.ts
CHANGED
|
@@ -18,6 +18,12 @@ export declare function isLocale(value: unknown): value is Locale;
|
|
|
18
18
|
export declare function localeFromPath(path: string): Locale;
|
|
19
19
|
/** The path without its /fr, /es or /ht prefix (query and hash kept). Always starts with "/". */
|
|
20
20
|
export declare function stripLocale(path: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* Where an address that spells out English should go: "/en/privacy?x=1" → "/privacy?x=1", "/en" → "/".
|
|
23
|
+
* English has no prefix, so a site answers /en/… with a 301 to this address. Null for every other
|
|
24
|
+
* path ("/fr/a", "/english", "/"), which needs no redirect.
|
|
25
|
+
*/
|
|
26
|
+
export declare function defaultLocaleRedirect(path: string): string | null;
|
|
21
27
|
/** The path in another language: any prefix is replaced; English has none. Query and hash kept. */
|
|
22
28
|
export declare function localizedPath(path: string, locale: Locale): string;
|
|
23
29
|
/** hreflang alternates for a page's <head>: one per language, then x-default (English). */
|
|
@@ -36,6 +42,7 @@ export declare function suggestLocale(opts: {
|
|
|
36
42
|
current: Locale;
|
|
37
43
|
chosen: string | null | undefined;
|
|
38
44
|
}): Locale | null;
|
|
45
|
+
declare const PLURAL_CATEGORIES: readonly ["zero", "one", "two", "few", "many", "other"];
|
|
39
46
|
/** A plural leaf: `one` and `other` always, the other CLDR categories when the language needs them. */
|
|
40
47
|
export type PluralMessage = {
|
|
41
48
|
one: string;
|
|
@@ -88,6 +95,14 @@ export type Translator<M> = <K extends MessageKey<M>>(key: K, ...args: Translate
|
|
|
88
95
|
* does the same for object literals; this helper just names the intent and keeps the typed result.
|
|
89
96
|
*/
|
|
90
97
|
export declare function defineCatalog<M>(): (catalog: Catalog<M>) => Catalog<M>;
|
|
98
|
+
export type PluralCategory = (typeof PLURAL_CATEGORIES)[number];
|
|
99
|
+
/**
|
|
100
|
+
* CLDR's cardinal plural category of `count`, without `Intl`: for engines that lack
|
|
101
|
+
* `Intl.PluralRules` (Hermes on Android, which Expo apps run on). Covers en, fr, es, ht (as fr),
|
|
102
|
+
* pt (Brazil; pt-PT apart) and ar; other languages get the English rule. Gives the same answers as
|
|
103
|
+
* `Intl.PluralRules` (default options: up to 3 decimals) for these languages (tested).
|
|
104
|
+
*/
|
|
105
|
+
export declare function cldrPluralCategory(locale: string, count: number): PluralCategory;
|
|
91
106
|
export declare function createTranslator<M extends Messages>(locale: Locale, messages: M, fallback?: M): Translator<M>;
|
|
92
107
|
/**
|
|
93
108
|
* A translation typed by its source: `createTranslator<typeof en>('fr', fr)` (a website sends only the
|
package/dist/i18n.js
CHANGED
|
@@ -34,6 +34,12 @@ function stripLocale(path) {
|
|
|
34
34
|
return pathname + rest;
|
|
35
35
|
return (pathname.slice(segment.length + 1) || "/") + rest;
|
|
36
36
|
}
|
|
37
|
+
function defaultLocaleRedirect(path) {
|
|
38
|
+
const { pathname, rest } = splitPath(path);
|
|
39
|
+
if (firstSegment(pathname) !== DEFAULT_LOCALE)
|
|
40
|
+
return null;
|
|
41
|
+
return (pathname.slice(DEFAULT_LOCALE.length + 1) || "/") + rest;
|
|
42
|
+
}
|
|
37
43
|
function localizedPath(path, locale) {
|
|
38
44
|
const stripped = stripLocale(path);
|
|
39
45
|
if (locale === DEFAULT_LOCALE)
|
|
@@ -107,14 +113,58 @@ function leafAt(messages, key) {
|
|
|
107
113
|
return node;
|
|
108
114
|
return;
|
|
109
115
|
}
|
|
116
|
+
var pluralLanguage = (locale) => locale.toLowerCase().split(/[-_]/)[0] === "ht" ? "fr" : locale;
|
|
117
|
+
function cldrPluralCategory(locale, count) {
|
|
118
|
+
if (!Number.isFinite(count))
|
|
119
|
+
return "other";
|
|
120
|
+
const n = Math.round(Math.abs(count) * 1000) / 1000;
|
|
121
|
+
const whole = Number.isInteger(n);
|
|
122
|
+
const i = Math.trunc(n);
|
|
123
|
+
const millions = whole && i !== 0 && i % 1e6 === 0;
|
|
124
|
+
const tag = pluralLanguage(locale).toLowerCase();
|
|
125
|
+
switch (tag.split(/[-_]/)[0]) {
|
|
126
|
+
case "fr":
|
|
127
|
+
return i === 0 || i === 1 ? "one" : millions ? "many" : "other";
|
|
128
|
+
case "pt":
|
|
129
|
+
if (tag === "pt-pt" || tag === "pt_pt")
|
|
130
|
+
return whole && i === 1 ? "one" : millions ? "many" : "other";
|
|
131
|
+
return i === 0 || i === 1 ? "one" : millions ? "many" : "other";
|
|
132
|
+
case "es":
|
|
133
|
+
return n === 1 ? "one" : millions ? "many" : "other";
|
|
134
|
+
case "ar": {
|
|
135
|
+
if (!whole)
|
|
136
|
+
return "other";
|
|
137
|
+
const h = i % 100;
|
|
138
|
+
if (i === 0)
|
|
139
|
+
return "zero";
|
|
140
|
+
if (i === 1)
|
|
141
|
+
return "one";
|
|
142
|
+
if (i === 2)
|
|
143
|
+
return "two";
|
|
144
|
+
if (h >= 3 && h <= 10)
|
|
145
|
+
return "few";
|
|
146
|
+
return h >= 11 ? "many" : "other";
|
|
147
|
+
}
|
|
148
|
+
default:
|
|
149
|
+
return whole && i === 1 ? "one" : "other";
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function pluralSelector(locale) {
|
|
153
|
+
const language = pluralLanguage(locale);
|
|
154
|
+
if (typeof Intl !== "undefined" && typeof Intl.PluralRules === "function") {
|
|
155
|
+
const rules = new Intl.PluralRules(language);
|
|
156
|
+
return (count) => rules.select(count);
|
|
157
|
+
}
|
|
158
|
+
return (count) => cldrPluralCategory(language, count);
|
|
159
|
+
}
|
|
110
160
|
function createTranslator(locale, messages, fallback) {
|
|
111
|
-
const
|
|
112
|
-
const numbers = new Intl.NumberFormat(locale);
|
|
161
|
+
const select = pluralSelector(locale);
|
|
162
|
+
const numbers = typeof Intl !== "undefined" && typeof Intl.NumberFormat === "function" ? new Intl.NumberFormat(locale) : undefined;
|
|
113
163
|
const fill = (template, params, formatNumbers) => template.replace(/\{(\w+)\}/g, (placeholder, name) => {
|
|
114
164
|
const value = params?.[name];
|
|
115
165
|
if (value === undefined)
|
|
116
166
|
return placeholder;
|
|
117
|
-
return typeof value === "number" && formatNumbers ? numbers.format(value) : String(value);
|
|
167
|
+
return typeof value === "number" && formatNumbers && numbers ? numbers.format(value) : String(value);
|
|
118
168
|
});
|
|
119
169
|
return (key, params) => {
|
|
120
170
|
const leaf = leafAt(messages, key) ?? leafAt(fallback, key);
|
|
@@ -123,7 +173,7 @@ function createTranslator(locale, messages, fallback) {
|
|
|
123
173
|
if (typeof leaf === "string")
|
|
124
174
|
return fill(leaf, params, false);
|
|
125
175
|
const count = params?.count;
|
|
126
|
-
const category = typeof count === "number" ?
|
|
176
|
+
const category = typeof count === "number" ? select(count) : "other";
|
|
127
177
|
const chosen = leaf[category];
|
|
128
178
|
return fill(typeof chosen === "string" && chosen ? chosen : leaf.other === undefined ? key : String(leaf.other), params, true);
|
|
129
179
|
};
|
|
@@ -207,7 +257,9 @@ export {
|
|
|
207
257
|
LOCALE_NAMES,
|
|
208
258
|
alternates,
|
|
209
259
|
checkCatalog,
|
|
260
|
+
cldrPluralCategory,
|
|
210
261
|
createTranslator,
|
|
262
|
+
defaultLocaleRedirect,
|
|
211
263
|
defineCatalog,
|
|
212
264
|
isLocale,
|
|
213
265
|
localeFromPath,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jgb-solutions/kit",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
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",
|