Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Unify number localization strategy across the app. (#770)

sarayourfriende4784363e632
diff --git a/src/composables/use-get-locale-formatted-number.js b/src/composables/use-get-locale-formatted-number.js
new file mode 100644
index 000000000..26046463d
--- /dev/null
+++ b/src/composables/use-get-locale-formatted-number.js
@@ -0,0 +1,40 @@
+import { useContext } from '@nuxtjs/composition-api'
+
+const WESTERN_ARABIC_NUMERALS = [
+ '0',
+ '1',
+ '2',
+ '3',
+ '4',
+ '5',
+ '6',
+ '7',
+ '8',
+ '9',
+]
+
+/**
+ * Guards number formatting to prevent using Eastern Arabic Numerals,
+ * instead always using Western Arabic Numerals but still respecting
+ * the locale preferences for delimiters.
+ */
+export const useGetLocaleFormattedNumber = () => {
+ const { i18n } = useContext()
+
+ /**
+ * @param {number} n The number to format
+ */
+ return (n) => {
+ let { locale } = i18n
+
+ const testFormat = n.toLocaleString(locale)
+
+ if (
+ WESTERN_ARABIC_NUMERALS.some((numeral) => testFormat.includes(numeral))
+ ) {
+ return testFormat
+ }
+
+ return n.toLocaleString('en')
+ }
+}
diff --git a/src/composables/use-i18n-utilities.js b/src/composables/use-i18n-utilities.js
index aa12b4b20..5e392d4c2 100644
--- a/src/composables/use-i18n-utilities.js
+++ b/src/composables/use-i18n-utilities.js
@@ -1,4 +1,5 @@
import { useContext } from '@nuxtjs/composition-api'
+import { useGetLocaleFormattedNumber } from '~/composables/use-get-locale-formatted-number'
/**
* Not using dynamically-generated keys to ensure that
@@ -15,6 +16,8 @@ const i18nKeys = {
*/
export function useI18nResultsCount() {
const { i18n } = useContext()
+ const getLocaleFormattedNumber = useGetLocaleFormattedNumber()
+
/**
* @param {number} resultsCount
* @returns {string}
@@ -27,9 +30,7 @@ export function useI18nResultsCount() {
? 'more'
: 'result'
const fullKey = i18nKeys[countKey]
- const countLocale =
- i18n.localeProperties?.dir === 'rtl' ? 'en' : i18n.locale
- const localeCount = resultsCount.toLocaleString(countLocale)
+ const localeCount = getLocaleFormattedNumber(resultsCount)
return i18n.tc(fullKey, resultsCount, { localeCount })
}
return {
diff --git a/src/pages/sources.vue b/src/pages/sources.vue
index 85ae25bc6..1471c9041 100644
--- a/src/pages/sources.vue
+++ b/src/pages/sources.vue
@@ -133,7 +133,7 @@
</a>
</td>
<td class="number-cell font-semibold">
- {{ getProviderMediaCount(imageProvider.media_count || 0) }}
+ {{ getLocaleFormattedNumber(imageProvider.media_count || 0) }}
</td>
</tr>
</tbody>
@@ -146,8 +146,7 @@
import sortBy from 'lodash.sortby'
import { mapState } from 'vuex'
import { PROVIDER } from '~/constants/store-modules'
-
-const ARABIC_NUMERAL_LOCALES = ['ar', 'fa', 'ur', 'ckb', 'ps']
+import { useGetLocaleFormattedNumber } from '~/composables/use-get-locale-formatted-number'
const SourcePage = {
name: 'source-page',
@@ -159,6 +158,11 @@ const SourcePage = {
},
}
},
+ setup() {
+ const getLocaleFormattedNumber = useGetLocaleFormattedNumber()
+
+ return { getLocaleFormattedNumber }
+ },
computed: {
...mapState(PROVIDER, ['imageProviders']),
sortedProviders() {
@@ -167,20 +171,6 @@ const SourcePage = {
},
},
methods: {
- /**
- * @param {number} mediaCount
- * @return {string} Localized media count
- */
- getProviderMediaCount(mediaCount = 0) {
- let locale = this.$i18n.locale
- if (ARABIC_NUMERAL_LOCALES.some((l) => locale.startsWith(l))) {
- // most sites with RTL language with numbers continue to use Western Arabic Numerals whereas `toLocaleString` will use Eastern Arabic Numerals for Arabic and Hebrew by default
- // Prevent formatting using Eastern Arabic Numerals and use `en-GB` to match the most common decimal and thousands delimiters for those regions
- // https://en.wikipedia.org/wiki/Decimal_separator#Countries_using_decimal_point
- locale = 'en-GB'
- }
- return mediaCount.toLocaleString(locale)
- },
sortTable(field) {
let direction = 'asc'
if (field === this.sort.field) {
diff --git a/test/unit/specs/composables/use-get-locale-formatted-number.spec.js b/test/unit/specs/composables/use-get-locale-formatted-number.spec.js
new file mode 100644
index 000000000..f2f27ec46
--- /dev/null
+++ b/test/unit/specs/composables/use-get-locale-formatted-number.spec.js
@@ -0,0 +1,61 @@
+import Vue from 'vue'
+import { render } from '@testing-library/vue'
+import VueI18n from 'vue-i18n'
+import messages from '~/locales/en.json'
+
+import { useGetLocaleFormattedNumber } from '~/composables/use-get-locale-formatted-number'
+
+const TestWrapper = Vue.component('TestWrapper', {
+ props: ['n'],
+ setup() {
+ const getLocaleFormattedNumber = useGetLocaleFormattedNumber()
+
+ return { getLocaleFormattedNumber }
+ },
+ template: `<div>{{ getLocaleFormattedNumber(n) }}</div>`,
+})
+
+describe('use-get-locale-formatted-number', () => {
+ const getMockedI18n = (locale) => ({
+ $nuxt: {
+ context: {
+ i18n: new VueI18n({
+ locale,
+ fallbackLocale: 'en',
+ messages: { en: messages },
+ }),
+ },
+ },
+ })
+
+ it.each(['ar', 'fa', 'ckb', 'ps'])(
+ 'should use en formatting for locales like %s that use Eastern Arabic Numerals',
+ (locale) => {
+ const { container } = render(TestWrapper, {
+ mocks: getMockedI18n(locale),
+ props: { n: 1000.01 },
+ })
+
+ expect(container.firstChild.textContent).toEqual(
+ (1000.01).toLocaleString('en')
+ )
+ expect(container.firstChild.textContent).not.toEqual(
+ (1000.01).toLocaleString(locale)
+ )
+ }
+ )
+
+ it.each(['pt-br', 'en-us', 'en-gb', 'ru'])(
+ 'should use the default formatting for locales not matching eastern arabic numeral locales like %s',
+ (locale) => {
+ const { container } = render(TestWrapper, {
+ mocks: getMockedI18n(locale),
+ props: { n: 1000.01 },
+ })
+
+ expect(container.firstChild.textContent).toEqual(
+ (1000.01).toLocaleString(locale)
+ )
+ }
+ )
+})
diff --git a/test/unit/specs/composables/use-i18n-utilities.spec.js b/test/unit/specs/composables/use-i18n-utilities.spec.js
index 204e0277c..09a824465 100644
--- a/test/unit/specs/composables/use-i18n-utilities.spec.js
+++ b/test/unit/specs/composables/use-i18n-utilities.spec.js
@@ -7,8 +7,8 @@ jest.mock('@nuxtjs/composition-api', () => ({
resultsCount,
localeCount,
}),
+ locale: 'en',
},
- locale: 'en',
}),
}))
diff --git a/tsconfig.json b/tsconfig.json
index 48f164f6f..796d64c52 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -24,7 +24,7 @@
/* Module Resolution Options */
"moduleResolution": "node",
"resolveJsonModule": true,
- "typeRoots": ["./node_modules/@types"],
+ "typeRoots": ["./typings", "./node_modules/@types"],
"paths": {
"~/*": ["./src/*"],
"~~/*": ["./*"]
@@ -35,6 +35,7 @@
"src/composables/types.js",
"src/composables/use-event-listener-outside.js",
"src/composables/use-active-audio.js",
+ "src/composables/use-get-locale-formatted-number.js",
"src/utils/key-codes.js"
]
}
diff --git a/typings/nuxt__types/index.d.ts b/typings/nuxt__types/index.d.ts
new file mode 100644
index 000000000..96d701fa9
--- /dev/null
+++ b/typings/nuxt__types/index.d.ts
@@ -0,0 +1,8 @@
+import '@nuxt/types'
+import type { IVueI18n } from 'vue-i18n'
+
+declare module '@nuxt/types' {
+ export interface Context {
+ i18n: IVueI18n
+ }
+}