Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wszgrcy/cyia-localize
angular localize standalone
https://github.com/wszgrcy/cyia-localize
angular i18n l10n nodejs typescript
Last synced: 1 day ago
JSON representation
angular localize standalone
- Host: GitHub
- URL: https://github.com/wszgrcy/cyia-localize
- Owner: wszgrcy
- Created: 2024-03-22T04:48:43.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2024-03-24T02:42:02.000Z (10 months ago)
- Last Synced: 2025-01-04T00:23:22.963Z (28 days ago)
- Topics: angular, i18n, l10n, nodejs, typescript
- Language: TypeScript
- Homepage:
- Size: 103 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
| [中文](https://github.com/wszgrcy/cyia-localize/blob/master/readme.zh-Hans.md) | [English](./readme.md) |
| --------------------------- | ---------------------- |---
# Introduction
- `@angular/localize` standalone version, allowed for use in any node/front-end project
- Extract/merge/convert translation files through commands `i18n` / `l10n`# Source
- Angular 17.3.1
## Usage
```ts
import { $localize } from '@cyia/localize';
$localize`one`;
```
- Use `i18n ./src` Extract all `$localize` label template contents under `src` and generate `extract.json` metadata
> Generate ID consistent with `@angular/localize`
- Copy `extract.json` custom language translation and write the translation content into the `target` field
- Use `i18n convert ./i18n-merge ./i18n` Convert translation metadata to `key-value` format for reference
- Custom reference format import translation, such as```ts
// Node environment demonstration
import path from 'path';
import fs from 'fs';
import { loadTranslations } from '@cyia/localize';
export const LanguageMap: Record = {
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();
```## Merge other translations
- If other dependency packages also use `@cyia/localize` and the published package contains translated/metadata text, use `i18n merge ./output ./pkg1 ./pkg2` merge multiple and then import them again## Update existing translations
- `i18n ./src ./i18n-meta --update --name zh-Hans`Extract the content and use `zh-Hans` as the original file (no translation required), while files in other languages will add/remove corresponding text