@translifycc/angular 0.1.2 → 0.1.4
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/fesm2022/translifycc-angular.mjs +547 -0
- package/fesm2022/translifycc-angular.mjs.map +1 -0
- package/package.json +20 -5
- package/types/translifycc-angular.d.ts +83 -0
- package/src/index.d.ts +0 -9
- package/src/index.ts +0 -3
- package/src/translate.ts +0 -112
- package/src/translify-selector.component.ts +0 -463
- package/src/translify.module.ts +0 -10
package/package.json
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@translifycc/angular",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Angular language selector component for Translify",
|
|
5
|
-
"main": "
|
|
6
|
-
"
|
|
5
|
+
"main": "dist/fesm2022/translifycc-angular.mjs",
|
|
6
|
+
"module": "fesm2022/translifycc-angular.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"default": "./fesm2022/translifycc-angular.mjs",
|
|
11
|
+
"types": "./types/translifycc-angular.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": {
|
|
14
|
+
"default": "./package.json"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
7
17
|
"peerDependencies": {
|
|
8
18
|
"@angular/common": ">=15.0.0",
|
|
9
19
|
"@angular/core": ">=15.0.0"
|
|
@@ -14,5 +24,10 @@
|
|
|
14
24
|
"translation",
|
|
15
25
|
"angular"
|
|
16
26
|
],
|
|
17
|
-
"license": "MIT"
|
|
18
|
-
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"typings": "types/translifycc-angular.d.ts",
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"tslib": "^2.3.0"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import * as i0 from '@angular/core';
|
|
2
|
+
import { OnInit, OnDestroy, NgZone, ChangeDetectorRef } from '@angular/core';
|
|
3
|
+
|
|
4
|
+
interface Language {
|
|
5
|
+
code: string;
|
|
6
|
+
name: string;
|
|
7
|
+
nativeName: string;
|
|
8
|
+
isRTL: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare class TranslifySelectorComponent implements OnInit, OnDestroy {
|
|
12
|
+
private ngZone;
|
|
13
|
+
private cdr;
|
|
14
|
+
apiKey: string;
|
|
15
|
+
apiBase: string;
|
|
16
|
+
domain: string;
|
|
17
|
+
classNames: {
|
|
18
|
+
trigger?: string;
|
|
19
|
+
dropdown?: string;
|
|
20
|
+
dropdownHeader?: string;
|
|
21
|
+
option?: string;
|
|
22
|
+
optionActive?: string;
|
|
23
|
+
optionNative?: string;
|
|
24
|
+
optionName?: string;
|
|
25
|
+
optionRtl?: string;
|
|
26
|
+
restore?: string;
|
|
27
|
+
overlay?: string;
|
|
28
|
+
overlaySpinner?: string;
|
|
29
|
+
overlayTitle?: string;
|
|
30
|
+
overlaySub?: string;
|
|
31
|
+
toast?: string;
|
|
32
|
+
};
|
|
33
|
+
labels: {
|
|
34
|
+
trigger?: string;
|
|
35
|
+
dropdownHeader?: string;
|
|
36
|
+
restore?: string;
|
|
37
|
+
overlayTitle?: string;
|
|
38
|
+
overlaySub?: string;
|
|
39
|
+
};
|
|
40
|
+
languages: Language[];
|
|
41
|
+
activeLang: string | null;
|
|
42
|
+
isOpen: boolean;
|
|
43
|
+
loadingState: 'idle' | 'overlay' | 'toast';
|
|
44
|
+
overlayVisible: boolean;
|
|
45
|
+
toastVisible: boolean;
|
|
46
|
+
private originalTexts;
|
|
47
|
+
private translationCache;
|
|
48
|
+
private currentCacheLang;
|
|
49
|
+
private isTranslating;
|
|
50
|
+
private pendingNodes;
|
|
51
|
+
private observer;
|
|
52
|
+
private debounceTimer;
|
|
53
|
+
private overlayTimer;
|
|
54
|
+
constructor(ngZone: NgZone, cdr: ChangeDetectorRef);
|
|
55
|
+
get activeLangObj(): Language | undefined;
|
|
56
|
+
ngOnInit(): Promise<void>;
|
|
57
|
+
ngOnDestroy(): void;
|
|
58
|
+
private ensureCacheLoaded;
|
|
59
|
+
private applyCache;
|
|
60
|
+
private showOverlay;
|
|
61
|
+
private hideOverlay;
|
|
62
|
+
private showToast;
|
|
63
|
+
private hideToast;
|
|
64
|
+
private flushNodes;
|
|
65
|
+
private startObserver;
|
|
66
|
+
private stopObserver;
|
|
67
|
+
private patchHistory;
|
|
68
|
+
private onRouteChange;
|
|
69
|
+
translate(targetLang: string): Promise<void>;
|
|
70
|
+
restore(): void;
|
|
71
|
+
handleSelect(langCode: string): void;
|
|
72
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<TranslifySelectorComponent, never>;
|
|
73
|
+
static ɵcmp: i0.ɵɵComponentDeclaration<TranslifySelectorComponent, "translify-selector", never, { "apiKey": { "alias": "apiKey"; "required": false; }; "apiBase": { "alias": "apiBase"; "required": false; }; "domain": { "alias": "domain"; "required": false; }; "classNames": { "alias": "classNames"; "required": false; }; "labels": { "alias": "labels"; "required": false; }; }, {}, never, never, true, never>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
declare class TranslifyModule {
|
|
77
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<TranslifyModule, never>;
|
|
78
|
+
static ɵmod: i0.ɵɵNgModuleDeclaration<TranslifyModule, never, [typeof TranslifySelectorComponent], [typeof TranslifySelectorComponent]>;
|
|
79
|
+
static ɵinj: i0.ɵɵInjectorDeclaration<TranslifyModule>;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { TranslifyModule, TranslifySelectorComponent };
|
|
83
|
+
export type { Language };
|
package/src/index.d.ts
DELETED
package/src/index.ts
DELETED
package/src/translate.ts
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
export interface Language {
|
|
2
|
-
code: string;
|
|
3
|
-
name: string;
|
|
4
|
-
nativeName: string;
|
|
5
|
-
isRTL: boolean;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const SKIP_TAGS = new Set([
|
|
9
|
-
'SCRIPT', 'STYLE', 'NOSCRIPT', 'IFRAME',
|
|
10
|
-
'CODE', 'PRE', 'SVG', 'MATH', 'INPUT', 'TEXTAREA',
|
|
11
|
-
]);
|
|
12
|
-
|
|
13
|
-
export function getSavedLang(): string | null {
|
|
14
|
-
return localStorage.getItem('translify_lang');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function saveLang(lang: string): void {
|
|
18
|
-
localStorage.setItem('translify_lang', lang);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function clearSavedLang(): void {
|
|
22
|
-
localStorage.removeItem('translify_lang');
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function loadTranslationCache(lang: string): Map<string, string> {
|
|
26
|
-
try {
|
|
27
|
-
const stored = localStorage.getItem(`translify_cache_${lang}`);
|
|
28
|
-
return stored ? new Map(Object.entries(JSON.parse(stored))) : new Map();
|
|
29
|
-
} catch {
|
|
30
|
-
return new Map();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function saveTranslationCache(lang: string, cache: Map<string, string>): void {
|
|
35
|
-
try {
|
|
36
|
-
const obj: Record<string, string> = {};
|
|
37
|
-
cache.forEach((v, k) => { obj[k] = v; });
|
|
38
|
-
localStorage.setItem(`translify_cache_${lang}`, JSON.stringify(obj));
|
|
39
|
-
} catch {}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function getTextNodes(root: Node): Text[] {
|
|
43
|
-
const nodes: Text[] = [];
|
|
44
|
-
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
|
|
45
|
-
acceptNode(node: Node) {
|
|
46
|
-
const text = (node as Text);
|
|
47
|
-
if (!text.textContent?.trim()) return NodeFilter.FILTER_REJECT;
|
|
48
|
-
if (SKIP_TAGS.has((text.parentElement?.tagName ?? ''))) return NodeFilter.FILTER_REJECT;
|
|
49
|
-
if ((text.textContent?.trim().length ?? 0) > 5000) return NodeFilter.FILTER_REJECT;
|
|
50
|
-
if (text.parentElement?.closest('[translate="no"]')) return NodeFilter.FILTER_REJECT;
|
|
51
|
-
return NodeFilter.FILTER_ACCEPT;
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
let n: Node | null;
|
|
55
|
-
while ((n = walker.nextNode())) nodes.push(n as Text);
|
|
56
|
-
return nodes;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function collectNewTextNodes(node: Node, originalTexts: Map<Node, string>): Text[] {
|
|
60
|
-
const nodes: Text[] = [];
|
|
61
|
-
if (node.nodeType === Node.TEXT_NODE) {
|
|
62
|
-
const t = node as Text;
|
|
63
|
-
if (!originalTexts.has(t) && t.textContent?.trim()) nodes.push(t);
|
|
64
|
-
} else if (node.nodeType === Node.ELEMENT_NODE) {
|
|
65
|
-
const walker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT);
|
|
66
|
-
let n: Node | null;
|
|
67
|
-
while ((n = walker.nextNode())) {
|
|
68
|
-
const t = n as Text;
|
|
69
|
-
if (
|
|
70
|
-
!originalTexts.has(t) &&
|
|
71
|
-
t.textContent?.trim() &&
|
|
72
|
-
!SKIP_TAGS.has(t.parentElement?.tagName ?? '') &&
|
|
73
|
-
!t.parentElement?.closest('[translate="no"]')
|
|
74
|
-
) {
|
|
75
|
-
nodes.push(t);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return nodes;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export async function fetchLanguages(apiBase: string): Promise<Language[]> {
|
|
83
|
-
const res = await fetch(`${apiBase}/translate/languages`);
|
|
84
|
-
const json = await res.json();
|
|
85
|
-
return json.data.languages;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export async function callTranslateApi(
|
|
89
|
-
apiBase: string,
|
|
90
|
-
apiKey: string,
|
|
91
|
-
domain: string,
|
|
92
|
-
texts: string[],
|
|
93
|
-
targetLang: string,
|
|
94
|
-
): Promise<string[]> {
|
|
95
|
-
const results: string[] = [];
|
|
96
|
-
for (let i = 0; i < texts.length; i += 100) {
|
|
97
|
-
const chunk = texts.slice(i, i + 100);
|
|
98
|
-
const res = await fetch(`${apiBase}/translate`, {
|
|
99
|
-
method: 'POST',
|
|
100
|
-
headers: {
|
|
101
|
-
'Content-Type': 'application/json',
|
|
102
|
-
'x-api-key': apiKey,
|
|
103
|
-
'x-domain': domain,
|
|
104
|
-
},
|
|
105
|
-
body: JSON.stringify({ texts: chunk, targetLanguage: targetLang, sourceLanguage: 'auto' }),
|
|
106
|
-
});
|
|
107
|
-
const json = await res.json();
|
|
108
|
-
if (!json.success) throw new Error(json.message || 'Translation failed');
|
|
109
|
-
results.push(...json.data.translations);
|
|
110
|
-
}
|
|
111
|
-
return results;
|
|
112
|
-
}
|