Skip to content

Instantly share code, notes, and snippets.

@atomjoy
Last active July 2, 2025 09:30
Show Gist options
  • Save atomjoy/4e78790834db7ce8f63f657e5095f803 to your computer and use it in GitHub Desktop.
Save atomjoy/4e78790834db7ce8f63f657e5095f803 to your computer and use it in GitHub Desktop.
Vue component local locales
<script setup>
// See: https://vue-i18n.intlify.dev/guide/advanced/composition
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
const { t } = useI18n({
// useScope: 'global', // Overwrite global
locale: 'en',
messages: {
en: {
msg: 'hello',
named: '{msg} world!',
list: '{0} world!',
literal: "{'hello'} world!",
the_world: 'the world',
dio: 'DIO:',
linked: '@:dio @:the_world !!!!'
},
ja: {
msg: 'こんにちは',
named: '{msg} 世界!',
list: '{0} 世界!',
literal: "{'こんにちは'} 世界!",
the_world: 'ザ・ワールド!',
dio: 'ディオ:',
linked: '@:dio @:the_world !!!!'
}
}
})
const msg = computed(() => t('msg'))
</script>
<template>
<p>{{ t('named', { msg }) }}</p>
<p>{{ t('list', [msg]) }}</p>
<p>{{ t('literal') }}</p>
<p>{{ t('linked') }}</p>
</template>
<script setup>
import { useI18n } from 'vue-i18n'
const { t } = useI18n({
locale: 'en',
messages: {
en: {
car: 'car | cars',
apple: 'no apples | one apple | {count} apples',
banana: 'no bananas | {n} banana | {n} bananas'
}
}
})
</script>
<template>
<h2>Car:</h2>
<p>{{ t('car', 1) }}</p>
<p>{{ t('car', 2) }}</p>
<h2>Apple:</h2>
<p>{{ t('apple', 0) }}</p>
<p>{{ t('apple', 1) }}</p>
<p>{{ t('apple', { count: 10 }, 10) }}</p>
<p>{{ t('apple', 10) }}</p>
<h2>Banana:</h2>
<p>{{ t('banana', { n: 1 }, 1) }}</p>
<p>{{ t('banana', 1) }}</p>
<p>{{ t('banana', { n: 'too many' }, 100) }}</p>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment