Skip to content

Instantly share code, notes, and snippets.

@hdahlheim
Created June 5, 2020 05:44
Show Gist options
  • Save hdahlheim/cc3f74474f8040bbe7d68cd29fb7f639 to your computer and use it in GitHub Desktop.
Save hdahlheim/cc3f74474f8040bbe7d68cd29fb7f639 to your computer and use it in GitHub Desktop.
Messages won't update if rendered inside a slot.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Global scope example</title>
<script src="../../../node_modules/vue/dist/vue.global.js"></script>
<script src="../../../dist/vue-i18n.global.js"></script>
</head>
<body>
<div id="app">
<h1>Root</h1>
<form>
<label>{{ $t('message.language') }}</label>
<select v-model="$i18n.locale">
<option value="en">en</option>
<option value="ja">ja</option>
</select>
</form>
<p>{{ $t('message.hello') }}</p>
<Child />
</div>
<script>
const { createApp } = Vue
const { createI18n } = VueI18n
const SlotChild = {
template: `
<p><slot/></p>
`
}
const SubChild = {
template: `
<div class="sub-child">
<h1>Sub Child</h1>
<form>
<label>{{ $t('message.language') }}</label>
<select v-model="$i18n.locale">
<option value="en">en</option>
<option value="ja">ja</option>
</select>
</form>
<p>{{ $t('message.hi') }}</p>
</div>
`
}
const Child = {
components: {
SubChild,
SlotChild
},
template: `
<div class="child">
<h1>Child</h1>
<form>
<label>{{ $t('message.language') }}</label>
<select v-model="$i18n.locale">
<option value="en">en</option>
<option value="ja">ja</option>
</select>
</form>
<p>{{ $t('message.hi') }}</p>
<SubChild />
$t inside of slot
<SlotChild>
{{ $t('message.hi') }}
</SlotChild>
i18n-t inside of slot
<SlotChild>
<i18n-t keypath='message.hi'/>
</SlotChild>
</div>
`
}
const i18n = createI18n({
legacy: true,
locale: 'ja',
messages: {
en: {
message: {
language: 'Language',
hello: 'hello world!',
hi: 'Hi !'
}
},
ja: {
message: {
language: '言語',
hello: 'こんにちは、世界!',
hi: 'こんにちは!'
}
}
}
})
const app = createApp({
components: { Child }
})
app.use(i18n)
app.mount('#app')
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment