-
-
Notifications
You must be signed in to change notification settings - Fork 204
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
7 changed files
with
89 additions
and
53 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
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,49 @@ | ||
/* | ||
* @Author: Bin | ||
* @Date: 2025-01-04 | ||
* @FilePath: /i18next-parser/src/lexers/vue-template-lexer.js | ||
*/ | ||
import HTMLLexer from './html-lexer.js' | ||
import JavascriptLexer from './javascript-lexer.js' | ||
import * as cheerio from 'cheerio' | ||
|
||
export default class VueTemplateLexer extends HTMLLexer { | ||
constructor(options = {}) { | ||
super(options) | ||
this.vueBindAttr = options.vueBindAttr || true | ||
this.jsLexer = new JavascriptLexer(options) | ||
} | ||
|
||
extract(content, filename = '__default.vue') { | ||
const $ = cheerio.load(content) | ||
const jsLexer = this.jsLexer | ||
let keys = [] | ||
|
||
// use HTMLLexer | ||
const HTMLkeys = super.extract(content) | ||
keys.push(...HTMLkeys) | ||
|
||
// use JavascriptLexer | ||
const JSKeys = jsLexer.extract(content, filename) | ||
keys.push(...JSKeys) | ||
|
||
// support vue bind attribute | ||
if (this.vueBindAttr) { | ||
// traverse all tags to filter bind attribute | ||
$('*').each((_, node) => { | ||
const attributes = node.attributes.filter( | ||
(item) => item.name.startsWith(':') || item.name.startsWith('v-bind:') | ||
) | ||
if (attributes.length > 0) { | ||
// there are calculation attributes. | ||
attributes.forEach((content) => { | ||
const items = jsLexer.extract(content.value, filename) | ||
keys.push(...items) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
return keys | ||
} | ||
} |
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
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,31 @@ | ||
/* | ||
* @Author: Bin | ||
* @Date: 2025-01-04 | ||
* @FilePath: /i18next-parser/test/lexers/vue-template-lexer.test.js | ||
*/ | ||
import { assert } from 'chai' | ||
import VueTemplateLexer from '../../src/lexers/vue-template-lexer.js' | ||
|
||
describe('VueTemplateLexer', () => { | ||
it('supports vue bind attributes', (done) => { | ||
const Lexer = new VueTemplateLexer({ | ||
functions: ['t', '$t'], | ||
vueBindAttr: true, | ||
}) | ||
const content = ` | ||
<template> | ||
<button :aria-label="t('b_a_l', 'button aria label')"> | ||
button label | ||
</button> | ||
<button v-bind:aria-label="$t('button v-bind aria label')"> | ||
button label form v-bind | ||
</button> | ||
</template> | ||
` | ||
assert.deepEqual(Lexer.extract(content), [ | ||
{ key: 'b_a_l', defaultValue: 'button aria label' }, | ||
{ key: 'button v-bind aria label' }, | ||
]) | ||
done() | ||
}) | ||
}) |