@alaikis/translation-sdk 1.2.1 → 1.2.3
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 +195 -0
- package/package.json +12 -3
- package/rollup.config.js +19 -0
- package/translation-client.d.ts +360 -353
- package/translation-client.js +1417 -1130
package/README.md
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
# @alaikis/translation-sdk
|
|
2
|
+
|
|
3
|
+
Laker Translation Service SDK for JavaScript/TypeScript - Simplified Single Entry Point
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @alaikis/translation-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or use directly in browser via CDN:
|
|
12
|
+
|
|
13
|
+
```html
|
|
14
|
+
<script src="https://unpkg.com/@alaikis/translation-sdk@latest/translation-client.js"></script>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quick Start
|
|
18
|
+
|
|
19
|
+
### Browser (UMD Global)
|
|
20
|
+
|
|
21
|
+
```html
|
|
22
|
+
<script src="https://unpkg.com/@alaikis/translation-sdk@latest/translation-client.js"></script>
|
|
23
|
+
<script>
|
|
24
|
+
// Global is available as window.LakerTranslation
|
|
25
|
+
// baseUrl is optional - defaults to official production endpoint
|
|
26
|
+
const client = new LakerTranslation.TranslationClient({
|
|
27
|
+
token: 'your-jwt-token',
|
|
28
|
+
senseId: 'your-sense-id'
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Translate text with optional fingerprint
|
|
32
|
+
client.translate('Hello world', 'en', 'zh', 'user-fingerprint-123')
|
|
33
|
+
.then(result => console.log(result));
|
|
34
|
+
</script>
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### ES Module / CommonJS (Node.js/Bundlers)
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { TranslationClient, createTranslation } from '@alaikis/translation-sdk';
|
|
41
|
+
|
|
42
|
+
// Option 1: Use constructor directly
|
|
43
|
+
// baseUrl is optional - defaults to official production endpoint
|
|
44
|
+
const client = new TranslationClient({
|
|
45
|
+
token: 'your-jwt-token',
|
|
46
|
+
senseId: 'your-sense-id'
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Option 2: Use helper function (recommended)
|
|
50
|
+
// baseUrl is optional in the 3rd parameter
|
|
51
|
+
const client = createTranslation('your-jwt-token', 'your-sense-id');
|
|
52
|
+
|
|
53
|
+
// With custom baseUrl
|
|
54
|
+
const client = createTranslation('your-jwt-token', 'your-sense-id', {
|
|
55
|
+
baseUrl: 'https://api.your-custom-domain.com'
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## API Reference
|
|
60
|
+
|
|
61
|
+
### `TranslationClient` Constructor
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
new TranslationClient(options: TranslationClientOptions)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
**Options:**
|
|
68
|
+
```typescript
|
|
69
|
+
{
|
|
70
|
+
baseUrl?: string; // API base URL, optional, default: https://api.hottol.com/laker/
|
|
71
|
+
token: string; // JWT authentication token (required)
|
|
72
|
+
senseId: string; // Translation sense ID (required)
|
|
73
|
+
fingerprint?: string; // Default client-level fingerprint (optional)
|
|
74
|
+
maxCacheSize?: number; // Max LRU cache size for LLM translations, default: 10000
|
|
75
|
+
crossTabSync?: {
|
|
76
|
+
enabled: boolean; // Enable cross-tab synchronization, default: false
|
|
77
|
+
channelName?: string;
|
|
78
|
+
storageKeyPrefix?: string;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Methods
|
|
84
|
+
|
|
85
|
+
#### `translate(text, srcLang?, dstLang?, fingerprint?)`
|
|
86
|
+
|
|
87
|
+
Translate text. First lookup in cache, if not found request from backend and cache result.
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
async translate(
|
|
91
|
+
text: string,
|
|
92
|
+
srcLang?: string,
|
|
93
|
+
dstLang?: string,
|
|
94
|
+
fingerprint?: string // Override client-level fingerprint (optional)
|
|
95
|
+
): Promise<string>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
**Lookup Priority:**
|
|
99
|
+
1. If `fingerprint` parameter provided → search that fingerprint's cache
|
|
100
|
+
2. If client has default fingerprint set (`options.fingerprint`) → search that
|
|
101
|
+
3. Fallback to common translations
|
|
102
|
+
4. If not found in any cache → request from backend and cache
|
|
103
|
+
|
|
104
|
+
#### `lookup(text, fingerprint?)`
|
|
105
|
+
|
|
106
|
+
Lookup translation only from cache, no network request if not found.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
lookup(text: string, fingerprint?: string): string | null
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Returns the translated string if found, `null` otherwise.
|
|
113
|
+
|
|
114
|
+
#### `loadFingerprint(fingerprint)`
|
|
115
|
+
|
|
116
|
+
Preload all cached translations for a specific fingerprint from backend.
|
|
117
|
+
Called automatically if you call `translate()` with an unloaded fingerprint.
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
async loadFingerprint(fingerprint: string): Promise<TranslationItem[]>
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### `getStats()`
|
|
124
|
+
|
|
125
|
+
Get cache statistics.
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
getStats(): string
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
#### `extractTemplate(text)`
|
|
132
|
+
|
|
133
|
+
Extract template from text with numeric placeholders (e.g. `Page 1 of 10` → `Page %1 of %2`).
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
extractTemplate(text: string): ExtractedTemplate
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Fingerprint Personalization
|
|
140
|
+
|
|
141
|
+
The SDK supports per-user personalized translations via fingerprints. Every translation lookup/request can specify a fingerprint.
|
|
142
|
+
|
|
143
|
+
**Example with multiple users:**
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
// Client has no default fingerprint
|
|
147
|
+
const client = new TranslationClient({
|
|
148
|
+
baseUrl: 'https://api.example.com',
|
|
149
|
+
token: 'token',
|
|
150
|
+
senseId: 'sense-123'
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// Translate for user 1
|
|
154
|
+
const translation1 = await client.translate(text, 'en', 'zh', 'fingerprint-user-1');
|
|
155
|
+
|
|
156
|
+
// Translate for user 2
|
|
157
|
+
const translation2 = await client.translate(text, 'en', 'zh', 'fingerprint-user-2');
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
**Example with fixed client fingerprint:**
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
// Client has default fingerprint for current user
|
|
164
|
+
const client = new TranslationClient({
|
|
165
|
+
baseUrl: 'https://api.example.com',
|
|
166
|
+
token: 'token',
|
|
167
|
+
senseId: 'sense-123',
|
|
168
|
+
fingerprint: 'current-user-fingerprint'
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// Uses client fingerprint by default
|
|
172
|
+
const result = await client.translate(text);
|
|
173
|
+
|
|
174
|
+
// Override for a specific translation
|
|
175
|
+
const specialResult = await client.translate(text, 'en', 'zh', 'another-fingerprint');
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Features
|
|
179
|
+
|
|
180
|
+
- **Single Entry Point**: All functionality in `TranslationClient`, no complex layering
|
|
181
|
+
- **Lazy Loading**: Fingerprints are automatically loaded on first use
|
|
182
|
+
- **LRU Cache**: Caches LLM translation results to avoid unnecessary requests
|
|
183
|
+
- **Multi-fingerprint Support**: Isolated caches for different users/personalizations
|
|
184
|
+
- **Cross-tab Synchronization**: Optional broadcast channel sync between browser tabs
|
|
185
|
+
- **UMD Bundle**: Works everywhere - browser, Node.js, bundlers
|
|
186
|
+
|
|
187
|
+
## Backend Compatibility
|
|
188
|
+
|
|
189
|
+
This SDK is compatible with backend changes:
|
|
190
|
+
- Backend returns single `result` field (instead of `special` / `common`)
|
|
191
|
+
- Backend returns special translation when fingerprint has entry, otherwise returns common
|
|
192
|
+
|
|
193
|
+
## License
|
|
194
|
+
|
|
195
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alaikis/translation-sdk",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "Laker Translation Service SDK for TypeScript/JavaScript",
|
|
5
5
|
"main": "translation-client.js",
|
|
6
6
|
"types": "translation-client.d.ts",
|
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
"*.js",
|
|
9
9
|
"*.d.ts"
|
|
10
10
|
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "npx tsc && npx rollup --config rollup.config.js"
|
|
13
|
+
},
|
|
11
14
|
"keywords": [
|
|
12
15
|
"translation",
|
|
13
16
|
"grpc-web",
|
|
@@ -24,5 +27,11 @@
|
|
|
24
27
|
"bugs": {
|
|
25
28
|
"url": "https://github.com/alaikis/laker-translate-sdk/issues"
|
|
26
29
|
},
|
|
27
|
-
"homepage": "https://github.com/alaikis/laker-translate-sdk#readme"
|
|
28
|
-
|
|
30
|
+
"homepage": "https://github.com/alaikis/laker-translate-sdk#readme",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
33
|
+
"rollup": "^4.60.0",
|
|
34
|
+
"tslib": "^2.8.1",
|
|
35
|
+
"typescript": "^6.0.2"
|
|
36
|
+
}
|
|
37
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import typescript from '@rollup/plugin-typescript';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
input: 'translation-client.ts',
|
|
5
|
+
output: [
|
|
6
|
+
{
|
|
7
|
+
file: 'translation-client.js',
|
|
8
|
+
format: 'umd',
|
|
9
|
+
name: 'LakerTranslation',
|
|
10
|
+
sourcemap: false
|
|
11
|
+
}
|
|
12
|
+
],
|
|
13
|
+
plugins: [typescript({
|
|
14
|
+
target: 'es2015',
|
|
15
|
+
declaration: true,
|
|
16
|
+
declarationDir: './',
|
|
17
|
+
strict: false
|
|
18
|
+
})]
|
|
19
|
+
};
|