-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
239 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,126 @@ | ||
"use strict"; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = {}; | ||
const component_compiler_utils_1 = require("@vue/component-compiler-utils"); | ||
const compiler = __importStar(require("vue-template-compiler")); | ||
const path_1 = __importDefault(require("path")); | ||
const debug_1 = require("debug"); | ||
const debug = debug_1.debug('vue-i18n-extract-locale-messages'); | ||
const messages = [{ | ||
fullPath: '/project1/src/App.vue', | ||
content: ` | ||
<i18n> | ||
{ | ||
"en": { "title": "Application" }, | ||
"ja": { "title": "アプリケーション" } | ||
} | ||
</i18n> | ||
` | ||
}, { | ||
fullPath: '/project1/src/components/Modal.vue', | ||
content: ` | ||
<i18n locale="en"> | ||
{ | ||
"ok": "OK", | ||
"cancel": "Cancel" | ||
} | ||
</i18n> | ||
<i18n locale="ja"> | ||
{ | ||
"ok": "OK", | ||
"cancel": "キャンセル" | ||
} | ||
</i18n> | ||
` | ||
}, { | ||
fullPath: '/project1/src/components/nest/RankingTable.vue', | ||
content: ` | ||
<i18n locale="en"> | ||
{ | ||
"headers": { | ||
"rank": "Rank", | ||
"name": "Name", | ||
"score": "Score" | ||
} | ||
} | ||
</i18n> | ||
` | ||
}, { | ||
fullPath: '/project1/src/pages/Login.vue', | ||
content: ` | ||
<i18n> | ||
{ | ||
"ja": { | ||
"id": "ユーザーID", | ||
"passowrd": "パスワード", | ||
"confirm": "パスワードの確認入力", | ||
"button": "ログイン" | ||
} | ||
} | ||
</i18n> | ||
` | ||
}]; | ||
function parseTargets(base, targets) { | ||
const messages = {}; | ||
targets.forEach(target => { | ||
const nestKeypaths = parsePath(base, target); | ||
debug('nestKeypaths', nestKeypaths); | ||
const blockMessages = parseI18nBlock(target.content); | ||
const locales = Object.keys(blockMessages); | ||
const collects = locales.reduce((messages, locale) => { | ||
const ret = nestKeypaths.reduce((messages, key) => { | ||
return Object.assign({}, { [key]: messages }); | ||
}, blockMessages[locale]); | ||
return Object.assign(messages, { [locale]: ret }); | ||
}, {}); | ||
debug('collects', collects); | ||
locales.forEach(locale => { | ||
messages[locale] = messages[locale] || {}; | ||
messages[locale] = Object.assign(messages[locale], collects[locale]); | ||
}); | ||
}); | ||
return messages; | ||
} | ||
exports.parseTargets = parseTargets; | ||
function parsePath(base, target) { | ||
const pathObj = path_1.default.parse(target.fullPath); | ||
const [_, keypathRaw] = pathObj.dir.split(base); | ||
const keypaths = keypathRaw.split(path_1.default.sep); | ||
keypaths.shift(); | ||
return [pathObj.name, ...keypaths.reverse()]; | ||
} | ||
function parseI18nBlock(content) { | ||
const desc = component_compiler_utils_1.parse({ | ||
source: content, | ||
compiler: compiler | ||
}); | ||
return desc.customBlocks.reduce((messages, block) => { | ||
if (block.type === 'i18n') { | ||
const obj = JSON.parse(block.content); | ||
if (block.attrs.locale) { | ||
return Object.assign(messages, { [block.attrs.locale]: obj }); | ||
} | ||
else { | ||
return Object.assign(messages, obj); | ||
} | ||
} | ||
else { | ||
return messages; | ||
} | ||
}, {}); | ||
} | ||
const res = parseTargets('/project1/src', messages); | ||
console.log('extract locale messages', JSON.stringify(res, null, 2)); | ||
function add(a, b) { | ||
return a + b; | ||
} | ||
exports.default = add; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"use strict"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"use strict"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"use strict"; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
export default function add (a: number, b: number) { | ||
return a + b | ||
import getLocaleMessageMeta from './reflector' | ||
import squeeze from './squeezer' | ||
import infuse from './infuser' | ||
|
||
export { | ||
getLocaleMessageMeta, | ||
squeeze, | ||
infuse | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { LocaleMessages, LocaleMessageMeta } from '../types' | ||
|
||
export default function infuse (messages: LocaleMessages, meta: LocaleMessageMeta[]): void { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { LocaleMessageMeta } from '../types' | ||
|
||
export default function getLocaleMessageMeta (components: string[]): LocaleMessageMeta[] { | ||
return [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { LocaleMessageMeta, LocaleMessages } from '../types' | ||
|
||
export default function sqeeze (meta: LocaleMessageMeta[]): LocaleMessages { | ||
return {} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import infuse from '../src/infuser' | ||
|
||
test('infuse', () => { | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import getLocaleMessageMeta from '../src/reflector' | ||
|
||
test('getLocaleMessageMeta', () => { | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import squeeze from '../src/squeezer' | ||
|
||
test('squeeze', () => { | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Locale Message Recursive Structure | ||
* e.g. | ||
* { | ||
* "en": { // 'en' Locale | ||
* "key1": "this is message1", // basic | ||
* "nested": { // nested | ||
* "message1": "this is nested message1" | ||
* }, | ||
* "errors": [ // array | ||
* "this is 0 error code message", | ||
* { // object in array | ||
* "internal1": "this is internal 1 error message" | ||
* }, | ||
* [ // array in array | ||
* "this is nested array error 1" | ||
* ] | ||
* ] | ||
* }, | ||
* "ja": { // 'ja' Locale | ||
* // ... | ||
* } | ||
* } | ||
*/ | ||
|
||
export type LocaleMessage = string | LocaleMessageObject | LocaleMessageArray | ||
export interface LocaleMessageArray extends Array<LocaleMessage> {} | ||
export interface LocaleMessageObject { [key: string]: LocaleMessage } | ||
export type LocaleMessages = { [key: string]: LocaleMessageObject } | ||
|
||
/** | ||
* Locale Message Meta to squeeze / infuse. | ||
* e.g. | ||
* { | ||
* contentPath: '/path/to/project1/src/components/common/Modal.vue', | ||
* content: ` | ||
* <i18n> | ||
* { | ||
* "en": { | ||
* "ok": "OK", | ||
* "cancel": "Cancel" | ||
* }, | ||
* "ja": { | ||
* "ok": "OK", | ||
* "cancel": "キャンセル" | ||
* } | ||
* } | ||
* </i18n> | ||
* `, | ||
* component: 'Modal', | ||
* messagePath: '/components/common' | ||
* } | ||
*/ | ||
|
||
export type LocaleMessageMeta = { | ||
contentPath: string | ||
content: string | ||
component: string | ||
messagePath: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters