diff --git a/src/lib/currency.js b/src/lib/currency.js index b18ccdf0..44d960fc 100644 --- a/src/lib/currency.js +++ b/src/lib/currency.js @@ -3,10 +3,6 @@ import currencyConstants from '@/consts/currency.js'; import { isString, isNumber } from './common.js'; export function appendCurrencySymbol(value, currencyDisplayType, currencyCode, currencyName) { - if (!currencyDisplayType) { - return value; - } - if (isNumber(value)) { value = value.toString(); } @@ -15,8 +11,31 @@ export function appendCurrencySymbol(value, currencyDisplayType, currencyCode, c return value; } + const symbol = getAmountPrependAndAppendCurrencySymbol(currencyDisplayType, currencyCode, currencyName); + + if (!symbol) { + return value; + } + + const separator = currencyDisplayType.separator || ''; + + if (symbol.prependText) { + value = symbol.prependText + separator + value; + } + + if (symbol.appendText) { + value = value + separator + symbol.appendText; + } + + return value; +} + +export function getAmountPrependAndAppendCurrencySymbol(currencyDisplayType, currencyCode, currencyName) { + if (!currencyDisplayType) { + return null; + } + let symbol = ''; - let separator = currencyDisplayType.separator || ''; if (currencyDisplayType.symbol === currencyConstants.allCurrencyDisplaySymbol.Symbol) { const currencyInfo = currencyConstants.all[currencyCode]; @@ -35,10 +54,14 @@ export function appendCurrencySymbol(value, currencyDisplayType, currencyCode, c } if (currencyDisplayType.location === currencyConstants.allCurrencyDisplayLocation.BeforeAmount) { - return `${symbol}${separator}${value}`; + return { + prependText: symbol + }; } else if (currencyDisplayType.location === currencyConstants.allCurrencyDisplayLocation.AfterAmount) { - return `${value}${separator}${symbol}`; + return { + appendText: symbol + }; } else { - return value; + return null; } } diff --git a/src/lib/i18n.js b/src/lib/i18n.js index 599033df..bf775566 100644 --- a/src/lib/i18n.js +++ b/src/lib/i18n.js @@ -15,8 +15,6 @@ import { isString, isNumber, isBoolean, - getTextBefore, - getTextAfter, copyObjectTo, copyArrayTo } from './common.js'; @@ -48,7 +46,8 @@ import { } from './numeral.js'; import { - appendCurrencySymbol + appendCurrencySymbol, + getAmountPrependAndAppendCurrencySymbol } from './currency.js'; import { @@ -830,6 +829,21 @@ function getFormatedAmount(value, translateFn, userStore) { return formatAmount(value, numberFormatOptions); } +function getCurrentCurrencyDisplayType(translateFn, userStore) { + let currencyDisplayType = currencyConstants.allCurrencyDisplayTypeMap[userStore.currentUserCurrencyDisplayType]; + + if (!currencyDisplayType) { + const defaultCurrencyDisplayTypeName = translateFn('default.currencyDisplayType'); + currencyDisplayType = currencyConstants.allCurrencyDisplayType[defaultCurrencyDisplayTypeName]; + } + + if (!currencyDisplayType) { + currencyDisplayType = currencyConstants.defaultCurrencyDisplayType; + } + + return currencyDisplayType; +} + function getFormatedAmountWithCurrency(value, currencyCode, translateFn, userStore, settingsStore, notConvertValue, currencyDisplayType) { if (!isNumber(value) && !isString(value)) { return value; @@ -865,16 +879,7 @@ function getFormatedAmountWithCurrency(value, currencyCode, translateFn, userSto } if (!currencyDisplayType) { - currencyDisplayType = currencyConstants.allCurrencyDisplayTypeMap[userStore.currentUserCurrencyDisplayType]; - - if (!currencyDisplayType) { - const defaultCurrencyDisplayTypeName = translateFn('default.currencyDisplayType'); - currencyDisplayType = currencyConstants.allCurrencyDisplayType[defaultCurrencyDisplayTypeName]; - } - - if (!currencyDisplayType) { - currencyDisplayType = currencyConstants.defaultCurrencyDisplayType; - } + currencyDisplayType = getCurrentCurrencyDisplayType(translateFn, userStore); } const currencyName = getCurrencyName(currencyCode, translateFn); @@ -891,30 +896,10 @@ function getAdaptiveAmountRate(amount1, amount2, fromExchangeRate, toExchangeRat return getAdaptiveDisplayAmountRate(amount1, amount2, fromExchangeRate, toExchangeRate, numberFormatOptions); } -function getAmountPrependAndAppendText(currencyCode, translateFn, userStore, settingsStore) { - const placeholder = '***'; - const finalText = getFormatedAmountWithCurrency(placeholder, currencyCode, translateFn, userStore, settingsStore, true); - - if (!finalText) { - return null; - } - - let prependText = getTextBefore(finalText, placeholder); - - if (prependText) { - prependText = prependText.trim(); - } - - let appendText = getTextAfter(finalText, placeholder); - - if (appendText) { - appendText = appendText.trim(); - } - - return { - prependText: prependText, - appendText: appendText - }; +function getAmountPrependAndAppendText(currencyCode, userStore, settingsStore, translateFn) { + const currencyDisplayType = getCurrentCurrencyDisplayType(translateFn, userStore); + const currencyName = getCurrencyName(currencyCode, translateFn); + return getAmountPrependAndAppendCurrencySymbol(currencyDisplayType, currencyCode, currencyName); } function getAllAccountCategories(translateFn) { @@ -1444,7 +1429,7 @@ export function i18nFunctions(i18nGlobal) { formatAmountWithCurrency: (settingsStore, userStore, value, currencyCode) => getFormatedAmountWithCurrency(value, currencyCode, i18nGlobal.t, userStore, settingsStore), formatExchangeRateAmount: (userStore, value) => getFormatedExchangeRateAmount(value, i18nGlobal.t, userStore), getAdaptiveAmountRate: (userStore, amount1, amount2, fromExchangeRate, toExchangeRate) => getAdaptiveAmountRate(amount1, amount2, fromExchangeRate, toExchangeRate, i18nGlobal.t, userStore), - getAmountPrependAndAppendText: (settingsStore, userStore, currencyCode) => getAmountPrependAndAppendText(currencyCode, i18nGlobal.t, userStore, settingsStore), + getAmountPrependAndAppendText: (settingsStore, userStore, currencyCode) => getAmountPrependAndAppendText(currencyCode, userStore, settingsStore, i18nGlobal.t), getAllAccountCategories: () => getAllAccountCategories(i18nGlobal.t), getAllAccountTypes: () => getAllAccountTypes(i18nGlobal.t), getAllCategoricalChartTypes: () => getAllCategoricalChartTypes(i18nGlobal.t),