generaltranslation 1.1.25 → 1.1.27

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 CHANGED
@@ -14,18 +14,18 @@ Full documentation coming soon!
14
14
  npm i generaltranslation
15
15
  ```
16
16
 
17
- <b>In your code:</b>
17
+ <b>In your code, import functions directly</b>
18
18
 
19
19
  ```
20
- import GT from 'generaltranslation'
21
-
22
- const gt = new GT()
20
+ import { getLanguageName } from 'generaltranslation'
23
21
  ```
24
22
 
25
- or, import functions directly:
23
+ or, to initialize a GT API client:
26
24
 
27
25
  ```
28
- import { getLanguageName } from 'generaltranslation'
26
+ import GT from 'generaltranslation'
27
+
28
+ const gt = new GT()
29
29
  ```
30
30
 
31
31
  ## Convert between languages and ISO-639 codes
@@ -35,10 +35,10 @@ import { getLanguageName } from 'generaltranslation'
35
35
  Returns a language name from a two or three-letter ISO-639 language code, or an array of codes.
36
36
 
37
37
  ```
38
- const language = gt.getLanguageName('en');
38
+ const language = getLanguageName('en');
39
39
  console.log(language) // 'English'
40
40
 
41
- const languages = gt.getLanguageName(['fr', 'es'])
41
+ const languages = getLanguageName(['fr', 'es'])
42
42
  console.log(languages) // ['French', 'Spanish']
43
43
  ```
44
44
 
@@ -47,10 +47,10 @@ console.log(languages) // ['French', 'Spanish']
47
47
  Returns an ISO-639 code from a language name or an array of language names.
48
48
 
49
49
  ```
50
- const code = gt.getLanguageCode('English');
50
+ const code = getLanguageCode('English');
51
51
  console.log(language) // 'en'
52
52
 
53
- const codes = gt.getLanguageCodes(['French', 'Spanish'])
53
+ const codes = getLanguageCodes(['French', 'Spanish'])
54
54
  console.log(codes) // ['fr', 'es']
55
55
  ```
56
56
 
@@ -58,25 +58,13 @@ console.log(codes) // ['fr', 'es']
58
58
 
59
59
  ### getUserLanguage()
60
60
 
61
- Returns a user's default browser language. Meant for use in a web browser.
61
+ Returns a user's default browser language. Meant for use in a web browser (i.e. on the client side).
62
62
 
63
63
  ```
64
64
  const userLanguage = getUserLanguage();
65
65
  console.log(userLanguage) // 'en'
66
66
  ```
67
67
 
68
- If invoked on an instance of the GT class, this function will return the default language of that instance, similar to accessing the `defaultLanguage` property.
69
-
70
- ```
71
- import GT from 'generaltranslation'
72
-
73
- const gt = new GT({
74
- defaultLanguage: 'en'
75
- });
76
-
77
- console.log(gt.getUserLanguage()) // same as gt.defaultLanguage
78
- ```
79
-
80
68
  ## Prompt Internationalization API
81
69
 
82
70
  For this function, you need to sign up for an API key at <a href='https://generaltranslation.com' target='_blank'>generaltranslation.com</a>.
@@ -152,7 +140,7 @@ We continually benchmark AI models and add new models as they are released. That
152
140
  Get the latest list of models for which there is data. Returns an array of model names.
153
141
 
154
142
  ```
155
- const models = await gt.getModelList();
143
+ const models = await getModelList();
156
144
  console.log(models) // ['gpt-4', ... ]
157
145
  ```
158
146
 
@@ -161,7 +149,7 @@ console.log(models) // ['gpt-4', ... ]
161
149
  Get all languages known to be compatible with a given AI model. Returns an array of languages codes, or null if the model is unknown.
162
150
 
163
151
  ```
164
- const languages = await gt.getModelLanguages('mixtral-8x7b')
152
+ const languages = await getModelLanguages('mixtral-8x7b')
165
153
  console.log(languages) // ['en', 'fr', 'de', 'es', 'it']
166
154
  ```
167
155
 
@@ -170,6 +158,6 @@ console.log(languages) // ['en', 'fr', 'de', 'es', 'it']
170
158
  Returns true if a model is known to be compatible with a given language, represented by an ISO-639 language code. Returns false otherwise.
171
159
 
172
160
  ```
173
- const supported = await gt.isSupportedLanguage('gpt-4', 'fr')
161
+ const supported = await isSupportedLanguage('gpt-4', 'fr')
174
162
  console.log(supported) // true
175
163
  ```
@@ -2,7 +2,7 @@
2
2
  // e.g. 'en-US', not necessarily 'en'
3
3
  // generaltranslation handles this well, but other libraries may not
4
4
  // (it's a skill issue I'm afraid)
5
- export default function _getUserLanguage({defaultLanguage = ''} = {}) {
5
+ export default function _getUserLanguage({ defaultLanguage = '' } = {}) {
6
6
  if (typeof window !== 'undefined' && window?.navigator) {
7
7
  return navigator?.language || navigator?.userLanguage || defaultLanguage || '';
8
8
  } else if (defaultLanguage) {
package/codes/codes.js CHANGED
@@ -11,14 +11,14 @@ import LanguageToCodeTriletter from './639-3/LanguageToCodeTriletter.js'
11
11
 
12
12
  // Returns the name of a language from an ISO 639 code or an array of codes
13
13
  const _mapCodeToLanguage = code => {
14
- if (code.length === 2) {
14
+ if (code?.length === 2) {
15
15
  return CodeToLanguage[code]
16
16
  }
17
- if (code.length === 3) {
17
+ if (code?.length === 3) {
18
18
  CodeToLanguageTriletter[code]
19
19
  }
20
20
  else {
21
- return CodeToLanguage[code.slice(0, 2)] || '';
21
+ return CodeToLanguage[code?.slice(0, 2)] || '';
22
22
  }
23
23
  }
24
24
  export const _getLanguageName = codes => {
@@ -28,7 +28,7 @@ export const _getLanguageName = codes => {
28
28
  // Returns an ISO 639 code from a language name or an array of language names
29
29
  // Preferentially returns two-letter codes
30
30
  const _mapLanguageToCode = language => {
31
- const lowerCaseLanguage = language.toLowerCase();
31
+ const lowerCaseLanguage = language?.toLowerCase();
32
32
  return LanguageToCode[lowerCaseLanguage] || LanguageToCodeTriletter[lowerCaseLanguage] || '';
33
33
  }
34
34
  export const _getLanguageCode = languages => {
package/index.js CHANGED
@@ -6,7 +6,8 @@
6
6
  import _getUserLanguage from "./client/getUserLanguage.js";
7
7
  import { _getLanguageCode, _getLanguageName } from "./codes/codes.js"
8
8
  import { _getModelInfo, _getModelList, _getModelLanguages, _isLanguageSupported, _getModelsByDeveloper, _getModelsByLanguage } from "./models/models.js"
9
- import _translatePrompt from "./prompts/translate.js"
9
+ import _translatePrompt from "./translate/prompt.js"
10
+ import _createI18N from "./translate/site.js";
10
11
 
11
12
  // ----- CORE CLASS ----- //
12
13
 
@@ -22,29 +23,22 @@ export default class GT {
22
23
  this.baseURL = baseURL;
23
24
  }
24
25
 
25
- // Language code functions
26
- getLanguageName = _getLanguageName; // e.g. 'en' => 'English'
27
- getLanguageCode = _getLanguageCode; // e.g. 'English' => 'en'
28
-
29
- // Model information functions
30
- getModelList = _getModelList; // returns array of supported model names
31
- getModelInfo = _getModelInfo; // returns model object
32
- getModelLanguages = _getModelLanguages; // e.g. 'mistral-7b' => ['en']
33
- isLanguageSupported = _isLanguageSupported; // e.g. ('mistral-7b', 'en') => true
34
- getModelsByLanguage = _getModelsByLanguage; // returns array of model names
35
- getModelsByDeveloper = _getModelsByDeveloper; // returns array of model names
36
-
37
- // Get a user's browser language
38
- getUserLanguage = () => {
39
- return _getUserLanguage({
40
- defaultLanguage: this.defaultLanguage
41
- });
42
- }
43
-
44
26
  // Prompt I18N
45
27
  translatePrompt = async (prompt, language) => {
46
28
  return await _translatePrompt({
47
- content: prompt, language: language, context: this
29
+ content: prompt, language: language, config: this
30
+ });
31
+ }
32
+
33
+ // Site I18N
34
+ createI18N = async ({ projectID, html, strings, defaultLanguage, userLanguage }) => {
35
+ return await _createI18N({
36
+ projectID: projectID,
37
+ html: html,
38
+ strings: strings,
39
+ defaultLanguage: defaultLanguage,
40
+ userLanguage: userLanguage,
41
+ config: this
48
42
  });
49
43
  }
50
44
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generaltranslation",
3
- "version": "1.1.25",
3
+ "version": "1.1.27",
4
4
  "description": "A language toolkit for AI developers",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -65,15 +65,15 @@ const _constructPrompt = ({ content, untranslated = null}) => {
65
65
  // Get a translation via General Translation API
66
66
  // Returns string
67
67
  const _translatePrompt = async ({
68
- content, language, context
68
+ content, language, config
69
69
  }) => {
70
70
 
71
- const apiKey = context?.apiKey;
71
+ const apiKey = config?.apiKey;
72
72
  if (!apiKey) {
73
73
  throw new Error('Missing API Key!')
74
74
  };
75
75
 
76
- const defaultLanguage = context?.defaultLanguage;
76
+ const defaultLanguage = config?.defaultLanguage;
77
77
  if (language === defaultLanguage) {
78
78
  return _constructPrompt({ content: content });
79
79
  };
@@ -81,7 +81,7 @@ const _translatePrompt = async ({
81
81
  const { processed, untranslated } = _processPrompt({ content });
82
82
 
83
83
  try {
84
- const response = await fetch(`${context?.baseURL}/prompt`, {
84
+ const response = await fetch(`${config?.baseURL}/prompt`, {
85
85
  method: 'POST',
86
86
  headers: {
87
87
  'Content-Type': 'application/json',
@@ -0,0 +1,47 @@
1
+ const _createI18N = async ({
2
+ projectID,
3
+ defaultLanguage,
4
+ userLanguage,
5
+ content,
6
+ metadata,
7
+ config
8
+ }) => {
9
+
10
+ const apiKey = config?.apiKey;
11
+ if (!apiKey) {
12
+ throw new Error('Missing API Key!')
13
+ };
14
+
15
+ if (!projectID) {
16
+ throw new Error('Missing project ID!')
17
+ };
18
+
19
+ try {
20
+ const response = await fetch(`${config?.baseURL}/site`, {
21
+ method: 'POST',
22
+ headers: {
23
+ 'Content-Type': 'application/json',
24
+ 'gtx-api-key': apiKey
25
+ },
26
+ body: JSON.stringify({
27
+ projectID: projectID,
28
+ content: content,
29
+ defaultLanguage: defaultLanguage,
30
+ userLanguage: userLanguage,
31
+ metadata: metadata
32
+ })
33
+ })
34
+ if (!response.ok) {
35
+ const result = await response.text();
36
+ throw new Error(`${result || response.status}`);
37
+ } else {
38
+ const result = await response.json();
39
+ return result;
40
+ }
41
+ } catch (error) {
42
+ console.error(error)
43
+ return content;
44
+ }
45
+
46
+ }
47
+ export default _createI18N;