Skip to content

Commit 73480f5

Browse files
doc: add i18n html example
fix #643
1 parent e246c2e commit 73480f5

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Extract i18n Keys <Badge type="tip" text="Has Fix" />
2+
3+
* [Playground Link](/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6Imh0bWwiLCJxdWVyeSI6IiIsInJld3JpdGUiOiIiLCJzdHJpY3RuZXNzIjoicmVsYXhlZCIsInNlbGVjdG9yIjoiIiwiY29uZmlnIjoicnVsZTpcbiAga2luZDogdGV4dFxuICBwYXR0ZXJuOiAkVFxuICBub3Q6XG4gICAgcmVnZXg6ICdcXHtcXHsuKlxcfVxcfSdcbmZpeDogXCJ7eyAkKCckVCcpIH19XCIiLCJzb3VyY2UiOiI8dGVtcGxhdGU+XG4gIDxzcGFuPkhlbGxvPC9zcGFuPlxuICA8c3Bhbj57eyB0ZXh0IH19PC9zcGFuPlxuPC90ZW1wbGF0ZT4ifQ==)
4+
5+
### Description
6+
7+
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.
8+
9+
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`](/guide/tools/json.html) mode, or using [`@ast-grep/napi`](/guide/api-usage/js-api.html).
10+
11+
### YAML
12+
```yaml
13+
rule:
14+
kind: text
15+
pattern: $T
16+
# skip dynamic text in mustache syntax
17+
not: { regex: '\{\{.*\}\}' }
18+
fix: "{{ $('$T') }}"
19+
```
20+
21+
### Example
22+
23+
<!-- highlight matched code in curly-brace {lineNum} -->
24+
```html {2}
25+
<template>
26+
<span>Hello</span>
27+
<span>{{ text }}</span>
28+
</template>
29+
```
30+
31+
### Diff
32+
<!-- use // [!code --] and // [!code ++] to annotate diff -->
33+
```html
34+
<template>
35+
<span>Hello</span> // [!code --]
36+
<span>{{ $('Hello') }}</span> // [!code ++]
37+
<span>{{ text }}</span>
38+
</template>
39+
```
40+
41+
### Contributed by
42+
Inspired by [Vue.js RFC](https://github.com/vuejs/rfcs/discussions/705#discussion-7255672)

website/catalog/html/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ You can leverage the [`languageGlobs`](/reference/sgconfig.html#languageglobs) o
88
**Caveat**: This approach may not parse framework-specific syntax, like Astro's [frontmatter script](https://docs.astro.build/en/basics/astro-components/#the-component-script) or [Svelte control flow](https://svelte.dev/docs/svelte/if). You will need to load [custom languages](/advanced/custom-language.html) for such cases.
99
:::
1010

11-
<!--@include: ./upgrade-ant-design-vue.md-->
11+
<!--@include: ./upgrade-ant-design-vue.md-->
12+
<!--@include: ./extract-i18n-key.md-->

website/catalog/html/upgrade-ant-design-vue.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ The rule can be broken down into the following steps:
2020
<!-- Use YAML in the example. Delete this section if use pattern. -->
2121
### YAML
2222
```yaml
23+
id: upgrade-ant-design-vue
24+
language: HTML
2325
utils:
2426
inside-tag:
2527
# find the enclosing element of the attribute
@@ -57,7 +59,7 @@ fix: :open
5759

5860
### Diff
5961
<!-- use // [!code --] and // [!code ++] to annotate diff -->
60-
```js
62+
```html
6163
<template>
6264
<a-modal :visible="visible">content</a-modal> // [!code --]
6365
<a-modal :open="visible">content</a-modal> // [!code ++]

website/catalog/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Feel free to join our [Discord](https://discord.gg/4YZjf6htSQ) channel and ask @
1717
* [Find function declarations with names of certain pattern](/catalog/go/#find-function-declarations-with-names-of-certain-pattern)
1818
* [HTML](/catalog/go/)
1919
* [Upgrade Ant Design Vue](/catalog/html/#upgrade-ant-design-vue)
20+
* [Extract i18n Keys](/catalog/html/#extract-i18n-keys)
2021
* [Java](/catalog/java/)
2122
* [No Unused Vars](/catalog/java/#no-unused-vars)
2223
* [Python](/catalog/python/)

0 commit comments

Comments
 (0)