Skip to content

Commit

Permalink
fix: corecctly transform the resources that including array messages,…
Browse files Browse the repository at this point in the history
… as array (#2147)
  • Loading branch information
kazupon committed Jun 12, 2023
1 parent 5ed73d5 commit 88c960a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
3 changes: 2 additions & 1 deletion playground/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
},
"welcome": "Welcome",
"hello": "Hello {name} !",
"tag": "<p>Tag</p>"
"tag": "<p>Tag</p>",
"items": [{ "name": "apple" }, { "name": "banana" }, { "name": "strabelly" }]
}
3 changes: 2 additions & 1 deletion playground/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
}
},
"welcome": "Bienvenue",
"hello": "Bonjour {name} !"
"hello": "Bonjour {name} !",
"items": []
}
11 changes: 10 additions & 1 deletion playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { computed } from 'vue'
import { LocaleObject } from '#i18n'
const route = useRoute()
const { t, strategy, locale, locales, localeProperties, setLocale, defaultLocale, finalizePendingLocaleChange } = useI18n()
const { t, rt, tm, strategy, locale, locales, localeProperties, setLocale, defaultLocale, finalizePendingLocaleChange } = useI18n()
const localePath = useLocalePath()
const switchLocalePath = useSwitchLocalePath()
const getRouteBaseName = useRouteBaseName()
Expand All @@ -30,6 +30,11 @@ const availableLocales = computed(() => {
return (locales.value as LocaleObject[]).filter(i => i.code !== locale.value)
})
const i = tm('items')
console.log('items via tm', i, typeof i)
const items = i.map(item => rt(item.name))
console.log('items items', items)
definePageMeta({
title: 'pages.title.top',
middleware: () => {
Expand Down Expand Up @@ -76,6 +81,10 @@ definePageMeta({
</nav>
<p>{{ $t('settings.profile') }}</p>
<p>{{ $t('tag') }}</p>
<h3>Items</h3>
<div v-for="item in items">
<p>{{ item }}</p>
</div>
</div>
</template>

Expand Down
3 changes: 2 additions & 1 deletion playground/vue-i18n.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export default defineI18nConfig(() => ({
bar: {
buz: 'こんにちは!{name}!',
fn: ({ named }: any) => `こんにちは!${named('name')}!`
}
},
items: [{ name: 'りんご' }, { name: 'バナナ' }, { name: 'いちご' }]
},
fr
},
Expand Down
17 changes: 15 additions & 2 deletions src/runtime/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,21 @@ export function parseAcceptLanguage(input: string): string[] {

function deepCopy(src: Record<string, any>, des: Record<string, any>, predicate?: (src: any, des: any) => boolean) {
for (const key in src) {
if (isObject(src[key])) {
if (!isObject(des[key])) des[key] = {}
if (isArray(src[key])) {
if (!isArray(des[key])) {
des[key] = []
}
;(src[key] as any[]).forEach((item, index) => {
if (!des[key][index]) {
const desItem = {}
deepCopy(item, desItem, predicate)
des[key].push(desItem)
}
})
} else if (isObject(src[key])) {
if (!isObject(des[key])) {
des[key] = {}
}
deepCopy(src[key], des[key], predicate)
} else {
if (predicate) {
Expand Down

0 comments on commit 88c960a

Please sign in to comment.