diff --git a/apps/meteor/app/utils/lib/i18n.ts b/apps/meteor/app/utils/lib/i18n.ts index 13d5c667709d..ed9953bb70a9 100644 --- a/apps/meteor/app/utils/lib/i18n.ts +++ b/apps/meteor/app/utils/lib/i18n.ts @@ -5,9 +5,31 @@ import { isObject } from '../../../lib/utils/isObject'; export const i18n = i18next.use(sprintf); -export const addSprinfToI18n = function (t: (typeof i18n)['t']) { +type HasCounter = { + counter?: number; +}; + +const hasCounterProperty = (obj: any): obj is HasCounter => obj?.hasOwnProperty('counter') && typeof obj.counter === 'number'; + +export const addSprinfToI18n = function (t: (typeof i18n)['t'], i18nInstance?: typeof i18n) { return function (key: string, ...replaces: any): string { - if (replaces[0] === undefined || isObject(replaces[0])) { + if (replaces[0] === undefined) { + return t(key, ...replaces); + } + + if (isObject(replaces[0])) { + if (!hasCounterProperty(replaces[0])) { + return t(key, ...replaces); + } + + const pluralKey = `${key}_plural`; + const pluralKeyExists = i18nInstance?.exists(pluralKey); + const counter = replaces[0]?.counter; + + if (counter !== undefined && pluralKeyExists) { + return counter === 1 ? t(key, ...replaces) : t(pluralKey, ...replaces); + } + return t(key, ...replaces); } diff --git a/apps/meteor/client/providers/TranslationProvider.tsx b/apps/meteor/client/providers/TranslationProvider.tsx index 2cf47066c4e4..2601e789c972 100644 --- a/apps/meteor/client/providers/TranslationProvider.tsx +++ b/apps/meteor/client/providers/TranslationProvider.tsx @@ -293,7 +293,7 @@ const TranslationProviderInner = ({ loadLanguage: async (language: string): Promise => { i18n.changeLanguage(language).then(() => applyCustomTranslations()); }, - translate: Object.assign(addSprinfToI18n(t), { + translate: Object.assign(addSprinfToI18n(t, i18n), { has: ((key, options) => key && i18n.exists(key, options)) as TranslationContextValue['translate']['has'], }), }),