Skip to content

Commit 109d7e1

Browse files
committed
implement i18n-id for identity management
Note that when there is no COMPILE_ID provided all contents are shown
1 parent 98f0780 commit 109d7e1

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,24 @@ COMPILE_LANG=zh NODE_ENV=production yarn build
1515

1616
Run `yarn build i18n:extract`, and languages defined in `config.langsKeyToGenerate` in gulpfile.js will be updated to `./i18n/` . This operation will not discard the old translations.
1717

18-
After translated strings in the i18n folder, run `yarn build` with environment variable `COMPILE_LANG` will generate the compiled HTML in `./dist/` .
18+
After translated strings in the i18n folder, run `yarn build` with environment variable `COMPILE_LANG` will generate the compiled HTML in `./dist/` .
19+
20+
## How to use i18n-id
21+
22+
```html
23+
<div i18n-id="id1">content</div>
24+
```
25+
26+
Adding the `i18n-id` attribute for a tag can control whether the tag will be rendered given compile-time option `COMPILE_ID`, e.g. the following command would render the tag whereas when `COMPILE_ID=id2` the tag would not be included.
27+
28+
```
29+
COMPILE_ID=id1 yarn build
30+
```
31+
32+
Note that when `COMPILE_ID=''` or when this option is not provided, all id are assumed and all contents are rendered.
33+
34+
```html
35+
<div i18n-id="id1" i18n-if="zh">content</div>
36+
```
37+
38+
Another advanced usage example. When `COMPILE_ID=id1` and `COMPILE_LANG=zh` the tag would be shown.

gulpfile.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const config = {
22
langs: ['zh', 'en'],
33
langsKeyToGenerate: ['zh'],
44
compileLang: process.env.COMPILE_LANG || 'zh',
5+
compileId: process.env.COMPILE_ID || '',
56
i18nPath: './i18n/',
67
i18nAttrs: ['title', 'class'],
78
isProduction: process.env.NODE_ENV === 'production'
@@ -43,7 +44,7 @@ const htmlCompile = () => src('./*.html')
4344
lang = YAML.parse(fs.readFileSync(config.i18nPath + getLangFileName(file.basename, config.compileLang), 'utf-8'));
4445
} catch (_) {}
4546
const $ = cheerio.load(file.contents.toString(), { decodeEntities: false });
46-
const i18nElements = $(['[i18n]', '[i18n-if]', '[i18n-key]', ...config.i18nAttrs.map(s => `[i18n-${s}]`)].join(',')).map((_, e) => e).get();
47+
const i18nElements = $(['[i18n]', '[i18n-if]', '[i18n-id]', '[i18n-key]', ...config.i18nAttrs.map(s => `[i18n-${s}]`)].join(',')).map((_, e) => e).get();
4748
for(let i in i18nElements) {
4849
const e = i18nElements[i];
4950
let val;
@@ -75,6 +76,14 @@ const htmlCompile = () => src('./*.html')
7576
$(e).remove();
7677
}
7778
}
79+
80+
if (typeof $(e).attr('i18n-id') === 'string') {
81+
if ($(e).attr('i18n-id').includes(config.compileId)) {
82+
$(e).removeAttr('i18n-id');
83+
} else {
84+
$(e).remove();
85+
}
86+
}
7887
}
7988

8089
const gaTrackId = (process.env.GOOGLE_ANALYTICS_ID || '').trim();

0 commit comments

Comments
 (0)