Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Commit fde106f

Browse files
authored
Merge pull request #9 from charlybrown03/feature-add-locale-import
Feature add locale import
2 parents 9444a69 + f66d40f commit fde106f

File tree

12 files changed

+111
-12
lines changed

12 files changed

+111
-12
lines changed

lib/generate.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
33
return (mod && mod.__esModule) ? mod : { "default": mod };
44
};
55
Object.defineProperty(exports, "__esModule", { value: true });
6+
const path_1 = __importDefault(require("path"));
7+
const fs_1 = __importDefault(require("fs"));
68
const json5_1 = __importDefault(require("json5"));
79
const js_yaml_1 = __importDefault(require("js-yaml"));
810
const debug_1 = require("debug");
@@ -11,12 +13,14 @@ exports.VUE_OPTIONS = '__vue__options__';
1113
exports.VUE_I18N_OPTION = '__i18n';
1214
const defaultLang = 'json';
1315
const debug = debug_1.debug('vue-i18n-jest');
14-
function generate(blocks) {
16+
function generate(blocks, sourcePath) {
1517
const base = `${exports.VUE_OPTIONS}.${exports.VUE_I18N_OPTION} = []`;
1618
const codes = blocks.map(block => {
1719
if (block.type === 'i18n') {
1820
const lang = (block.attrs && block.attrs.lang) || defaultLang;
19-
// const lang = block.attrs?lang ?? defaultLang
21+
block.content = (block.attrs && block.attrs.src)
22+
? fs_1.default.readFileSync(getAbsolutePath(block, sourcePath)).toString()
23+
: block.content;
2024
const data = convert(block.content, lang);
2125
const value = JSON.stringify(JSON.parse(data))
2226
.replace(/\u2028/g, '\\u2028')
@@ -44,3 +48,8 @@ function convert(source, lang) {
4448
return source;
4549
}
4650
}
51+
function getAbsolutePath(block, sourcePath) {
52+
if (path_1.default.isAbsolute(block.attrs.src))
53+
return block.attrs.src;
54+
return path_1.default.join(path_1.default.dirname(sourcePath), block.attrs.src);
55+
}

lib/transform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function jestProcess(sourceText, sourcePath, config, options) {
2525
const { customBlocks } = vue_template_compiler_1.parseComponent(sourceText, { pad: true });
2626
let coding = ';\n';
2727
if (customBlocks) {
28-
coding += generate_1.default(customBlocks);
28+
coding += generate_1.default(customBlocks, sourcePath);
2929
}
3030
const retCode = code + coding;
3131
debug('process', retCode);

src/generate.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import path from 'path'
2+
import fs from 'fs'
3+
14
import JSON5 from 'json5'
25
import yaml from 'js-yaml'
36
import { debug as Debug } from 'debug'
@@ -12,13 +15,17 @@ const defaultLang = 'json'
1215

1316
const debug = Debug('vue-i18n-jest')
1417

15-
export default function generate (blocks: SFCBlock[]) {
18+
export default function generate (blocks: SFCBlock[], sourcePath: string) {
1619
const base = `${VUE_OPTIONS}.${VUE_I18N_OPTION} = []`
1720
const codes = blocks.map(block => {
1821
if (block.type === 'i18n') {
1922
const lang = (block.attrs && block.attrs.lang) || defaultLang
2023
// const lang = block.attrs?lang ?? defaultLang
21-
const data = convert(block.content, lang)
24+
const content = (block.attrs && block.attrs.src)
25+
? fs.readFileSync(getAbsolutePath(block, sourcePath)).toString()
26+
: block.content
27+
28+
const data = convert(content, lang)
2229
const value = JSON.stringify(JSON.parse(data))
2330
.replace(/\u2028/g, '\\u2028')
2431
.replace(/\u2029/g, '\\u2029')
@@ -45,3 +52,8 @@ function convert (source: string, lang: string): string {
4552
return source
4653
}
4754
}
55+
56+
function getAbsolutePath (block: SFCBlock, sourcePath: string): string {
57+
if (path.isAbsolute(block.attrs.src)) return block.attrs.src
58+
return path.join(path.dirname(sourcePath), block.attrs.src)
59+
}

src/transform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function jestProcess (
4343

4444
let coding = ';\n'
4545
if (customBlocks) {
46-
coding += generate(customBlocks)
46+
coding += generate(customBlocks, sourcePath)
4747
}
4848

4949
const retCode = code + coding

test/__snapshots__/generate.test.ts.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,27 @@ exports[`generate: json.vue 1`] = `
1010
__vue__options__.__i18n.push('{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"},\\"en\\":{\\"hello\\":\\"hello!\\"}}')"
1111
`;
1212

13+
exports[`generate: json-locale-import.vue 1`] = `
14+
"__vue__options__.__i18n = []
15+
__vue__options__.__i18n.push('{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"},\\"en\\":{\\"hello\\":\\"hello!\\"}}')"
16+
`;
17+
1318
exports[`generate: json5.vue 1`] = `
1419
"__vue__options__.__i18n = []
1520
__vue__options__.__i18n.push('{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"},\\"en\\":{\\"hello\\":\\"hello!\\"}}')"
1621
`;
1722

23+
exports[`generate: json5-locale-import.vue 1`] = `
24+
"__vue__options__.__i18n = []
25+
__vue__options__.__i18n.push('{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"},\\"en\\":{\\"hello\\":\\"hello!\\"}}')"
26+
`;
27+
1828
exports[`generate: yaml.vue 1`] = `
1929
"__vue__options__.__i18n = []
2030
__vue__options__.__i18n.push('{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"},\\"en\\":{\\"hello\\":\\"hello!\\"}}')"
2131
`;
32+
33+
exports[`generate: yaml-locale-import.vue 1`] = `
34+
"__vue__options__.__i18n = []
35+
__vue__options__.__i18n.push('{\\"ja\\":{\\"hello\\":\\"こんにちは!\\"},\\"en\\":{\\"hello\\":\\"hello!\\"}}')"
36+
`;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<template>
2+
<p>hello!</p>
3+
</template>
4+
5+
<script>
6+
export default {
7+
}
8+
</script>
9+
10+
<i18n src="./locale.json"></i18n>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<template>
2+
<p>hello!</p>
3+
</template>
4+
5+
<script>
6+
export default {
7+
}
8+
</script>
9+
10+
<i18n lang="json5" src="./locale.json5"></i18n>

test/fixtures/locale.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"ja": {
3+
"hello": "こんにちは!"
4+
},
5+
"en": {
6+
"hello": "hello!"
7+
}
8+
}

test/fixtures/locale.json5

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
ja: {
3+
hello: "こんにちは!"
4+
},
5+
"en": {
6+
"hello": "hello!"
7+
}
8+
}

test/fixtures/locale.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ja:
2+
hello: こんにちは!
3+
en:
4+
hello: hello!

0 commit comments

Comments
 (0)