@rolatech/angular-i18n 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ # angular-i18n
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Running unit tests
6
+
7
+ Run `nx test angular-i18n` to execute the unit tests.
@@ -0,0 +1,82 @@
1
+ import * as i0 from '@angular/core';
2
+ import { signal, computed, Injectable, inject, Pipe } from '@angular/core';
3
+
4
+ class I18nService {
5
+ localeSignal = signal('en', ...(ngDevMode ? [{ debugName: "localeSignal" }] : []));
6
+ dictionariesSignal = signal({}, ...(ngDevMode ? [{ debugName: "dictionariesSignal" }] : []));
7
+ locale = this.localeSignal.asReadonly();
8
+ dictionary = computed(() => {
9
+ const values = Object.values(this.dictionariesSignal());
10
+ return values.reduce((acc, current) => deepMerge(acc, current), {});
11
+ }, ...(ngDevMode ? [{ debugName: "dictionary" }] : []));
12
+ setLocale(locale) {
13
+ this.localeSignal.set(locale);
14
+ }
15
+ register(namespace, dictionary) {
16
+ this.dictionariesSignal.update((current) => ({
17
+ ...current,
18
+ [namespace]: deepMerge(current[namespace] ?? {}, dictionary),
19
+ }));
20
+ }
21
+ clear() {
22
+ this.dictionariesSignal.set({});
23
+ }
24
+ t(key, fallback) {
25
+ const value = getByPath(this.dictionary(), key);
26
+ return typeof value === 'string' ? value : (fallback ?? key);
27
+ }
28
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.1", ngImport: i0, type: I18nService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
29
+ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.1", ngImport: i0, type: I18nService, providedIn: 'root' });
30
+ }
31
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.1", ngImport: i0, type: I18nService, decorators: [{
32
+ type: Injectable,
33
+ args: [{ providedIn: 'root' }]
34
+ }] });
35
+ function getByPath(obj, path) {
36
+ return path.split('.').reduce((acc, part) => {
37
+ if (acc && typeof acc === 'object' && part in acc) {
38
+ return acc[part];
39
+ }
40
+ return undefined;
41
+ }, obj);
42
+ }
43
+ function deepMerge(target, source) {
44
+ if (!isObject(target)) {
45
+ target = {};
46
+ }
47
+ if (!isObject(source)) {
48
+ return source;
49
+ }
50
+ const output = { ...target };
51
+ for (const [key, value] of Object.entries(source)) {
52
+ output[key] = key in output ? deepMerge(output[key], value) : value;
53
+ }
54
+ return output;
55
+ }
56
+ function isObject(value) {
57
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
58
+ }
59
+
60
+ class TranslatePipe {
61
+ i18n = inject(I18nService);
62
+ transform(key, fallback) {
63
+ return this.i18n.t(key, fallback);
64
+ }
65
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.1", ngImport: i0, type: TranslatePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe });
66
+ static ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "21.2.1", ngImport: i0, type: TranslatePipe, isStandalone: true, name: "translate", pure: false });
67
+ }
68
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.1", ngImport: i0, type: TranslatePipe, decorators: [{
69
+ type: Pipe,
70
+ args: [{
71
+ name: 'translate',
72
+ standalone: true,
73
+ pure: false,
74
+ }]
75
+ }] });
76
+
77
+ /**
78
+ * Generated bundle index. Do not edit.
79
+ */
80
+
81
+ export { I18nService, TranslatePipe };
82
+ //# sourceMappingURL=rolatech-angular-i18n.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rolatech-angular-i18n.mjs","sources":["../../../../packages/angular-i18n/src/lib/service/i18n.service.ts","../../../../packages/angular-i18n/src/lib/translate.pipe.ts","../../../../packages/angular-i18n/src/rolatech-angular-i18n.ts"],"sourcesContent":["import { Injectable, computed, signal } from '@angular/core';\n\nexport type TranslationDictionary = Record<string, unknown>;\n\n@Injectable({ providedIn: 'root' })\nexport class I18nService {\n private readonly localeSignal = signal('en');\n private readonly dictionariesSignal = signal<Record<string, TranslationDictionary>>({});\n\n readonly locale = this.localeSignal.asReadonly();\n\n readonly dictionary = computed(() => {\n const values = Object.values(this.dictionariesSignal());\n return values.reduce<TranslationDictionary>((acc, current) => deepMerge(acc, current), {});\n });\n\n setLocale(locale: string): void {\n this.localeSignal.set(locale);\n }\n\n register(namespace: string, dictionary: TranslationDictionary): void {\n this.dictionariesSignal.update((current) => ({\n ...current,\n [namespace]: deepMerge(current[namespace] ?? {}, dictionary),\n }));\n }\n\n clear(): void {\n this.dictionariesSignal.set({});\n }\n\n t(key: string, fallback?: string): string {\n const value = getByPath(this.dictionary(), key);\n return typeof value === 'string' ? value : (fallback ?? key);\n }\n}\n\nfunction getByPath(obj: unknown, path: string): unknown {\n return path.split('.').reduce<unknown>((acc, part) => {\n if (acc && typeof acc === 'object' && part in (acc as Record<string, unknown>)) {\n return (acc as Record<string, unknown>)[part];\n }\n return undefined;\n }, obj);\n}\n\nfunction deepMerge(target: unknown, source: unknown): any {\n if (!isObject(target)) {\n target = {};\n }\n if (!isObject(source)) {\n return source;\n }\n\n const output: Record<string, unknown> = { ...(target as Record<string, unknown>) };\n\n for (const [key, value] of Object.entries(source)) {\n output[key] = key in output ? deepMerge(output[key], value) : value;\n }\n\n return output;\n}\n\nfunction isObject(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n","import { Pipe, PipeTransform, inject } from '@angular/core';\nimport { I18nService } from './service/i18n.service';\n\n@Pipe({\n name: 'translate',\n standalone: true,\n pure: false,\n})\nexport class TranslatePipe implements PipeTransform {\n private readonly i18n = inject(I18nService);\n\n transform(key: string, fallback?: string): string {\n return this.i18n.t(key, fallback);\n }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;MAKa,WAAW,CAAA;AACL,IAAA,Y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OAAO,CAAC,KAAK,CAAC;AAC7E;;MCzDa,aAAa,CAAA;AACP,IAAA,IAAI,GAAG,MAAM,CAAC,WAAW,CAAC;IAE3C,SAAS,CAAC,GAAW,EAAE,QAAiB,EAAA;QACtC,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,EAAE,QAAQ,CAAC;IACnC;uGALW,aAAa,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,IAAA,EAAA,CAAA;qGAAb,aAAa,EAAA,YAAA,EAAA,IAAA,EAAA,IAAA,EAAA,WAAA,EAAA,IAAA,EAAA,KAAA,EAAA,CAAA;;2FAAb,aAAa,EAAA,UAAA,EAAA,CAAA;kBALzB,IAAI;AAAC,YAAA,IAAA,EAAA,CAAA;AACJ,oBAAA,IAAI,EAAE,WAAW;AACjB,oBAAA,UAAU,EAAE,IAAI;AAChB,oBAAA,IAAI,EAAE,KAAK;AACZ,iBAAA;;;ACPD;;AAEG;;;;"}
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@rolatech/angular-i18n",
3
+ "version": "0.0.1",
4
+ "private": false,
5
+ "peerDependencies": {
6
+ "@angular/common": "^21.2.0",
7
+ "@angular/core": "^21.2.0"
8
+ },
9
+ "sideEffects": false,
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/viablecell/rolatech-web.git"
13
+ },
14
+ "license": "MIT",
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "module": "fesm2022/rolatech-angular-i18n.mjs",
19
+ "typings": "types/rolatech-angular-i18n.d.ts",
20
+ "exports": {
21
+ "./package.json": {
22
+ "default": "./package.json"
23
+ },
24
+ ".": {
25
+ "types": "./types/rolatech-angular-i18n.d.ts",
26
+ "default": "./fesm2022/rolatech-angular-i18n.mjs"
27
+ }
28
+ },
29
+ "dependencies": {
30
+ "tslib": "^2.3.0"
31
+ }
32
+ }
@@ -0,0 +1,26 @@
1
+ import * as i0 from '@angular/core';
2
+ import { PipeTransform } from '@angular/core';
3
+
4
+ type TranslationDictionary = Record<string, unknown>;
5
+ declare class I18nService {
6
+ private readonly localeSignal;
7
+ private readonly dictionariesSignal;
8
+ readonly locale: i0.Signal<string>;
9
+ readonly dictionary: i0.Signal<TranslationDictionary>;
10
+ setLocale(locale: string): void;
11
+ register(namespace: string, dictionary: TranslationDictionary): void;
12
+ clear(): void;
13
+ t(key: string, fallback?: string): string;
14
+ static ɵfac: i0.ɵɵFactoryDeclaration<I18nService, never>;
15
+ static ɵprov: i0.ɵɵInjectableDeclaration<I18nService>;
16
+ }
17
+
18
+ declare class TranslatePipe implements PipeTransform {
19
+ private readonly i18n;
20
+ transform(key: string, fallback?: string): string;
21
+ static ɵfac: i0.ɵɵFactoryDeclaration<TranslatePipe, never>;
22
+ static ɵpipe: i0.ɵɵPipeDeclaration<TranslatePipe, "translate", true>;
23
+ }
24
+
25
+ export { I18nService, TranslatePipe };
26
+ export type { TranslationDictionary };