@rivinia/i18n 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/schematics/ng-add/index.cjs +58 -9
- package/schematics/ng-add/schema.json +3 -3
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ Headless language manager for Angular 22+. Signals, RTL/LTR, lazy loaders,
|
|
|
7
7
|
ICU plurals (all six Arabic forms), browser detection, Intl formatting —
|
|
8
8
|
zero DOM opinions beyond `<html dir/lang>`, zero styling.
|
|
9
9
|
|
|
10
|
-
- Version: **0.1.
|
|
10
|
+
- Version: **0.1.1**
|
|
11
11
|
- Requires: **Angular `^22.0.0`**, `tslib`
|
|
12
12
|
|
|
13
13
|
## Install
|
package/package.json
CHANGED
|
@@ -1,18 +1,69 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseLanguageIds = parseLanguageIds;
|
|
3
4
|
exports.ngAdd = ngAdd;
|
|
4
5
|
const schematics_1 = require("@angular-devkit/schematics");
|
|
5
6
|
const PROVIDE_IMPORT = `import { provideRiviniaI18n } from '@rivinia/i18n';`;
|
|
6
|
-
|
|
7
|
+
/** Known display names/directions; anything else gets `{ nativeName: id, dir }`. */
|
|
8
|
+
const KNOWN_LANGUAGES = {
|
|
9
|
+
ar: { nativeName: 'العربية', dir: 'rtl' },
|
|
10
|
+
en: { nativeName: 'English', dir: 'ltr' },
|
|
11
|
+
fr: { nativeName: 'Français', dir: 'ltr' },
|
|
12
|
+
de: { nativeName: 'Deutsch', dir: 'ltr' },
|
|
13
|
+
es: { nativeName: 'Español', dir: 'ltr' },
|
|
14
|
+
he: { nativeName: 'עברית', dir: 'rtl' },
|
|
15
|
+
fa: { nativeName: 'فارسی', dir: 'rtl' },
|
|
16
|
+
ur: { nativeName: 'اردو', dir: 'rtl' },
|
|
17
|
+
};
|
|
18
|
+
const RTL_IDS = new Set(['ar', 'he', 'fa', 'ur', 'ps', 'yi', 'dv', 'ku', 'sd']);
|
|
19
|
+
const STARTER = (ids, detectBrowser) => {
|
|
20
|
+
const languages = ids
|
|
21
|
+
.map((id) => {
|
|
22
|
+
const known = KNOWN_LANGUAGES[id];
|
|
23
|
+
const nativeName = known?.nativeName ?? id;
|
|
24
|
+
const dir = known?.dir ?? (RTL_IDS.has(id) ? 'rtl' : 'ltr');
|
|
25
|
+
return ` { id: '${id}', nativeName: '${nativeName}', dir: '${dir}' },`;
|
|
26
|
+
})
|
|
27
|
+
.join('\n');
|
|
7
28
|
const detect = detectBrowser ? ',\n detectBrowser: true' : '';
|
|
8
29
|
return (`provideRiviniaI18n({\n` +
|
|
9
|
-
` defaultLanguage: '${
|
|
10
|
-
` languages: [\n` +
|
|
11
|
-
` { id: 'ar', nativeName: 'العربية', dir: 'rtl' },\n` +
|
|
12
|
-
` { id: 'en', nativeName: 'English', dir: 'ltr' },\n` +
|
|
13
|
-
` ],${detect}\n` +
|
|
30
|
+
` defaultLanguage: '${ids[0]}',\n` +
|
|
31
|
+
` languages: [\n${languages}\n ],${detect}\n` +
|
|
14
32
|
` })`);
|
|
15
33
|
};
|
|
34
|
+
/**
|
|
35
|
+
* Parses a free-form language list (`ar, en`, `'ar','en'`, `AR EN`…).
|
|
36
|
+
* Never trusts raw input: strips quotes/whitespace, lowercases, validates
|
|
37
|
+
* BCP-47-ish tags, dedupes. Throws a clear error instead of emitting code.
|
|
38
|
+
*/
|
|
39
|
+
function parseLanguageIds(raw) {
|
|
40
|
+
if (!raw || raw.trim() === '')
|
|
41
|
+
return ['ar', 'en'];
|
|
42
|
+
// Accept bare lists (`ar, en`), quoted parts (`'ar', "en"`), and bracketed
|
|
43
|
+
// arrays (`[ar, en]`, `['ar','en']`) — then validate strictly regardless.
|
|
44
|
+
let text = raw.trim();
|
|
45
|
+
if (text.startsWith('['))
|
|
46
|
+
text = text.slice(1);
|
|
47
|
+
if (text.endsWith(']'))
|
|
48
|
+
text = text.slice(0, -1);
|
|
49
|
+
const ids = text
|
|
50
|
+
.split(',')
|
|
51
|
+
.map((part) => part
|
|
52
|
+
.trim()
|
|
53
|
+
.replace(/^['"`]+|['"`]+$/g, '')
|
|
54
|
+
.trim()
|
|
55
|
+
.toLowerCase())
|
|
56
|
+
.filter((id) => id.length > 0);
|
|
57
|
+
const unique = [...new Set(ids)];
|
|
58
|
+
if (unique.length === 0)
|
|
59
|
+
return ['ar', 'en'];
|
|
60
|
+
const bad = unique.filter((id) => !/^[a-z]{2,8}(-[a-z0-9]{2,8})?$/.test(id));
|
|
61
|
+
if (bad.length > 0) {
|
|
62
|
+
throw new schematics_1.SchematicsException(`[rivinia/i18n] invalid language id(s): ${bad.join(', ')}. ` +
|
|
63
|
+
`Use comma-separated codes like 'ar, en' or 'ar-EG, en'.`);
|
|
64
|
+
}
|
|
65
|
+
return unique;
|
|
66
|
+
}
|
|
16
67
|
/**
|
|
17
68
|
* `ng add @rivinia/i18n` — wires a starter bilingual config.
|
|
18
69
|
*
|
|
@@ -47,9 +98,7 @@ function ngAdd(options) {
|
|
|
47
98
|
changed = true;
|
|
48
99
|
}
|
|
49
100
|
if (!content.includes('provideRiviniaI18n(')) {
|
|
50
|
-
const call = STARTER(options.
|
|
51
|
-
? options.defaultLanguage
|
|
52
|
-
: 'ar', options.detectBrowser === true);
|
|
101
|
+
const call = STARTER(parseLanguageIds(options.languages), options.detectBrowser === true);
|
|
53
102
|
content = insertProvider(content, call);
|
|
54
103
|
changed = true;
|
|
55
104
|
}
|
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
"title": "Rivinia i18n ng-add options",
|
|
5
5
|
"type": "object",
|
|
6
6
|
"properties": {
|
|
7
|
-
"
|
|
7
|
+
"languages": {
|
|
8
8
|
"type": "string",
|
|
9
|
-
"description": "
|
|
9
|
+
"description": "Comma-separated language ids — bare (ar, en), quoted ('ar', 'en') or bracketed ([ar, en]). First id becomes the default language.",
|
|
10
10
|
"x-prompt": {
|
|
11
|
-
"message": "
|
|
11
|
+
"message": "Language ids for this project (ar, en — brackets and quotes also fine)?",
|
|
12
12
|
"type": "input"
|
|
13
13
|
}
|
|
14
14
|
},
|