Skip to content

Commit 66f1640

Browse files
wip(docs): add custom blocks example
1 parent 7918bb6 commit 66f1640

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

docs/examples.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,73 @@ In the following example we use a trick to preserve reactivity through the `Vue.
619619

620620

621621

622+
## Example using SFC Custom Blocks for i18n
623+
624+
<!--example:source:custom_block_i18n-->
625+
```html
626+
<!DOCTYPE html>
627+
<html>
628+
<body>
629+
<script src="https://unpkg.com/vue@next/dist/vue.runtime.global.prod.js"></script>
630+
<script src="https://unpkg.com/vue-i18n@next"></script>
631+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue3-sfc-loader.js"></script>
632+
<script>
633+
634+
/* <!-- */
635+
const config = {
636+
files: {
637+
'/component.vue': `
638+
<template>
639+
{{ $t('hello') }}
640+
</template>
641+
<i18n>
642+
{
643+
"en": {
644+
"hello": "hello world!"
645+
},
646+
"ja": {
647+
"hello": "こんにちは、世界!"
648+
}
649+
}
650+
</i18n>
651+
`
652+
}
653+
};
654+
/* --> */
655+
656+
const i18n = VueI18n.createI18n();
657+
658+
const options = {
659+
moduleCache: { vue: Vue },
660+
getFile: url => config.files[url],
661+
addStyle: () => {},
662+
customBlockHandler(block, filename, options) {
663+
664+
if ( block.type !== 'i18n' )
665+
return
666+
667+
const messages = JSON.parse(block.content);
668+
for ( let locale in messages )
669+
i18n.global.mergeLocaleMessage(locale, messages[locale]);
670+
}
671+
}
672+
673+
const app = Vue.createApp(Vue.defineAsyncComponent(() => window['vue3-sfc-loader'].loadModule('/component.vue', options)));
674+
675+
app.use(i18n);
676+
677+
app.mount(document.body);
678+
679+
</script>
680+
</body>
681+
</html>
682+
```
683+
<!--example:target:custom_block_i18n-->
684+
<!--/example:target:custom_block_i18n-->
685+
[:top:](#readme)
686+
687+
688+
622689
<!---
623690
624691
const regexpReservedChars = '\\.+*?^$|[{()';

0 commit comments

Comments
 (0)