@stacksjs/i18n 0.75.422 → 0.75.423

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.
@@ -3,7 +3,14 @@
3
3
  */
4
4
  export declare function getPluralCategory(n: number, locale: string): PluralCategory;
5
5
  /**
6
- * Get the plural rule function for a locale
6
+ * Get the plural rule function for a locale: one added with
7
+ * {@link addPluralRule}, else the runtime's CLDR rules (`Intl.PluralRules`)
8
+ * for the full locale (`pt` and `pt-PT` differ), else `other` for everything.
9
+ *
10
+ * The rules used to be a hand-written table of 33 languages. 19 others were
11
+ * missing from it and said `other` even for 1 (Catalan, Croatian, Serbian,
12
+ * Bulgarian, `nb` for Norwegian...), and several it had disagreed with CLDR
13
+ * (Hebrew's `two`, Latvian's `zero`, Portuguese 0, most fractions).
7
14
  */
8
15
  export declare function getPluralRule(locale: string): PluralRule;
9
16
  /**
@@ -17,19 +24,30 @@ export declare function getPluralRule(locale: string): PluralRule;
17
24
  */
18
25
  export declare function parsePluralForms(str: string, separator?: string): Map<PluralCategory, string>;
19
26
  /**
20
- * Select the appropriate plural form for a count
27
+ * Select the appropriate plural form for a count.
28
+ *
29
+ * A count of 0 takes the string's zero form when it has one: the first of
30
+ * `"no items | one item | {count} items"`, which is what a three-part string
31
+ * is for. It used to go by CLDR category alone, and English has no `zero`
32
+ * category, so 0 read "0 items" and the zero form was never shown. In a
33
+ * language whose CLDR rules have a `zero` category (Latvian, Arabic, Welsh)
34
+ * 0 is in it anyway, so nothing changes there.
21
35
  */
22
36
  export declare function selectPluralForm(forms: Map<PluralCategory, string>, count: number, locale: string): string;
23
37
  /**
24
- * Get all supported locales for pluralization
38
+ * Languages given their own rule with {@link addPluralRule}. Every language
39
+ * the runtime has CLDR plural data for is supported as well; ask
40
+ * {@link hasPluralRule} about one.
25
41
  */
26
42
  export declare function getSupportedPluralLocales(): string[];
27
43
  /**
28
- * Check if a locale has custom plural rules
44
+ * Whether `locale` has plural rules: its own from {@link addPluralRule}, or
45
+ * the runtime's CLDR data for its language.
29
46
  */
30
47
  export declare function hasPluralRule(locale: string): boolean;
31
48
  /**
32
- * Add a custom plural rule for a locale
49
+ * Add a custom plural rule for a locale's language. It wins over the
50
+ * runtime's CLDR rules for every locale of that language.
33
51
  */
34
52
  export declare function addPluralRule(locale: string, rule: PluralRule): void;
35
53
  /**
@@ -1 +1 @@
1
- export function getPluralCategory(n,locale){return getPluralRule(locale)(n)}export function getPluralRule(locale){const lang=locale.split("-")[0]?.toLowerCase()??locale.toLowerCase(),rule=pluralRules[lang];if(rule)return rule;return()=>"other"}export function parsePluralForms(str,separator="|"){const parts=str.split(separator).map((s)=>s.trim()),forms=new Map;if(parts.length===1)forms.set("other",parts[0]??"");else if(parts.length===2){forms.set("one",parts[0]??"");forms.set("other",parts[1]??"")}else if(parts.length===3){forms.set("zero",parts[0]??"");forms.set("one",parts[1]??"");forms.set("other",parts[2]??"")}else if(parts.length>=4){const categories=["zero","one","two","few","many","other"];for(let i=0;i<parts.length&&i<categories.length;i++){const category=categories[i],part=parts[i];if(category!==void 0&&part!==void 0)forms.set(category,part)}}return forms}export function selectPluralForm(forms,count,locale){const category=getPluralCategory(count,locale);if(forms.has(category))return forms.get(category);if(forms.has("other"))return forms.get("other");const first=forms.values().next();return first.done?"":first.value}const pluralRules={en:(n)=>n===1?"one":"other",de:(n)=>n===1?"one":"other",nl:(n)=>n===1?"one":"other",es:(n)=>n===1?"one":"other",it:(n)=>n===1?"one":"other",pt:(n)=>n===1?"one":"other",sv:(n)=>n===1?"one":"other",da:(n)=>n===1?"one":"other",no:(n)=>n===1?"one":"other",fi:(n)=>n===1?"one":"other",el:(n)=>n===1?"one":"other",he:(n)=>n===1?"one":"other",hu:(n)=>n===1?"one":"other",tr:(n)=>n===1?"one":"other",zh:()=>"other",ja:()=>"other",ko:()=>"other",vi:()=>"other",th:()=>"other",id:()=>"other",ms:()=>"other",fr:(n)=>n===0||n===1?"one":"other",ru:(n)=>{const mod10=n%10,mod100=n%100;if(mod10===1&&mod100!==11)return"one";if(mod10>=2&&mod10<=4&&(mod100<12||mod100>14))return"few";if(mod10===0||mod10>=5&&mod10<=9||mod100>=11&&mod100<=14)return"many";return"other"},uk:(n)=>{const mod10=n%10,mod100=n%100;if(mod10===1&&mod100!==11)return"one";if(mod10>=2&&mod10<=4&&(mod100<12||mod100>14))return"few";if(mod10===0||mod10>=5&&mod10<=9||mod100>=11&&mod100<=14)return"many";return"other"},pl:(n)=>{const mod10=n%10,mod100=n%100;if(n===1)return"one";if(mod10>=2&&mod10<=4&&(mod100<12||mod100>14))return"few";if(mod10===0||mod10===1||mod10>=5&&mod10<=9||mod100>=12&&mod100<=14)return"many";return"other"},cs:(n)=>{if(n===1)return"one";if(n>=2&&n<=4)return"few";return"other"},sk:(n)=>{if(n===1)return"one";if(n>=2&&n<=4)return"few";return"other"},ar:(n)=>{if(n===0)return"zero";if(n===1)return"one";if(n===2)return"two";const mod100=n%100;if(mod100>=3&&mod100<=10)return"few";if(mod100>=11&&mod100<=99)return"many";return"other"},cy:(n)=>{if(n===0)return"zero";if(n===1)return"one";if(n===2)return"two";if(n===3)return"few";if(n===6)return"many";return"other"},sl:(n)=>{const mod100=n%100;if(mod100===1)return"one";if(mod100===2)return"two";if(mod100===3||mod100===4)return"few";return"other"},ga:(n)=>{if(n===1)return"one";if(n===2)return"two";if(n>=3&&n<=6)return"few";if(n>=7&&n<=10)return"many";return"other"},lt:(n)=>{const mod10=n%10,mod100=n%100;if(mod10===1&&(mod100<11||mod100>19))return"one";if(mod10>=2&&mod10<=9&&(mod100<11||mod100>19))return"few";return"other"},lv:(n)=>{const mod10=n%10,mod100=n%100;if(n===0)return"zero";if(mod10===1&&mod100!==11)return"one";return"other"},ro:(n)=>{if(n===1)return"one";const mod100=n%100;if(n===0||mod100>=1&&mod100<=19)return"few";return"other"},hi:(n)=>n===0||n===1?"one":"other"};export function getSupportedPluralLocales(){return Object.keys(pluralRules)}export function hasPluralRule(locale){return(locale.split("-")[0]?.toLowerCase()??locale.toLowerCase())in pluralRules}export function addPluralRule(locale,rule){const lang=locale.split("-")[0]?.toLowerCase()??locale.toLowerCase();pluralRules[lang]=rule}
1
+ export function getPluralCategory(n,locale){return getPluralRule(locale)(n)}const customRules={},runtimeRules=new Map;function baseLanguage(locale){return locale.split("-")[0]?.toLowerCase()??locale.toLowerCase()}function runtimeRule(locale){if(!runtimeRules.has(locale)){let rules=null;try{if(Intl.PluralRules.supportedLocalesOf([locale]).length)rules=new Intl.PluralRules(locale)}catch{}runtimeRules.set(locale,rules)}return runtimeRules.get(locale)??null}export function getPluralRule(locale){const custom=customRules[baseLanguage(locale)];if(custom)return custom;const rules=runtimeRule(locale)??runtimeRule(baseLanguage(locale));if(rules)return(n)=>rules.select(n);return()=>"other"}export function parsePluralForms(str,separator="|"){const parts=str.split(separator).map((s)=>s.trim()),forms=new Map;if(parts.length===1)forms.set("other",parts[0]??"");else if(parts.length===2){forms.set("one",parts[0]??"");forms.set("other",parts[1]??"")}else if(parts.length===3){forms.set("zero",parts[0]??"");forms.set("one",parts[1]??"");forms.set("other",parts[2]??"")}else if(parts.length>=4){const categories=["zero","one","two","few","many","other"];for(let i=0;i<parts.length&&i<categories.length;i++){const category=categories[i],part=parts[i];if(category!==void 0&&part!==void 0)forms.set(category,part)}}return forms}export function selectPluralForm(forms,count,locale){if(count===0&&forms.has("zero"))return forms.get("zero");const category=getPluralCategory(count,locale);if(forms.has(category))return forms.get(category);if(forms.has("other"))return forms.get("other");const first=forms.values().next();return first.done?"":first.value}export function getSupportedPluralLocales(){return Object.keys(customRules)}export function hasPluralRule(locale){const lang=baseLanguage(locale);return lang in customRules||runtimeRule(lang)!==null}export function addPluralRule(locale,rule){customRules[baseLanguage(locale)]=rule}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stacksjs/i18n",
3
- "version": "0.75.422",
3
+ "version": "0.75.423",
4
4
  "description": "Internationalization system for Stacks.js applications",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -36,13 +36,13 @@
36
36
  "prepublishOnly": "bun run build"
37
37
  },
38
38
  "dependencies": {
39
- "@stacksjs/config": "0.75.422",
40
- "@stacksjs/logging": "0.75.422",
41
- "@stacksjs/path": "0.75.422",
39
+ "@stacksjs/config": "0.75.423",
40
+ "@stacksjs/logging": "0.75.423",
41
+ "@stacksjs/path": "0.75.423",
42
42
  "@stacksjs/ts-i18n": "^0.1.11"
43
43
  },
44
44
  "devDependencies": {
45
- "@stacksjs/development": "0.75.422"
45
+ "@stacksjs/development": "0.75.423"
46
46
  },
47
47
  "publishConfig": {
48
48
  "exports": {