@jgb-solutions/kit 0.4.3 → 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/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 rules = new Intl.PluralRules(locale);
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" ? rules.select(count) : "other";
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",
3
+ "version": "0.5.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",
@@ -57,6 +57,18 @@
57
57
  "types": "./dist/auth/rules.d.ts",
58
58
  "import": "./dist/auth/rules.js"
59
59
  },
60
+ "./auth/server": {
61
+ "types": "./dist/auth/server.d.ts",
62
+ "import": "./dist/auth/server.js"
63
+ },
64
+ "./auth/cache": {
65
+ "types": "./dist/auth/cache.d.ts",
66
+ "import": "./dist/auth/cache.js"
67
+ },
68
+ "./auth/redis": {
69
+ "types": "./dist/auth/redis.d.ts",
70
+ "import": "./dist/auth/redis.js"
71
+ },
60
72
  "./auth/expo": {
61
73
  "types": "./dist/auth/expo.d.ts",
62
74
  "import": "./dist/auth/expo.js"