Skip to content

Commit 4b9b711

Browse files
committed
🆙 update: API interfaces
1 parent cdde292 commit 4b9b711

File tree

11 files changed

+223
-217
lines changed

11 files changed

+223
-217
lines changed

lib/index.js

Lines changed: 6 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,11 @@
11
"use strict";
2-
var __importStar = (this && this.__importStar) || function (mod) {
3-
if (mod && mod.__esModule) return mod;
4-
var result = {};
5-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6-
result["default"] = mod;
7-
return result;
8-
};
92
var __importDefault = (this && this.__importDefault) || function (mod) {
103
return (mod && mod.__esModule) ? mod : { "default": mod };
114
};
125
Object.defineProperty(exports, "__esModule", { value: true });
13-
const component_compiler_utils_1 = require("@vue/component-compiler-utils");
14-
const compiler = __importStar(require("vue-template-compiler"));
15-
const path_1 = __importDefault(require("path"));
16-
const debug_1 = require("debug");
17-
const debug = debug_1.debug('vue-i18n-extract-locale-messages');
18-
const messages = [{
19-
fullPath: '/project1/src/App.vue',
20-
content: `
21-
<i18n>
22-
{
23-
"en": { "title": "Application" },
24-
"ja": { "title": "アプリケーション" }
25-
}
26-
</i18n>
27-
`
28-
}, {
29-
fullPath: '/project1/src/components/Modal.vue',
30-
content: `
31-
<i18n locale="en">
32-
{
33-
"ok": "OK",
34-
"cancel": "Cancel"
35-
}
36-
</i18n>
37-
<i18n locale="ja">
38-
{
39-
"ok": "OK",
40-
"cancel": "キャンセル"
41-
}
42-
</i18n>
43-
`
44-
}, {
45-
fullPath: '/project1/src/components/nest/RankingTable.vue',
46-
content: `
47-
<i18n locale="en">
48-
{
49-
"headers": {
50-
"rank": "Rank",
51-
"name": "Name",
52-
"score": "Score"
53-
}
54-
}
55-
</i18n>
56-
`
57-
}, {
58-
fullPath: '/project1/src/pages/Login.vue',
59-
content: `
60-
<i18n>
61-
{
62-
"ja": {
63-
"id": "ユーザーID",
64-
"passowrd": "パスワード",
65-
"confirm": "パスワードの確認入力",
66-
"button": "ログイン"
67-
}
68-
}
69-
</i18n>
70-
`
71-
}];
72-
function parseTargets(base, targets) {
73-
const messages = {};
74-
targets.forEach(target => {
75-
const nestKeypaths = parsePath(base, target);
76-
debug('nestKeypaths', nestKeypaths);
77-
const blockMessages = parseI18nBlock(target.content);
78-
const locales = Object.keys(blockMessages);
79-
const collects = locales.reduce((messages, locale) => {
80-
const ret = nestKeypaths.reduce((messages, key) => {
81-
return Object.assign({}, { [key]: messages });
82-
}, blockMessages[locale]);
83-
return Object.assign(messages, { [locale]: ret });
84-
}, {});
85-
debug('collects', collects);
86-
locales.forEach(locale => {
87-
messages[locale] = messages[locale] || {};
88-
messages[locale] = Object.assign(messages[locale], collects[locale]);
89-
});
90-
});
91-
return messages;
92-
}
93-
exports.parseTargets = parseTargets;
94-
function parsePath(base, target) {
95-
const pathObj = path_1.default.parse(target.fullPath);
96-
const [_, keypathRaw] = pathObj.dir.split(base);
97-
const keypaths = keypathRaw.split(path_1.default.sep);
98-
keypaths.shift();
99-
return [pathObj.name, ...keypaths.reverse()];
100-
}
101-
function parseI18nBlock(content) {
102-
const desc = component_compiler_utils_1.parse({
103-
source: content,
104-
compiler: compiler
105-
});
106-
return desc.customBlocks.reduce((messages, block) => {
107-
if (block.type === 'i18n') {
108-
const obj = JSON.parse(block.content);
109-
if (block.attrs.locale) {
110-
return Object.assign(messages, { [block.attrs.locale]: obj });
111-
}
112-
else {
113-
return Object.assign(messages, obj);
114-
}
115-
}
116-
else {
117-
return messages;
118-
}
119-
}, {});
120-
}
121-
const res = parseTargets('/project1/src', messages);
122-
console.log('extract locale messages', JSON.stringify(res, null, 2));
123-
function add(a, b) {
124-
return a + b;
125-
}
126-
exports.default = add;
6+
const reflector_1 = __importDefault(require("./reflector"));
7+
exports.getLocaleMessageMeta = reflector_1.default;
8+
const squeezer_1 = __importDefault(require("./squeezer"));
9+
exports.squeeze = squeezer_1.default;
10+
const infuser_1 = __importDefault(require("./infuser"));
11+
exports.infuse = infuser_1.default;

lib/infuser.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
function infuse(messages, meta) {
4+
}
5+
exports.default = infuse;

lib/reflector.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,47 @@
11
"use strict";
2+
var __importStar = (this && this.__importStar) || function (mod) {
3+
if (mod && mod.__esModule) return mod;
4+
var result = {};
5+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6+
result["default"] = mod;
7+
return result;
8+
};
9+
var __importDefault = (this && this.__importDefault) || function (mod) {
10+
return (mod && mod.__esModule) ? mod : { "default": mod };
11+
};
12+
Object.defineProperty(exports, "__esModule", { value: true });
13+
const component_compiler_utils_1 = require("@vue/component-compiler-utils");
14+
const compiler = __importStar(require("vue-template-compiler"));
15+
const path_1 = __importDefault(require("path"));
16+
const debug_1 = require("debug");
17+
const debug = debug_1.debug('vue-i18n-locale-message:reflector');
18+
function reflectLocaleMessageMeta(basePath, components) {
19+
return components.map(target => {
20+
const desc = component_compiler_utils_1.parse({
21+
source: target.content,
22+
filename: target.path,
23+
compiler: compiler
24+
});
25+
const { contentPath, component, hierarchy } = parsePath(basePath, target.path);
26+
return {
27+
contentPath,
28+
blocks: desc.customBlocks,
29+
component,
30+
hierarchy
31+
};
32+
});
33+
}
34+
exports.default = reflectLocaleMessageMeta;
35+
function parsePath(basePath, targetPath) {
36+
const parsed = path_1.default.parse(targetPath);
37+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
38+
const [_, target] = parsed.dir.split(basePath);
39+
const parsedTargetPath = target.split(path_1.default.sep);
40+
parsedTargetPath.shift();
41+
debug(`parsePath: contentPath = ${targetPath}, component = ${parsed.name}, messageHierarchy = ${parsedTargetPath}`);
42+
return {
43+
contentPath: targetPath,
44+
component: parsed.name,
45+
hierarchy: parsedTargetPath
46+
};
47+
}

lib/squeezer.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,61 @@
11
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
const json5_1 = __importDefault(require("json5"));
7+
const js_yaml_1 = __importDefault(require("js-yaml"));
8+
const debug_1 = require("debug");
9+
const debug = debug_1.debug('vue-i18n-locale-message:squeezer');
10+
function sqeeze(meta) {
11+
const messages = {};
12+
meta.forEach(target => {
13+
const blockMessages = squeezeFromI18nBlock(target.blocks);
14+
const locales = Object.keys(blockMessages);
15+
const collects = locales.reduce((messages, locale) => {
16+
const ret = target.hierarchy.reduce((messages, key) => {
17+
return Object.assign({}, { [key]: messages });
18+
}, blockMessages[locale]);
19+
return Object.assign(messages, { [locale]: ret });
20+
}, {});
21+
debug('collects', collects);
22+
locales.forEach(locale => {
23+
messages[locale] = messages[locale] || {};
24+
messages[locale] = Object.assign(messages[locale], collects[locale]);
25+
});
26+
});
27+
return messages;
28+
}
29+
exports.default = sqeeze;
30+
function squeezeFromI18nBlock(blocks) {
31+
return blocks.reduce((messages, block) => {
32+
debug('i18n block attrs', block.attrs);
33+
if (block.type === 'i18n') {
34+
let lang = block.attrs.lang;
35+
lang = (!lang || typeof lang !== 'string') ? 'json' : lang;
36+
const obj = parseContent(block.content, lang);
37+
const locale = block.attrs.locale;
38+
if (!locale || typeof locale !== 'string') {
39+
return Object.assign(messages, obj);
40+
}
41+
else {
42+
return Object.assign(messages, { [locale]: obj });
43+
}
44+
}
45+
else {
46+
return messages;
47+
}
48+
}, {});
49+
}
50+
function parseContent(content, lang) {
51+
switch (lang) {
52+
case 'yaml':
53+
case 'yml':
54+
return js_yaml_1.default.safeLoad(content);
55+
case 'json5':
56+
return json5_1.default.parse(content);
57+
case 'json':
58+
default:
59+
return JSON.parse(content);
60+
}
61+
}

src/reflector.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ export default function reflectLocaleMessageMeta (basePath: string, components:
1515
filename: target.path,
1616
compiler: compiler as VueTemplateCompiler
1717
})
18-
const { contentPath, component, messageHierarchy } = parsePath(basePath, target.path)
19-
const cm = ''
18+
const { contentPath, component, hierarchy } = parsePath(basePath, target.path)
2019
return {
2120
contentPath,
22-
content: cm,
2321
blocks: desc.customBlocks,
2422
component,
25-
messageHierarchy
23+
hierarchy
2624
}
2725
})
2826
}
@@ -37,6 +35,6 @@ function parsePath (basePath: string, targetPath: string) {
3735
return {
3836
contentPath: targetPath,
3937
component: parsed.name,
40-
messageHierarchy: parsedTargetPath
38+
hierarchy: parsedTargetPath
4139
}
4240
}

src/squeezer.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import { LocaleMessageMeta, LocaleMessages } from '../types'
2-
import { VueTemplateCompiler } from '@vue/component-compiler-utils/dist/types'
2+
import { SFCCustomBlock } from '@vue/component-compiler-utils'
33

4-
import { parse } from '@vue/component-compiler-utils'
5-
import * as compiler from 'vue-template-compiler'
64
import JSON5 from 'json5'
75
import yaml from 'js-yaml'
86

97
import { debug as Debug } from 'debug'
10-
const debug = Debug('vue-i18n-locale-messages:squeezer')
8+
const debug = Debug('vue-i18n-locale-message:squeezer')
119

1210
export default function sqeeze (meta: LocaleMessageMeta[]): LocaleMessages {
1311
const messages: LocaleMessages = {}
1412

1513
meta.forEach(target => {
16-
const blockMessages = squeezeFromI18nBlock(target.content)
14+
const blockMessages = squeezeFromI18nBlock(target.blocks)
1715
const locales = Object.keys(blockMessages)
1816
const collects: LocaleMessages = locales.reduce((messages, locale) => {
19-
const ret = target.messageHierarchy.reduce((messages, key) => {
17+
const ret = target.hierarchy.reduce((messages, key) => {
2018
return Object.assign({}, { [key]: messages })
2119
}, blockMessages[locale])
2220
return Object.assign(messages, { [locale]: ret })
@@ -32,13 +30,8 @@ export default function sqeeze (meta: LocaleMessageMeta[]): LocaleMessages {
3230
return messages
3331
}
3432

35-
function squeezeFromI18nBlock (content: string): LocaleMessages {
36-
const desc = parse({
37-
source: content,
38-
compiler: compiler as VueTemplateCompiler
39-
})
40-
41-
return desc.customBlocks.reduce((messages, block) => {
33+
function squeezeFromI18nBlock (blocks: SFCCustomBlock[]): LocaleMessages {
34+
return blocks.reduce((messages, block) => {
4235
debug('i18n block attrs', block.attrs)
4336

4437
if (block.type === 'i18n') {

test/fixtures/format/json5.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
export default [{
22
contentPath: '/path/to/project1/src/components/Modal.vue',
3-
content: `
4-
<i18n locale="en" lang="json5">
5-
{
6-
// modal contents
7-
"ok": "OK",
8-
cancel: "Cancel"
9-
}
10-
</i18n>
11-
<i18n locale="ja">
12-
{
13-
"ok": "OK",
14-
"cancel": "キャンセル"
15-
}
16-
</i18n>
17-
`,
3+
blocks: [{
4+
type: 'i18n',
5+
content: `
6+
{
7+
// modal contents
8+
"ok": "OK",
9+
cancel: "Cancel"
10+
}
11+
`,
12+
attrs: { locale: 'en', lang: 'json5' }
13+
}, {
14+
type: 'i18n',
15+
content: `
16+
{
17+
"ok": "OK",
18+
"cancel": "キャンセル"
19+
}
20+
`,
21+
attrs: { locale: 'ja' }
22+
}],
1823
component: 'Modal',
19-
messageHierarchy: ['components', 'Modal']
24+
hierarchy: ['components', 'Modal']
2025
}]

test/fixtures/format/yaml.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
export default [{
22
contentPath: '/path/to/project1/src/components/Modal.vue',
3-
content: `
4-
<i18n locale="en" lang="yaml">
5-
ok: "OK"
6-
cancel: "Cancel"
7-
</i18n>
8-
<i18n locale="ja" lang="yml">
9-
ok: OK
10-
cancel: キャンセル
11-
</i18n>
12-
`,
3+
blocks: [{
4+
type: 'i18n',
5+
content: `
6+
ok: "OK"
7+
cancel: "Cancel"
8+
`,
9+
attrs: { locale: 'en', lang: 'yaml' }
10+
}, {
11+
type: 'i18n',
12+
content: `
13+
ok: OK
14+
cancel: キャンセル
15+
`,
16+
attrs: { locale: 'ja', lang: 'yml' }
17+
}],
1318
component: 'Modal',
14-
messageHierarchy: ['components', 'Modal']
19+
hierarchy: ['components', 'Modal']
1520
}]

0 commit comments

Comments
 (0)