中文 | English |
---|
@angular/localize
standalone version, allowed for use in any node/front-end project- Extract/merge/convert translation files through commands
i18n
/l10n
- Angular 17.3.1
import { $localize } from '@cyia/localize';
$localize`one`;
- Use
i18n ./src
Extract all$localize
label template contents undersrc
and generateextract.json
metadataGenerate ID consistent with
@angular/localize
- Copy
extract.json
custom language translation and write the translation content into thetarget
field - Use
i18n convert ./i18n-merge ./i18n
Convert translation metadata tokey-value
format for reference - Custom reference format import translation, such as
// Node environment demonstration
import path from 'path';
import fs from 'fs';
import { loadTranslations } from '@cyia/localize';
export const LanguageMap: Record<string, string> = {
zh_cn: 'zh-Hans',
cn: 'zh-Hans',
en: 'en-US',
en_us: 'en-US',
};
export function loadI18n() {
let lang = process.env['CR_LANG']?.toLowerCase();
if (!lang) {
if (process.env['LANGUAGE']) {
lang = process.env['LANGUAGE'].split(':')[0].toLowerCase();
} else if (process.env['LANG']) {
lang = process.env['LANG'].split('.')[0].toLowerCase();
}
}
let cache;
const filePath = path.join(__dirname, `./i18n/${LanguageMap[lang!] || lang || 'zh-Hans'}.json`);
if (ENV === 'test') {
cache = {};
} else {
if (!fs.existsSync(filePath)) {
cache = __non_webpack_require__('./i18n/zh-Hans.json');
} else {
cache = __non_webpack_require__(filePath);
}
}
loadTranslations(cache);
}
loadI18n();
- If other dependency packages also use
@cyia/localize
and the published package contains translated/metadata text, usei18n merge ./output ./pkg1 ./pkg2
merge multiple and then import them again
i18n ./src ./i18n-meta --update --name zh-Hans
Extract the content and usezh-Hans
as the original file (no translation required), while files in other languages will add/remove corresponding text