generaltranslation 2.0.14 → 2.0.16
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 +131 -2
- package/dist/codes/codes.js +3 -1
- package/dist/codes/getLanguageDirection.d.ts +7 -0
- package/dist/codes/getLanguageDirection.js +70 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
A language toolkit for AI developers. Used in `gt-react`.
|
|
6
6
|
|
|
7
|
-
Full documentation
|
|
7
|
+
Full documentation: <a href='https://docs.generaltranslation.com'>docs.generaltranslation.com</a>.
|
|
8
8
|
|
|
9
9
|
## Getting Started
|
|
10
10
|
|
|
@@ -76,4 +76,133 @@ Checks if a given set of codes are all equivalent to the same language. Returns
|
|
|
76
76
|
```
|
|
77
77
|
const same = isSameLanguage('en', 'en-US', 'en-GB');
|
|
78
78
|
console.log(same) // true
|
|
79
|
-
```
|
|
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
|
+
```
|
|
86
|
+
const direction = getLanguageDirection('ar');
|
|
87
|
+
console.log(direction) // 'rtl'
|
|
88
|
+
|
|
89
|
+
const direction = getLanguageDirection('en');
|
|
90
|
+
console.log(direction) // '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 isValid = isValidLanguageCode('invalid-code');
|
|
102
|
+
console.log(isValid) // 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?: { page?: 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(<div>Hello</div>, 'es');
|
|
176
|
+
console.log(result.translation); // <div>Hola</div>
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### Parameters
|
|
180
|
+
|
|
181
|
+
- `content` (any): The React children content to be translated.
|
|
182
|
+
- `targetLanguage` (string): The target language for the translation.
|
|
183
|
+
- `metadata` (object): Additional metadata for the translation process.
|
|
184
|
+
|
|
185
|
+
#### Returns
|
|
186
|
+
|
|
187
|
+
- A promise that resolves to an object containing the translated content and optional error information.
|
|
188
|
+
|
|
189
|
+
#### `bundleRequests(requests: any[]): Promise<Array<any | null>>`
|
|
190
|
+
|
|
191
|
+
Bundles multiple requests and sends them to the server.
|
|
192
|
+
|
|
193
|
+
```javascript
|
|
194
|
+
const requests = [
|
|
195
|
+
{ content: 'Hello', targetLanguage: 'es' },
|
|
196
|
+
{ content: 'World', targetLanguage: 'fr' }
|
|
197
|
+
];
|
|
198
|
+
const results = await gt.bundleRequests(requests);
|
|
199
|
+
console.log(results); // ['Hola', 'Monde']
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
#### Parameters
|
|
203
|
+
|
|
204
|
+
- `requests` (array): Array of requests to be processed and sent.
|
|
205
|
+
|
|
206
|
+
#### Returns
|
|
207
|
+
|
|
208
|
+
- A promise that resolves to an array of processed results.
|
package/dist/codes/codes.js
CHANGED
|
@@ -38,9 +38,11 @@ const Predefined = Predefined_json_1.default;
|
|
|
38
38
|
* @returns {boolean} - Returns true if valid, false otherwise.
|
|
39
39
|
*/
|
|
40
40
|
function _isValidLanguageCode(code) {
|
|
41
|
-
if (!code)
|
|
41
|
+
if (!code || typeof code !== 'string')
|
|
42
42
|
return false;
|
|
43
43
|
try {
|
|
44
|
+
if (!_mapCodeToLanguage(code.split('-')[0]))
|
|
45
|
+
return false;
|
|
44
46
|
const locale = new Intl.Locale(code);
|
|
45
47
|
const { language, script, region } = locale;
|
|
46
48
|
const constructedCode = `${language}${script ? '-' + script : ''}${region ? '-' + region : ''}`;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get the text direction for a given language code.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} code - The language code to check.
|
|
5
|
+
* @returns {string} - 'rtl' if the language is right-to-left, otherwise 'ltr'.
|
|
6
|
+
*/
|
|
7
|
+
export default function _getLanguageDirection(code: string): string;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* ----- BEGIN YAHOO COPYRIGHT ----- */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.default = _getLanguageDirection;
|
|
5
|
+
/*
|
|
6
|
+
|
|
7
|
+
Copyright 2015 Yahoo Inc. All rights reserved.
|
|
8
|
+
|
|
9
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
10
|
+
|
|
11
|
+
* Redistributions of source code must retain the above copyright
|
|
12
|
+
notice, this list of conditions and the following disclaimer.
|
|
13
|
+
|
|
14
|
+
* Redistributions in binary form must reproduce the above copyright
|
|
15
|
+
notice, this list of conditions and the following disclaimer in the
|
|
16
|
+
documentation and/or other materials provided with the distribution.
|
|
17
|
+
|
|
18
|
+
* Neither the name of the Yahoo Inc. nor the
|
|
19
|
+
names of its contributors may be used to endorse or promote products
|
|
20
|
+
derived from this software without specific prior written permission.
|
|
21
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL YAHOO! INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
22
|
+
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* List of languages that are written from right to left.
|
|
26
|
+
* Each entry contains the language code and the language name.
|
|
27
|
+
*
|
|
28
|
+
* @type {string[]}
|
|
29
|
+
*/
|
|
30
|
+
const rtlLanguages = [
|
|
31
|
+
'ae', /* Avestan */
|
|
32
|
+
'ar', /* 'العربية', Arabic */
|
|
33
|
+
'arc', /* Aramaic */
|
|
34
|
+
'bcc', /* 'بلوچی مکرانی', Southern Balochi */
|
|
35
|
+
'bqi', /* 'بختياري', Bakthiari */
|
|
36
|
+
'ckb', /* 'Soranî / کوردی', Sorani */
|
|
37
|
+
'dv', /* Dhivehi */
|
|
38
|
+
'fa', /* 'فارسی', Persian */
|
|
39
|
+
'glk', /* 'گیلکی', Gilaki */
|
|
40
|
+
'he', /* 'עברית', Hebrew */
|
|
41
|
+
'ku', /* 'Kurdî / كوردی', Kurdish */
|
|
42
|
+
'mzn', /* 'مازِرونی', Mazanderani */
|
|
43
|
+
'nqo', /* N'Ko */
|
|
44
|
+
'pnb', /* 'پنجابی', Western Punjabi */
|
|
45
|
+
'prs', /* 'دری', Darī */
|
|
46
|
+
'ps', /* 'پښتو', Pashto, */
|
|
47
|
+
'sd', /* 'سنڌي', Sindhi */
|
|
48
|
+
'ug', /* 'Uyghurche / ئۇيغۇرچە', Uyghur */
|
|
49
|
+
'ur', /* 'اردو', Urdu */
|
|
50
|
+
'yi' /* 'ייִדיש', Yiddish */
|
|
51
|
+
];
|
|
52
|
+
/* ----- END YAHOO COPYRIGHT ----- */
|
|
53
|
+
const codes_1 = require("./codes");
|
|
54
|
+
/**
|
|
55
|
+
* Get the text direction for a given language code.
|
|
56
|
+
*
|
|
57
|
+
* @param {string} code - The language code to check.
|
|
58
|
+
* @returns {string} - 'rtl' if the language is right-to-left, otherwise 'ltr'.
|
|
59
|
+
*/
|
|
60
|
+
function _getLanguageDirection(code) {
|
|
61
|
+
return rtlLanguages.some(language => {
|
|
62
|
+
const lo = (0, codes_1._getLanguageObject)(language);
|
|
63
|
+
const clo = (0, codes_1._getLanguageObject)(code);
|
|
64
|
+
if (!lo || !clo)
|
|
65
|
+
return false;
|
|
66
|
+
if (lo.script && clo.script && lo.script !== clo.script)
|
|
67
|
+
return false;
|
|
68
|
+
return (0, codes_1._isSameLanguage)(code, language);
|
|
69
|
+
}) ? 'rtl' : 'ltr';
|
|
70
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { _isValidLanguageCode, _getLanguageObject, _isSameLanguage } from './codes/codes';
|
|
2
|
+
import _getLanguageDirection from './codes/getLanguageDirection';
|
|
2
3
|
/**
|
|
3
4
|
* Type representing the constructor parameters for the GT class.
|
|
4
5
|
*/
|
|
@@ -80,6 +81,7 @@ declare class GT {
|
|
|
80
81
|
bundleRequests(requests: any[]): Promise<Array<any | null>>;
|
|
81
82
|
}
|
|
82
83
|
export default GT;
|
|
84
|
+
export declare const getLanguageDirection: typeof _getLanguageDirection;
|
|
83
85
|
export declare const isValidLanguageCode: typeof _isValidLanguageCode;
|
|
84
86
|
export declare const getLanguageObject: typeof _getLanguageObject;
|
|
85
87
|
export declare const getLanguageCode: (languages: string | string[]) => string | string[];
|
package/dist/index.js
CHANGED
|
@@ -14,9 +14,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.isSameLanguage = exports.getLanguageName = exports.getLanguageCode = exports.getLanguageObject = exports.isValidLanguageCode = void 0;
|
|
17
|
+
exports.isSameLanguage = exports.getLanguageName = exports.getLanguageCode = exports.getLanguageObject = exports.isValidLanguageCode = exports.getLanguageDirection = void 0;
|
|
18
18
|
// ----- IMPORTS ----- //
|
|
19
19
|
const codes_1 = require("./codes/codes");
|
|
20
|
+
const getLanguageDirection_1 = __importDefault(require("./codes/getLanguageDirection"));
|
|
20
21
|
const _bundleRequests_1 = __importDefault(require("./translation/_bundleRequests"));
|
|
21
22
|
const _intl_1 = __importDefault(require("./translation/_intl"));
|
|
22
23
|
const _translate_1 = __importDefault(require("./translation/_translate"));
|
|
@@ -105,6 +106,7 @@ class GT {
|
|
|
105
106
|
// Export the class
|
|
106
107
|
exports.default = GT;
|
|
107
108
|
// Export the functions
|
|
109
|
+
exports.getLanguageDirection = getLanguageDirection_1.default;
|
|
108
110
|
exports.isValidLanguageCode = codes_1._isValidLanguageCode;
|
|
109
111
|
exports.getLanguageObject = codes_1._getLanguageObject;
|
|
110
112
|
exports.getLanguageCode = codes_1._getLanguageCode;
|