Skip to content

Commit 6d4cd2e

Browse files
authored
Merge pull request #140 from vuejs-jp/feature/sponsor-section-localization
refactor: make getTranslationOrDefault function composable
2 parents 4ff89e0 + 1c8cb44 commit 6d4cd2e

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

apps/web/app/components/SponsorPageSection.vue

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,25 @@
22
import { useI18n } from '#i18n'
33
import { useRuntimeConfig } from '#imports'
44
import { useColor, useTypography } from '@vuejs-jp/composable'
5-
import { useLocaleCurrent } from '@/composables/useLocaleCurrent'
5+
import { useTranslation } from '@/composables/useTranslation'
66
77
const config = useRuntimeConfig()
88
const { fontWeight, fontSize } = useTypography()
99
const { color } = useColor()
1010
11-
const { t, te } = useI18n()
12-
const { locale } = useLocaleCurrent()
13-
/**
14-
* Get translation or return empty string
15-
* @param key - translation key
16-
* @returns translation or empty string
17-
*/
18-
function getTranslationOrDefault(key: string): string {
19-
return te(key, locale.value) ? t(key) : ''
20-
}
11+
const { t } = useI18n()
12+
const { translate } = useTranslation()
2113
2214
const periodStart = {
2315
prefixYear: t('prefix_year'),
2416
date: t('sponsor.start_date'),
25-
dayOfWeek: getTranslationOrDefault('day_of_week.monday'),
17+
dayOfWeek: translate('day_of_week.monday'),
2618
}
2719
2820
// const periodEnd = {
2921
// suffixYear: t('suffix_year'),
3022
// date: t('sponsor.end_date'),
31-
// dayOfWeek: getTranslationOrDefault('day_of_week.thursday'),
23+
// dayOfWeek: translate('day_of_week.thursday'),
3224
// }
3325
</script>
3426

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useI18n } from '#i18n'
2+
import { useLocaleCurrent } from '@/composables/useLocaleCurrent'
3+
4+
export function useTranslation() {
5+
const { t, te } = useI18n()
6+
const { locale } = useLocaleCurrent()
7+
function translate(key: string) {
8+
return te(key, locale.value) ? t(key) : ''
9+
}
10+
11+
return {
12+
/**
13+
* return translation or empty string
14+
* https://github.com/vuejs-jp/vuefes-2024/pull/136#discussion_r1597312717
15+
*/
16+
translate
17+
}
18+
}

0 commit comments

Comments
 (0)