File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -140,12 +140,15 @@ import yaml from 'js-yaml'
140
140
const vueI18nPlugin = {
141
141
name: ' vue-i18n' ,
142
142
transform(code , id ) {
143
+ // if .vue file don't have <i18n> block, just return
143
144
if (! / vue&type=i18n/ .test (id )) {
144
145
return
145
146
}
147
+ // parse yaml
146
148
if (/ \. ya? ml$ / .test (id )) {
147
149
code = JSON .stringify (yaml .load (code .trim ()))
148
150
}
151
+ // mount the value on the i18n property of the component instance
149
152
return ` export default Comp => {
150
153
Comp.i18n = ${code }
151
154
} `
@@ -157,6 +160,30 @@ export default {
157
160
}
158
161
```
159
162
163
+ Create a file named ` Demo.vue ` , add ` lang="yaml" ` to the ` <i18n> ` blocks, then you can use the syntax of ` YAML ` :
164
+
165
+ ``` vue
166
+ <template>Hello</template>
167
+
168
+ <i18n lang="yaml">
169
+ message: 'world'
170
+ fullWord: 'hello world'
171
+ </i18n>
172
+ ```
173
+
174
+ ` message ` is mounted on the i18n property of the component instance, you can use like this:
175
+
176
+ ``` vue
177
+ <script setup lang="ts">
178
+ import Demo from 'components/Demo.vue'
179
+ </script>
180
+
181
+ <template>
182
+ <Demo /> {{ Demo.i18n.message }}
183
+ <div>{{ Demo.i18n.fullWord }}</div>
184
+ </template>
185
+ ```
186
+
160
187
## Using Vue SFCs as Custom Elements
161
188
162
189
> Requires ` vue@^3.2.0 ` & ` @vitejs/plugin-vue@^1.4.0 `
You can’t perform that action at this time.
0 commit comments