-
-
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.
⭐ new(api): reflect locale message meta from SFC file info
- Loading branch information
Showing
3 changed files
with
131 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,42 @@ | ||
import { LocaleMessageMeta } from '../types' | ||
import { LocaleMessageMeta, SFCFileInfo } from '../types' | ||
import { VueTemplateCompiler } from '@vue/component-compiler-utils/dist/types' | ||
|
||
export default function getLocaleMessageMeta (components: string[]): LocaleMessageMeta[] { | ||
return [] | ||
import { parse } from '@vue/component-compiler-utils' | ||
import * as compiler from 'vue-template-compiler' | ||
import path from 'path' | ||
|
||
import { debug as Debug } from 'debug' | ||
const debug = Debug('vue-i18n-locale-message:reflector') | ||
|
||
export default function reflectLocaleMessageMeta (basePath: string, components: SFCFileInfo[]): LocaleMessageMeta[] { | ||
return components.map(target => { | ||
const desc = parse({ | ||
source: target.content, | ||
filename: target.path, | ||
compiler: compiler as VueTemplateCompiler | ||
}) | ||
const { contentPath, component, messageHierarchy } = parsePath(basePath, target.path) | ||
const cm = '' | ||
return { | ||
contentPath, | ||
content: cm, | ||
blocks: desc.customBlocks, | ||
component, | ||
messageHierarchy | ||
} | ||
}) | ||
} | ||
|
||
function parsePath (basePath: string, targetPath: string) { | ||
const parsed = path.parse(targetPath) | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const [_, target] = parsed.dir.split(basePath) | ||
const parsedTargetPath = target.split(path.sep) | ||
parsedTargetPath.shift() | ||
debug(`parsePath: contentPath = ${targetPath}, component = ${parsed.name}, messageHierarchy = ${parsedTargetPath}`) | ||
return { | ||
contentPath: targetPath, | ||
component: parsed.name, | ||
messageHierarchy: parsedTargetPath | ||
} | ||
} |
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,5 +1,43 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
import getLocaleMessageMeta from '../src/reflector' | ||
import { SFCFileInfo } from '../types' | ||
import reflectLocaleMessageMeta from '../src/reflector' | ||
|
||
test('getLocaleMessageMeta', () => { | ||
test('reflectLocaleMessageMeta', () => { | ||
const componentInfo: SFCFileInfo = { | ||
path: '/path/to/project1/src/components/common/Modal.vue', | ||
content: ` | ||
<template> | ||
<!-- template contents is here ... --> | ||
</template> | ||
<script> | ||
// script codes is here ... | ||
export default {} | ||
</script> | ||
<style scoped> | ||
// css style codes is here ... | ||
</style> | ||
<i18n> | ||
{ | ||
"en": { | ||
"ok": "OK", | ||
"cancel": "Cancel" | ||
}, | ||
"ja": { | ||
"ok": "OK", | ||
"cancel": "キャンセル" | ||
} | ||
} | ||
</i18n> | ||
` | ||
} | ||
|
||
const metaInfo = reflectLocaleMessageMeta('/path/to/project1/src', [componentInfo]) | ||
expect(metaInfo.length).toBe(1) | ||
const [meta] = metaInfo | ||
expect(meta.contentPath).toBe(componentInfo.path) | ||
expect(meta.component).toBe('Modal') | ||
expect(meta.messageHierarchy).toEqual(['components', 'common']) | ||
expect(meta.blocks.length).toBe(1) | ||
}) |
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