Skip to content

armgit / ente

Public Imported from GitHub
0

💚 End-to-end encrypted cloud for everything.

list join

Manav Rathi062bbdfa880f
diff --git a/web/packages/base/i18n.ts b/web/packages/base/i18n.ts
index 6949de2eef..140b82edd8 100644
--- a/web/packages/base/i18n.ts
+++ b/web/packages/base/i18n.ts
@@ -234,6 +234,7 @@ export const setLocaleInUse = async (locale: SupportedLocale) => {
};
let _numberFormat: Intl.NumberFormat | undefined;
+
/**
* Lazily created, cached, instance of NumberFormat used by
* {@link formattedNumber}.
@@ -253,6 +254,31 @@ const numberFormat = () =>
*/
export const formattedNumber = (value: number) => numberFormat().format(value);
+let _listJoinFormat: Intl.ListFormat | undefined;
+
+/**
+ * Lazily created, cached, instance of NumberFormat used by
+ * {@link formattedListJoin}.
+ *
+ * See: [Note: Changing locale causes a full reload].
+ */
+const listJoinFormat = () =>
+ (_listJoinFormat ??= new Intl.ListFormat(i18n.language, {
+ style: "narrow",
+ }));
+
+/**
+ * Return the given {@link items} joined together into a single string using an
+ * locale specific "comma like" separator.
+ *
+ * Usually this will just use a comma (plus space) as the list item separator,
+ * but depending on the locale it might use a different separator too.
+ *
+ * e.g. ["Foo", "Bar"] becomes "Foo, Bar" in "en-US" and "Foo、Bar" in "zh".
+ */
+export const formattedListJoin = (value: string[]) =>
+ listJoinFormat().format(value);
+
/**
* A no-op marker for strings that, for various reasons, pending addition to the
* translation dataset.