@jgb-solutions/kit 0.4.3 → 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 +5 -0
- package/README.md +1 -0
- package/dist/i18n.d.ts +9 -0
- package/dist/i18n.js +49 -4
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
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
|
+
|
|
5
10
|
## 0.4.3
|
|
6
11
|
|
|
7
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.
|
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>.
|
package/dist/i18n.d.ts
CHANGED
|
@@ -42,6 +42,7 @@ export declare function suggestLocale(opts: {
|
|
|
42
42
|
current: Locale;
|
|
43
43
|
chosen: string | null | undefined;
|
|
44
44
|
}): Locale | null;
|
|
45
|
+
declare const PLURAL_CATEGORIES: readonly ["zero", "one", "two", "few", "many", "other"];
|
|
45
46
|
/** A plural leaf: `one` and `other` always, the other CLDR categories when the language needs them. */
|
|
46
47
|
export type PluralMessage = {
|
|
47
48
|
one: string;
|
|
@@ -94,6 +95,14 @@ export type Translator<M> = <K extends MessageKey<M>>(key: K, ...args: Translate
|
|
|
94
95
|
* does the same for object literals; this helper just names the intent and keeps the typed result.
|
|
95
96
|
*/
|
|
96
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;
|
|
97
106
|
export declare function createTranslator<M extends Messages>(locale: Locale, messages: M, fallback?: M): Translator<M>;
|
|
98
107
|
/**
|
|
99
108
|
* A translation typed by its source: `createTranslator<typeof en>('fr', fr)` (a website sends only the
|
package/dist/i18n.js
CHANGED
|
@@ -113,14 +113,58 @@ function leafAt(messages, key) {
|
|
|
113
113
|
return node;
|
|
114
114
|
return;
|
|
115
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
|
+
}
|
|
116
160
|
function createTranslator(locale, messages, fallback) {
|
|
117
|
-
const
|
|
118
|
-
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;
|
|
119
163
|
const fill = (template, params, formatNumbers) => template.replace(/\{(\w+)\}/g, (placeholder, name) => {
|
|
120
164
|
const value = params?.[name];
|
|
121
165
|
if (value === undefined)
|
|
122
166
|
return placeholder;
|
|
123
|
-
return typeof value === "number" && formatNumbers ? numbers.format(value) : String(value);
|
|
167
|
+
return typeof value === "number" && formatNumbers && numbers ? numbers.format(value) : String(value);
|
|
124
168
|
});
|
|
125
169
|
return (key, params) => {
|
|
126
170
|
const leaf = leafAt(messages, key) ?? leafAt(fallback, key);
|
|
@@ -129,7 +173,7 @@ function createTranslator(locale, messages, fallback) {
|
|
|
129
173
|
if (typeof leaf === "string")
|
|
130
174
|
return fill(leaf, params, false);
|
|
131
175
|
const count = params?.count;
|
|
132
|
-
const category = typeof count === "number" ?
|
|
176
|
+
const category = typeof count === "number" ? select(count) : "other";
|
|
133
177
|
const chosen = leaf[category];
|
|
134
178
|
return fill(typeof chosen === "string" && chosen ? chosen : leaf.other === undefined ? key : String(leaf.other), params, true);
|
|
135
179
|
};
|
|
@@ -213,6 +257,7 @@ export {
|
|
|
213
257
|
LOCALE_NAMES,
|
|
214
258
|
alternates,
|
|
215
259
|
checkCatalog,
|
|
260
|
+
cldrPluralCategory,
|
|
216
261
|
createTranslator,
|
|
217
262
|
defaultLocaleRedirect,
|
|
218
263
|
defineCatalog,
|
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",
|