Skip to content

Commit c84d4e0

Browse files
committed
📝 docs(README): add vue-i18n extensions usages
1 parent 94b7b03 commit c84d4e0

File tree

1 file changed

+122
-6
lines changed

1 file changed

+122
-6
lines changed

README.md

Lines changed: 122 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,130 @@
11
# :globe_with_meridians: vue-i18n-extensions
22

3-
:warning: WIP
4-
53
[![CircleCI](https://circleci.com/gh/kazupon/vue-i18n-extensions/tree/dev.svg?style=svg)](https://circleci.com/gh/kazupon/vue-i18n-extensions/tree/dev)
6-
<!--
7-
[![codecov](https://codecov.io/gh/kazupon/vue-i18n-extensions/branch/dev/graph/badge.svg)](https://codecov.io/gh/kazupon/vue-i18n-extensions)
8-
-->
94
[![npm](https://img.shields.io/npm/v/vue-i18n-extensions.svg)](https://www.npmjs.com/package/vue-i18n-extensions)
105

11-
vue-i18n extensions
6+
> Extensions for vue-i18n
7+
8+
This library exports the following extensions:
9+
10+
- directive: `v-t` custom directive for server-side
11+
- module: `v-t` custom directive compiler module for `vue-template-compiler` or `vue-loader` (`compilerModules` option)
12+
13+
## :cd: Installation
14+
15+
```sh
16+
$ npm i --save-dev vue-i18n-extensions
17+
```
18+
19+
## :rocket: Extentions
20+
21+
### directive: `v-t` custom directive for server-side
22+
This directive is `v-t` custom directive for server-side-rendering. You can specify it as [`directives` option](https://ssr.vuejs.org/en/api.html#directives) of `createRenderer` function.
23+
24+
The following example:
25+
26+
```javascript
27+
import Vue from 'vue'
28+
import VueI18n from 'vue-i18n'
29+
import { createRenderer } from 'vue-server-renderer'
30+
import { directive as t } from 'vue-i18n-extensions'
31+
32+
Vue.use(VueI18n)
33+
34+
const i18n = new VueI18n({
35+
locale: 'en',
36+
messages: {
37+
en: {
38+
hello: 'hello'
39+
},
40+
ja: {
41+
hello: 'こんにちは'
42+
}
43+
}
44+
})
45+
const renderer = createRenderer({ directives: { t } })
46+
47+
const app = new Vue({
48+
i18n,
49+
render (h) {
50+
// <p v-t="'hello'"></p>
51+
return h('p', {
52+
directives: [{
53+
name: 't', rawName: 'v-t',
54+
value: ('hello'), expression: "'hello'"
55+
}]
56+
})
57+
}
58+
})
59+
60+
renderer.renderToString(app, (err, html) => {
61+
console.log(html) // output -> '<p data-server-rendered="true">hello</p>'
62+
})
63+
```
64+
65+
### module: `v-t` custom directive compiler module
66+
This module is `v-t` custom directive module for vue compiler. You can specify it as [`modules` option](https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler#vue-template-compiler) of `vue-template-compiler`.
67+
68+
The following example that use `compile` function of `vue-template-compiler`:
69+
70+
```javascript
71+
import Vue from 'vue'
72+
import VueI18n from 'vue-i18n'
73+
import { compile } from 'vue-template-compiler'
74+
import { module } from 'vue-i18n-extensions'
75+
76+
Vue.use(VueI18n)
77+
78+
const i18n = new VueI18n({
79+
locale: 'en',
80+
messages: {
81+
en: {
82+
hello: 'hello'
83+
},
84+
ja: {
85+
hello: 'こんにちは'
86+
}
87+
},
88+
missing: (locale, key) => {
89+
console.log(`translation missing: locale=${locale}, key=${key}`)
90+
}
91+
})
92+
const i18nModule = module(i18n)
93+
94+
const { ast, render } = compile(`<p v-t="'hello'"></p>`, { modules: [i18nModule] })
95+
console.log(ast.i18n) // output -> 'hello'
96+
console.log(render) // output -> `with(this){return _c('p',{domProps:{"textContent":_s("hello")}})}`
97+
```
98+
99+
The following configration example of `vue-loader`:
100+
101+
```javascript
102+
const Vue = require('vue')
103+
const VueI18n = require('vue-i18n')
104+
const i18nExtensions = require('vue-i18n-extensions')
105+
const messages = require('./locales.json')
106+
107+
Vue.use(VueI18n)
108+
109+
const i18n = new VueI18n({
110+
locale: 'ja',
111+
messages: messages
112+
})
113+
114+
module.exports = {
115+
module: {
116+
rules: [{
117+
test: /\.vue$/,
118+
loader: 'vue',
119+
options: {
120+
compilerModules: [i18nExtensions.module(i18n)],
121+
// other vue-loader options go here
122+
loaders: {}
123+
}
124+
}]
125+
}
126+
}
127+
```
12128

13129
## :scroll: Changelog
14130
Details changes for each release are documented in the [CHANGELOG.md](https://github.com/kazupon/vue-i18n-extensions/blob/dev/CHANGELOG.md).

0 commit comments

Comments
 (0)