]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/helpers/i18n-utils.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / helpers / i18n-utils.ts
index bbfb1295943eea93ed57ed814d8a1852e3fa972e..b7d73d16bda40e3aa9b8d28d7bfd52fdc05ca6cd 100644 (file)
@@ -1,3 +1,5 @@
+import IntlMessageFormat from 'intl-messageformat'
+import { logger } from '@root-helpers/logger'
 import { environment } from '../../environments/environment'
 
 function isOnDevLocale () {
@@ -8,7 +10,31 @@ function getDevLocale () {
   return 'fr-FR'
 }
 
+function prepareIcu (icu: string) {
+  let alreadyWarned = false
+
+  try {
+    const msg = new IntlMessageFormat(icu, $localize.locale)
+
+    return (context: { [id: string]: number | string }, fallback: string) => {
+      try {
+        return msg.format(context) as string
+      } catch (err) {
+        if (!alreadyWarned) logger.warn(`Cannot format ICU ${icu}.`, err)
+
+        alreadyWarned = true
+        return fallback
+      }
+    }
+  } catch (err) {
+    logger.warn(`Cannot build intl message ${icu}.`, err)
+
+    return (_context: unknown, fallback: string) => fallback
+  }
+}
+
 export {
   getDevLocale,
+  prepareIcu,
   isOnDevLocale
 }