From 8e6aece9aecafd331657403936133ceee2c0ea3f Mon Sep 17 00:00:00 2001 From: MaysWind Date: Fri, 23 Jun 2023 21:19:24 +0800 Subject: [PATCH] code refactor --- src/lib/i18n.js | 30 ++++++++++++++++++++-- src/views/mobile/SignupPage.vue | 21 +++++++-------- src/views/mobile/users/UserProfilePage.vue | 9 +++---- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/lib/i18n.js b/src/lib/i18n.js index e7b4067f..f1d11304 100644 --- a/src/lib/i18n.js +++ b/src/lib/i18n.js @@ -241,6 +241,14 @@ function getCurrentLanguageInfo(i18nGlobal) { return getDefaultLanguage(); } +function getDefaultCurrency(translateFn) { + return translateFn('default.currency'); +} + +function getDefaultFirstDayOfWeek(translateFn) { + return translateFn('default.firstDayOfWeek'); +} + function getAllLongMonthNames(translateFn) { return [ translateFn('datetime.January.long'), @@ -484,6 +492,21 @@ function getAllCurrencies(translateFn) { return allCurrencies; } +function getAllWeekDays(translateFn) { + const allWeekDays = []; + + for (let i = 0; i < datetime.allWeekDaysArray.length; i++) { + const weekDay = datetime.allWeekDaysArray[i]; + + allWeekDays.push({ + type: weekDay.type, + displayName: translateFn(`datetime.${weekDay.name}.long`) + }); + } + + return allWeekDays; +} + function getDisplayCurrency(value, currencyCode, notConvertValue, translateFn) { if (!isNumber(value) && !isString(value)) { return value; @@ -623,8 +646,8 @@ function setLanguage(i18nGlobal, locale, force) { services.setLocale(locale); document.querySelector('html').setAttribute('lang', locale); - const defaultCurrency = i18nGlobal.t('default.currency'); - const defaultFirstDayOfWeekName = i18nGlobal.t('default.firstDayOfWeek'); + const defaultCurrency = getDefaultCurrency(i18nGlobal.t); + const defaultFirstDayOfWeekName = getDefaultFirstDayOfWeek(i18nGlobal.t); let defaultFirstDayOfWeek = datetime.defaultFirstDayOfWeek; if (datetime.allWeekDays[defaultFirstDayOfWeekName]) { @@ -716,6 +739,8 @@ export function i18nFunctions(i18nGlobal) { getLanguageInfo: getLanguageInfo, getDefaultLanguage: getDefaultLanguage, getCurrentLanguageInfo: () => getCurrentLanguageInfo(i18nGlobal), + getDefaultCurrency: () => getDefaultCurrency(i18nGlobal.t), + getDefaultFirstDayOfWeek: () => getDefaultFirstDayOfWeek(i18nGlobal.t), getAllLongMonthNames: () => getAllLongMonthNames(i18nGlobal.t), getAllShortMonthNames: () => getAllShortMonthNames(i18nGlobal.t), getAllLongWeekdayNames: () => getAllLongWeekdayNames(i18nGlobal.t), @@ -743,6 +768,7 @@ export function i18nFunctions(i18nGlobal) { isShortTime24HourFormat: (userStore) => isShortTime24HourFormat(i18nGlobal.t, userStore.currentUserShortTimeFormat), getAllTimezones: (includeSystemDefault) => getAllTimezones(includeSystemDefault, i18nGlobal.t), getAllCurrencies: () => getAllCurrencies(i18nGlobal.t), + getAllWeekDays: () => getAllWeekDays(i18nGlobal.t), getDisplayCurrency: (value, currencyCode, notConvertValue) => getDisplayCurrency(value, currencyCode, notConvertValue, i18nGlobal.t), setLanguage: (locale, force) => setLanguage(i18nGlobal, locale, force), getTimezone: settings.getTimezone, diff --git a/src/views/mobile/SignupPage.vue b/src/views/mobile/SignupPage.vue index 5ae9d9ac..861c1e76 100644 --- a/src/views/mobile/SignupPage.vue +++ b/src/views/mobile/SignupPage.vue @@ -95,13 +95,13 @@ class="list-item-with-header-and-title list-item-no-item-after" :key="currentLocale + '_firstDayOfWeek'" :header="$t('First Day of Week')" - :title="getDayOfWeekName(user.firstDayOfWeek)" + :title="currentDayOfWeekName" smart-select :smart-select-params="{ openIn: 'popup', popupPush: true, closeOnSelect: true, scrollToSelectedItem: true, searchbar: true, searchbarPlaceholder: $t('Date'), searchbarDisableText: $t('Cancel'), appendSearchbarNotFound: $t('No results'), pageTitle: $t('First Day of Week'), popupCloseLinkText: $t('Done') }" > @@ -180,7 +180,6 @@ import { useSettingsStore } from '@/stores/setting.js'; import { useTransactionCategoriesStore } from '@/stores/transactionCategory.js'; import { useExchangeRatesStore } from '@/stores/exchangeRates.js'; -import datetimeConstants from '@/consts/datetime.js'; import categoryConstants from '@/consts/category.js'; import { getNameByKeyValue, copyArrayTo } from '@/lib/common.js'; @@ -201,7 +200,7 @@ export default { nickname: '', language: self.$i18n.locale, defaultCurrency: settingsStore.defaultSetting.currency, - firstDayOfWeek: datetimeConstants.allWeekDays[self.$t('default.firstDayOfWeek')] ? datetimeConstants.allWeekDays[self.$t('default.firstDayOfWeek')].type : 0 + firstDayOfWeek: settingsStore.defaultSetting.firstDayOfWeek, }, submitting: false, presetCategories: { @@ -224,7 +223,7 @@ export default { return this.$locale.getAllCurrencies(); }, allWeekDays() { - return datetimeConstants.allWeekDays; + return this.$locale.getAllWeekDays(); }, currentLocale: { get: function () { @@ -232,7 +231,7 @@ export default { }, set: function (value) { const isCurrencyDefault = this.user.defaultCurrency === this.settingsStore.defaultSetting.currency; - const isFirstWeekDayDefault = this.user.firstDayOfWeek === (datetimeConstants.allWeekDays[this.$t('default.firstDayOfWeek')] ? datetimeConstants.allWeekDays[this.$t('default.firstDayOfWeek')].type : 0); + const isFirstWeekDayDefault = this.user.firstDayOfWeek === this.settingsStore.defaultSetting.firstDayOfWeek; this.user.language = value; @@ -244,7 +243,7 @@ export default { } if (isFirstWeekDayDefault) { - this.user.firstDayOfWeek = datetimeConstants.allWeekDays[this.$t('default.firstDayOfWeek')] ? datetimeConstants.allWeekDays[this.$t('default.firstDayOfWeek')].type : 0; + this.user.firstDayOfWeek = this.settingsStore.defaultSetting.firstDayOfWeek; } } }, @@ -257,6 +256,9 @@ export default { return languageInfo.displayName; }, + currentDayOfWeekName() { + return getNameByKeyValue(this.allWeekDays, this.user.firstDayOfWeek, 'type', 'displayName'); + }, inputIsEmpty() { return !!this.inputEmptyProblemMessage; }, @@ -397,11 +399,6 @@ export default { } }); }, - getDayOfWeekName(dayOfWeek) { - const weekName = getNameByKeyValue(datetimeConstants.allWeekDays, dayOfWeek, 'type', 'name'); - const i18nWeekNameKey = `datetime.${weekName}.long`; - return this.$t(i18nWeekNameKey); - }, getCategoryTypeName(categoryType) { switch (categoryType) { case categoryConstants.allCategoryTypes.Income.toString(): diff --git a/src/views/mobile/users/UserProfilePage.vue b/src/views/mobile/users/UserProfilePage.vue index e4f87b03..1a5a7030 100644 --- a/src/views/mobile/users/UserProfilePage.vue +++ b/src/views/mobile/users/UserProfilePage.vue @@ -149,7 +149,7 @@ @@ -226,7 +226,6 @@ import { useSettingsStore } from '@/stores/setting.js'; import { useUserStore } from '@/stores/user.js'; import { useAccountsStore } from '@/stores/account.js'; -import datetimeConstants from '@/consts/datetime.js'; import { getNameByKeyValue } from '@/lib/common.js'; import { getCategorizedAccounts } from '@/lib/account.js'; @@ -311,7 +310,7 @@ export default { return getCategorizedAccounts(this.allVisibleAccounts); }, allWeekDays() { - return datetimeConstants.allWeekDays; + return this.$locale.getAllWeekDays(); }, allLongDateFormats() { return this.$locale.getAllLongDateFormats(); @@ -359,9 +358,7 @@ export default { return this.$t('Unknown'); }, currentDayOfWeekName() { - const weekName = getNameByKeyValue(datetimeConstants.allWeekDays, this.newProfile.firstDayOfWeek, 'type', 'name'); - const i18nWeekNameKey = `datetime.${weekName}.long`; - return this.$t(i18nWeekNameKey); + return getNameByKeyValue(this.allWeekDays, this.newProfile.firstDayOfWeek, 'type', 'displayName'); }, inputIsNotChanged() { return !!this.inputIsNotChangedProblemMessage;