« get me outta code hell

use Intl.PluralRules for better num pluralization - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <towerofnix@gmail.com>2021-03-10 10:18:07 -0400
committer(quasar) nebula <towerofnix@gmail.com>2021-03-10 10:18:07 -0400
commit8f0842ab522b238bf3aacd4d6bab2c6d384469bf (patch)
tree1b611eec7c671ea3f7a316caca15671afe05e102
parentc21054c17e5736299434d4aa9b0729b757e982fa (diff)
use Intl.PluralRules for better num pluralization
-rw-r--r--strings-default.json82
-rwxr-xr-xupd8.js10
2 files changed, 66 insertions, 26 deletions
diff --git a/strings-default.json b/strings-default.json
index d611758c..5a3d9364 100644
--- a/strings-default.json
+++ b/strings-default.json
@@ -1,37 +1,75 @@
 {
     "meta.languageCode": "en",
     "count.tracks": "{TRACKS}",
-    "count.tracks.withUnit.singular": "{TRACKS} track",
-    "count.tracks.withUnit.plural": "{TRACKS} tracks",
+    "count.tracks.withUnit.zero": "",
+    "count.tracks.withUnit.one": "{TRACKS} track",
+    "count.tracks.withUnit.two": "",
+    "count.tracks.withUnit.few": "",
+    "count.tracks.withUnit.many": "",
+    "count.tracks.withUnit.other": "{TRACKS} tracks",
     "count.albums": "{ALBUMS}",
-    "count.albums.withUnit.singular": "{ALBUMS} album",
-    "count.albums.withUnit.plural": "{ALBUMS} albums",
+    "count.albums.withUnit.zero": "",
+    "count.albums.withUnit.one": "{ALBUMS} album",
+    "count.albums.withUnit.two": "",
+    "count.albums.withUnit.two": "",
+    "count.albums.withUnit.few": "",
+    "count.albums.withUnit.many": "",
+    "count.albums.withUnit.other": "{ALBUMS} albums",
     "count.commentaryEntries": "{ENTRIES}",
-    "count.commentaryEntries.withUnit.singular": "{ENTRIES} entry",
-    "count.commentaryEntries.withUnit.plural": "{ENTRIES} entries",
+    "count.commentaryEntries.withUnit.zero": "",
+    "count.commentaryEntries.withUnit.one": "{ENTRIES} entry",
+    "count.commentaryEntries.withUnit.two": "",
+    "count.commentaryEntries.withUnit.few": "",
+    "count.commentaryEntries.withUnit.many": "",
+    "count.commentaryEntries.withUnit.other": "{ENTRIES} entries",
     "count.contributions": "{CONTRIBUTIONS}",
-    "count.contributions.withUnit.singular": "{CONTRIBUTIONS} contribution",
-    "count.contributions.withUnit.plural": "{CONTRIBUTIONS} contributions",
+    "count.contributions.withUnit.zero": "",
+    "count.contributions.withUnit.one": "{CONTRIBUTIONS} contribution",
+    "count.contributions.withUnit.two": "",
+    "count.contributions.withUnit.few": "",
+    "count.contributions.withUnit.many": "",
+    "count.contributions.withUnit.other": "{CONTRIBUTIONS} contributions",
     "count.coverArts": "{COVER_ARTS}",
-    "count.coverArts.withUnit.singular": "{COVER_ARTS} cover art",
-    "count.coverArts.withUnit.plural": "{COVER_ARTS} cover arts",
+    "count.coverArts.withUnit.zero": "",
+    "count.coverArts.withUnit.one": "{COVER_ARTS} cover art",
+    "count.coverArts.withUnit.two": "",
+    "count.coverArts.withUnit.few": "",
+    "count.coverArts.withUnit.many": "",
+    "count.coverArts.withUnit.other": "{COVER_ARTS} cover arts",
     "count.timesReferenced": "{TIMES_REFERENCED}",
-    "count.timesReferenced.withUnit.singular": "{TIMES_REFERENCED} time referenced",
-    "count.timesReferenced.withUnit.plural": "{TIMES_REFERENCED} times referenced",
+    "count.timesReferenced.withUnit.zero": "",
+    "count.timesReferenced.withUnit.one": "{TIMES_REFERENCED} time referenced",
+    "count.timesReferenced.withUnit.two": "",
+    "count.timesReferenced.withUnit.few": "",
+    "count.timesReferenced.withUnit.many": "",
+    "count.timesReferenced.withUnit.other": "{TIMES_REFERENCED} times referenced",
+    "count.words": "{WORDS}",
+    "count.words.thousand": "{WORDS}k",
+    "count.words.withUnit.zero": "",
+    "count.words.withUnit.one": "{WORDS} word",
+    "count.words.withUnit.two": "",
+    "count.words.withUnit.few": "",
+    "count.words.withUnit.many": "",
+    "count.words.withUnit.other": "{WORDS} words",
     "count.timesUsed": "{TIMES_USED}",
-    "count.timesUsed.withUnit.singular": "used {TIMES_USED} time",
-    "count.timesUsed.withUnit.plural": "used {TIMES_USED} times",
+    "count.timesUsed.withUnit.zero": "",
+    "count.timesUsed.withUnit.one": "used {TIMES_USED} time",
+    "count.timesUsed.withUnit.two": "",
+    "count.timesUsed.withUnit.few": "",
+    "count.timesUsed.withUnit.many": "",
+    "count.timesUsed.withUnit.other": "used {TIMES_USED} times",
+    "count.index.zero": "",
+    "count.index.one": "{INDEX}st",
+    "count.index.two": "{INDEX}nd",
+    "count.index.few": "{INDEX}rd",
+    "count.index.many": "",
+    "count.index.other": "{INDEX}th",
     "count.duration.hours": "{HOURS}:{MINUTES}:{SECONDS}",
     "count.duration.hours.withUnit": "{HOURS}:{MINUTES}:{SECONDS} hours",
     "count.duration.minutes": "{MINUTES}:{SECONDS}",
     "count.duration.minutes.withUnit": "{MINUTES}:{SECONDS} minutes",
     "count.duration.approximate": "~{DURATION}",
     "count.duration.missing": "_:__",
-    "count.index": "#{INDEX}",
-    "count.words": "{WORDS}",
-    "count.words.thousand": "{WORDS}k",
-    "count.words.withUnit.singular": "{WORDS} word",
-    "count.words.withUnit.plural": "{WORDS} words",
     "releaseInfo.by": "By {ARTISTS}.",
     "releaseInfo.coverArtBy": "Cover art by {ARTISTS}.",
     "releaseInfo.wallpaperArtBy": "Wallpaper art by {ARTISTS}.",
@@ -68,9 +106,9 @@
     "misc.alt.artistAvatar": "artist avatar",
     "misc.alt.flashArt": "flash art",
     "misc.chronology.seeArtistPages": "(See artist pages for chronology info!)",
-    "misc.chronology.heading.coverArt": "Cover art {INDEX} by {ARTIST}",
-    "misc.chronology.heading.flash": "Flash/game {INDEX} by {ARTIST}",
-    "misc.chronology.heading.track": "Track {INDEX} by {ARTIST}",
+    "misc.chronology.heading.coverArt": "{INDEX} cover art by {ARTIST}",
+    "misc.chronology.heading.flash": "{INDEX} flash/game by {ARTIST}",
+    "misc.chronology.heading.track": "{INDEX} track by {ARTIST}",
     "misc.external.bandcamp": "Bandcamp",
     "misc.external.bandcamp.domain": "Bandcamp ({DOMAIN})",
     "misc.external.deviantart": "DeviantArt",
diff --git a/upd8.js b/upd8.js
index a54a8867..97ee8bf7 100755
--- a/upd8.js
+++ b/upd8.js
@@ -494,6 +494,10 @@ function genStrings(stringsJSON, defaultJSON = null) {
             conjunction: new Intl.ListFormat(code, {type: 'conjunction'}),
             disjunction: new Intl.ListFormat(code, {type: 'disjunction'}),
             unit: new Intl.ListFormat(code, {type: 'unit'})
+        },
+        plural: {
+            cardinal: new Intl.PluralRules(code, {type: 'cardinal'}),
+            ordinal: new Intl.PluralRules(code, {type: 'ordinal'})
         }
     };
 
@@ -523,9 +527,7 @@ function genStrings(stringsJSON, defaultJSON = null) {
 
 const countHelper = (stringKey, argName = stringKey) => (value, {strings, unit = false}) => strings(
     (unit
-        ? `count.${stringKey}.withUnit` + (value === 1
-            ? '.singular'
-            : '.plural')
+        ? `count.${stringKey}.withUnit.` + strings.intl.plural.cardinal.select(value)
         : `count.${stringKey}`),
     {[argName]: strings.intl.number.format(value)});
 
@@ -568,7 +570,7 @@ const count = {
     },
 
     index: (value, {strings}) => {
-        return strings('count.index', {index: value});
+        return strings('count.index.' + strings.intl.plural.ordinal.select(value), {index: value});
     },
 
     number: value => strings.intl.number.format(value),