generaltranslation 2.0.66 → 2.0.68

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. package/README.md +1 -369
  2. package/dist/codes/{determineLanguage.js → _determineLanguage.js} +5 -2
  3. package/dist/codes/_isSameLanguage.js +22 -0
  4. package/dist/codes/codes.d.ts +1 -8
  5. package/dist/codes/codes.js +61 -287
  6. package/dist/formatting/{_format.js → format.js} +40 -3
  7. package/dist/formatting/{_string_content.js → string_content.js} +21 -12
  8. package/dist/index.d.ts +60 -77
  9. package/dist/index.js +95 -73
  10. package/dist/settings/libraryDefaultLanguage.d.ts +2 -0
  11. package/dist/settings/libraryDefaultLanguage.js +4 -0
  12. package/dist/translation/dictionaries/_translateBundle.js +2 -2
  13. package/dist/translation/dictionaries/_updateProjectDictionary.js +4 -5
  14. package/dist/translation/react/_translateReact.js +2 -2
  15. package/dist/translation/strings/_translate.js +2 -2
  16. package/package.json +2 -2
  17. package/dist/codes/15924/CodeToScript.json +0 -215
  18. package/dist/codes/15924/ScriptToCode.json +0 -215
  19. package/dist/codes/3166/CodeToRegion.json +0 -296
  20. package/dist/codes/3166/RegionToCode.json +0 -296
  21. package/dist/codes/639-1/CodeToLanguage.json +0 -185
  22. package/dist/codes/639-1/LanguageToCode.json +0 -227
  23. package/dist/codes/639-3/CodeToLanguageTriletter.json +0 -186
  24. package/dist/codes/639-3/LanguageToCodeTriletter.json +0 -228
  25. package/dist/codes/getLanguageDirection.js +0 -71
  26. package/dist/codes/predefined/Predefined.json +0 -28
  27. /package/dist/codes/{determineLanguage.d.ts → _determineLanguage.d.ts} +0 -0
  28. /package/dist/codes/{getLanguageDirection.d.ts → _isSameLanguage.d.ts} +0 -0
  29. /package/dist/formatting/{_format.d.ts → format.d.ts} +0 -0
  30. /package/dist/formatting/{_string_content.d.ts → string_content.d.ts} +0 -0
package/README.md CHANGED
@@ -4,372 +4,4 @@
4
4
 
5
5
  A language toolkit for AI developers. Used in `gt-react`.
6
6
 
7
- Full documentation: <a href='https://docs.generaltranslation.com'>docs.generaltranslation.com</a>.
8
-
9
- ## Getting Started
10
-
11
- <b>In your terminal:</b>
12
-
13
- ```
14
- npm i generaltranslation
15
- ```
16
-
17
- <b>In your code, import functions directly</b>
18
-
19
- ```
20
- import { getLanguageName } from 'generaltranslation'
21
- ```
22
-
23
- or, to initialize a GT API client:
24
-
25
- ```
26
- import GT from 'generaltranslation'
27
-
28
- const gt = new GT()
29
- ```
30
-
31
- ## Convert between languages and BCP 47 language tags
32
-
33
- ### getLanguageName(codes)
34
-
35
- Returns a language name from a BCP 47 language tag, or an array of tags.
36
-
37
- ```javascript
38
- const language1 = getLanguageName('en');
39
- console.log(language1); // 'English'
40
-
41
- const language2 = getLanguageName('en-GB');
42
- console.log(language2); // 'British English'
43
-
44
- const languages = getLanguageName(['fr', 'zh-Hans']);
45
- console.log(languages); // ['French', 'Mandarin Chinese']
46
- ```
47
-
48
- ### getLanguageObject(codes)
49
-
50
- Returns a language object, or array of language objects, each containing the English name of a language, script, and region.
51
-
52
- ```javascript
53
- const languageObject = getLanguageObject('en');
54
- console.log(languageObject); // { "language": "English", "script": "", "region": "" }
55
-
56
- const languageObjects = getLanguageObject(['zh-Hans', 'en-US']);
57
- console.log(languageObjects); // [{ "language": "Chinese", "script": "Han (simplified)", "region": "" }, { "language": "English", "script": "", "region": "United States" }]
58
- ```
59
-
60
- ### getLanguageCode(languages)
61
-
62
- Returns a BCP 47 language tag from a language name or an array of language names.
63
-
64
- ```javascript
65
- const code = getLanguageCode('English');
66
- console.log(code); // 'en'
67
-
68
- const codes = getLanguageCode(['French', 'Spanish']);
69
- console.log(codes); // ['fr', 'es']
70
- ```
71
-
72
- ### isSameLanguage(...codes)
73
-
74
- Checks if a given set of codes are all equivalent to the same language. Returns a boolean.
75
-
76
- ```javascript
77
- const same = isSameLanguage('en', 'en-US', 'en-GB');
78
- console.log(same); // true
79
- ```
80
-
81
- ### getLanguageDirection(code)
82
-
83
- Returns the text direction ('rtl' for right-to-left or 'ltr' for left-to-right) for a given language code.
84
-
85
- ```javascript
86
- const direction = getLanguageDirection('ar');
87
- console.log(direction); // 'rtl'
88
-
89
- const direction2 = getLanguageDirection('en');
90
- console.log(direction2); // 'ltr'
91
- ```
92
-
93
- ### isValidLanguageCode(code)
94
-
95
- Checks if a given language-country-script code is valid. Returns a boolean indicating validity.
96
-
97
- ```javascript
98
- const isValid = isValidLanguageCode('en-US');
99
- console.log(isValid); // true
100
-
101
- const isValid2 = isValidLanguageCode('invalid-code');
102
- console.log(isValid2); // false
103
- ```
104
-
105
- ## `GT` Class
106
-
107
- The `GT` class is the core driver for the General Translation library. It provides various methods for translating content, handling internationalization, and managing translation requests.
108
-
109
- ### Constructor
110
-
111
- The `GT` class constructor initializes an instance of the GT class with optional parameters.
112
-
113
- ```javascript
114
- const gt = new GT({
115
- apiKey: 'your-api-key',
116
- defaultLanguage: 'en',
117
- projectID: 'your-project-id',
118
- baseURL: 'https://prod.gtx.dev'
119
- });
120
- ```
121
-
122
- #### Parameters
123
-
124
- - `apiKey` (string): The API key for accessing the translation service. Defaults to an empty string.
125
- - `defaultLanguage` (string): The default language for translations. Defaults to 'en'.
126
- - `projectID` (string): The project ID for the translation service. Defaults to an empty string.
127
- - `baseURL` (string): The base URL for the translation service. Defaults to 'https://prod.gtx.dev'.
128
-
129
- ### Methods
130
-
131
- #### `translate(content: string, targetLanguage: string, metadata?: { notes?: string, [key: string]: any }): Promise<{ translation: string, error?: Error | unknown }>`
132
-
133
- Translates a string into a target language.
134
-
135
- ```javascript
136
- const result = await gt.translate('Hello', 'es');
137
- console.log(result.translation); // 'Hola'
138
- ```
139
-
140
- #### Parameters
141
-
142
- - `content` (string): The string to translate.
143
- - `targetLanguage` (string): The target language for the translation.
144
- - `metadata` (object): Additional metadata for the translation request.
145
-
146
- #### Returns
147
-
148
- - A promise that resolves to an object containing the translated content and optional error information.
149
-
150
- #### `intl(content: string, targetLanguage: string, projectID?: string, metadata?: { dictionaryName?: string, notes?: string, [key: string]: any }): Promise<{ translation: string, error?: Error | unknown }>`
151
-
152
- Translates a string and caches it for use in a public project.
153
-
154
- ```javascript
155
- const result = await gt.intl('Hello', 'es', 'your-project-id');
156
- console.log(result.translation); // 'Hola'
157
- ```
158
-
159
- #### Parameters
160
-
161
- - `content` (string): The string to translate.
162
- - `targetLanguage` (string): The target language for the translation.
163
- - `projectID` (string): The ID of the project. Defaults to the instance's projectID.
164
- - `metadata` (object): Additional metadata for the translation request.
165
-
166
- #### Returns
167
-
168
- - A promise that resolves to an object containing the translated content and optional error information.
169
-
170
- #### `translateReactChildren(content: any, targetLanguage: string, metadata?: { [key: string]: any }): Promise<{ translation: any | null, error?: Error | unknown }>`
171
-
172
- Translates the content of React children elements.
173
-
174
- ```javascript
175
- const result = await gt.translateReactChildren({ "type": "div", "props": { "children": "Hello, world" }}, 'es');
176
- console.log(result.translation); // { "type": "div", "props": { "children": "Hola, mundo" } }
177
- ```
178
-
179
- #### Parameters
180
-
181
- - `requests` (Request[]): An array of request objects. Each request can be of type `translate`, `intl`, or `react`.
182
-
183
- - **Translate Request**:
184
- - `type`: `'translate'`
185
- - `data`: An object containing:
186
- - `content` (string): The string content to be translated.
187
- - `targetLanguage` (string): The target language for the translation.
188
- - `metadata` (Record<string, any>): Additional metadata for the translation process.
189
-
190
- - **Intl Request**:
191
- - `type`: `'intl'`
192
- - `data`: An object containing:
193
- - `content` (string): The string content to be translated.
194
- - `targetLanguage` (string): The target language for the translation.
195
- - `metadata` (Record<string, any>): Additional metadata for the translation process.
196
-
197
- - **React Request**:
198
- - `type`: `'intl'`
199
- - `data`: An object containing:
200
- - `children` (object | string): The React children content to be translated.
201
- - `targetLanguage` (string): The target language for the translation.
202
- - `metadata` (Record<string, any>): Additional metadata for the translation process.
203
-
204
- #### Returns
205
-
206
- - A promise that resolves to an object containing the translated content, optional error information, and dictionary information for react and intl requests.
207
-
208
- #### `bundleTranslation(requests: any[]): Promise<Array<any | null>>`
209
-
210
- Bundles multiple requests and sends them to the server.
211
-
212
- ```javascript
213
- const requests = [
214
- {
215
- type: "translate"
216
- data: {
217
- content: 'Hello', targetLanguage: 'es'
218
- }
219
- }
220
- {
221
- type: "translate"
222
- data: {
223
- content: 'Hello', targetLanguage: 'de'
224
- }
225
- }
226
-
227
- ];
228
- const results = await gt.bundleTranslation(requests);
229
- console.log(results); // [{ translation: "Hola", language: "es" }, { translation: "Hallo", language: "de" }]
230
- ```
231
-
232
- #### Parameters
233
-
234
- - `requests` (array): Array of requests to be processed and sent.
235
-
236
- #### Returns
237
-
238
- - A promise that resolves to an array of processed results.
239
-
240
- ## Utility Functions
241
-
242
- ### getLanguageDirection
243
-
244
- Gets the writing direction for a given BCP 47 language code.
245
-
246
- ```javascript
247
- import { getLanguageDirection } from 'generaltranslation';
248
-
249
- const direction = getLanguageDirection('ar');
250
- console.log(direction); // 'rtl'
251
- ```
252
-
253
- #### Parameters
254
-
255
- - `code` (string): The BCP 47 language code to check.
256
-
257
- #### Returns
258
-
259
- - The language direction ('ltr' for left-to-right or 'rtl' for right-to-left).
260
-
261
- ### isValidLanguageCode
262
-
263
- Checks if a given BCP 47 language code is valid.
264
-
265
- ```javascript
266
- import { isValidLanguageCode } from 'generaltranslation';
267
-
268
- const isValid = isValidLanguageCode('en-US');
269
- console.log(isValid); // true
270
- ```
271
-
272
- #### Parameters
273
-
274
- - `code` (string): The BCP 47 language code to validate.
275
-
276
- #### Returns
277
-
278
- - True if the BCP 47 code is valid, false otherwise.
279
-
280
- ### standardizeLanguageCode
281
-
282
- Standardizes a BCP 47 language code to ensure correct formatting.
283
-
284
- ```javascript
285
- import { standardizeLanguageCode } from 'generaltranslation';
286
-
287
- const standardizedCode = standardizeLanguageCode('en-us');
288
- console.log(standardizedCode); // 'en-US'
289
- ```
290
-
291
- #### Parameters
292
-
293
- - `code` (string): The BCP 47 language code to standardize.
294
-
295
- #### Returns
296
-
297
- - The standardized BCP 47 language code.
298
-
299
- ### getLanguageObject
300
-
301
- Gets a language object from a BCP 47 language code.
302
-
303
- ```javascript
304
- import { getLanguageObject } from 'generaltranslation';
305
-
306
- const languageObject = getLanguageObject('en');
307
- console.log(languageObject); // { language: 'English', script: '', region: '' }
308
- ```
309
-
310
- #### Parameters
311
-
312
- - `code` (string): The BCP 47 language code to convert.
313
-
314
- #### Returns
315
-
316
- - The language object or null if the BCP 47 code is invalid.
317
-
318
- ### getLanguageCode
319
-
320
- Gets a BCP 47 language code from a language name.
321
-
322
- ```javascript
323
- import { getLanguageCode } from 'generaltranslation';
324
-
325
- const languageCode = getLanguageCode('English');
326
- console.log(languageCode); // 'en'
327
- ```
328
-
329
- #### Parameters
330
-
331
- - `language` (string): The language name to convert.
332
-
333
- #### Returns
334
-
335
- - The corresponding BCP 47 language code.
336
-
337
- ### getLanguageName
338
-
339
- Gets a language name from a BCP 47 language code.
340
-
341
- ```javascript
342
- import { getLanguageName } from 'generaltranslation';
343
-
344
- const languageName = getLanguageName('en');
345
- console.log(languageName); // 'English'
346
- ```
347
-
348
- #### Parameters
349
-
350
- - `code` (string): The BCP 47 language code to convert.
351
-
352
- #### Returns
353
-
354
- - The corresponding language name.
355
-
356
- ### isSameLanguage
357
-
358
- Checks if multiple BCP 47 language codes represent the same language.
359
-
360
- ```javascript
361
- import { isSameLanguage } from 'generaltranslation';
362
-
363
- const same = isSameLanguage('en', 'en-US', 'en-GB');
364
- console
365
-
366
- .log(same); // true
367
- ```
368
-
369
- #### Parameters
370
-
371
- - `codes` (string[]): The BCP 47 language codes to compare.
372
-
373
- #### Returns
374
-
375
- - True if all BCP 47 codes represent the same language, false otherwise.
7
+ Full documentation: <a href='https://docs.generaltranslation.com'>docs.generaltranslation.com</a>.
@@ -1,7 +1,10 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.default = _determineLanguage;
4
- var codes_1 = require("./codes");
7
+ var _isSameLanguage_1 = __importDefault(require("./_isSameLanguage"));
5
8
  /**
6
9
  * Given a list of language and a list of approved language, sorted in preference order
7
10
  * Determines which language of the given languages is the best match in the approvedLanguages, prioritizing exact matches and falling back to dialects of the same language
@@ -16,7 +19,7 @@ function _determineLanguage(languages, approvedLanguages) {
16
19
  var exactMatch = approvedLanguages.find(function (approvedLanguage) { return approvedLanguage === language; });
17
20
  if (exactMatch)
18
21
  return { value: exactMatch };
19
- var sameLanguage = approvedLanguages.find(function (approvedLanguage) { return (0, codes_1._isSameLanguage)(approvedLanguage, language); });
22
+ var sameLanguage = approvedLanguages.find(function (approvedLanguage) { return (0, _isSameLanguage_1.default)(approvedLanguage, language); });
20
23
  if (sameLanguage)
21
24
  return { value: sameLanguage };
22
25
  };
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = _isSameLanguage;
4
+ /**
5
+ * @internal
6
+ */
7
+ function _isSameLanguage() {
8
+ var codes = [];
9
+ for (var _i = 0; _i < arguments.length; _i++) {
10
+ codes[_i] = arguments[_i];
11
+ }
12
+ try {
13
+ var flattenedCodes = codes.flat();
14
+ // Get the language for each code
15
+ var languages_1 = flattenedCodes.map(function (code) { return new Intl.Locale(code).language; });
16
+ return languages_1.every(function (language) { return language === languages_1[0]; });
17
+ }
18
+ catch (error) {
19
+ console.error(error);
20
+ return false;
21
+ }
22
+ }
@@ -1,8 +1 @@
1
- /**
2
- * Represents a language object with optional script and region.
3
- */
4
- export type LanguageObject = {
5
- language: string;
6
- script?: string;
7
- region?: string;
8
- };
1
+ export {};