Extract i18n Keys Has Fix
Description
It is tedious to manually find and replace all the text in the template with i18n keys. This rule helps to extract static text into i18n keys. Dynamic text, e.g. mustache syntax, will be skipped.
In practice, you may want to map the extracted text to a key in a dictionary file. While this rule only demonstrates the extraction part, further mapping process can be done via a script reading the output of ast-grep's --json
mode, or using @ast-grep/napi
.
YAML
yaml
id: extract-i18n-key
language: html
rule:
kind: text
pattern: $T
# skip dynamic text in mustache syntax
not: { regex: '\{\{.*\}\}' }
fix: "{{ $('$T') }}"
Example
html
<template>
<span>Hello</span>
<span>{{ text }}</span>
</template>
Diff
html
<template>
<span>Hello</span> // [!code --]
<span>{{ $('Hello') }}</span> // [!code ++]
<span>{{ text }}</span>
</template>
Contributed by
Inspired by Vue.js RFC