generaltranslation 2.0.11 → 2.0.13

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
@@ -2,7 +2,7 @@
2
2
 
3
3
  <a href='https://www.generaltranslation.com' target="_blank">generaltranslation.com</a>
4
4
 
5
- A language toolkit for AI developers. Used in `@generaltranslation/react`.
5
+ A language toolkit for AI developers. Used in `gt-react`.
6
6
 
7
7
  Full documentation coming soon!
8
8
 
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Check if a given language-country-script code is valid.
3
+ * @param {string} code - The language-country-script code to validate.
4
+ * @returns {boolean} - Returns true if valid, false otherwise.
5
+ */
6
+ declare function _isValidLanguageCode(code: string): boolean;
1
7
  type LanguageObject = {
2
8
  language: string;
3
9
  script?: string;
@@ -31,4 +37,4 @@ declare const _getLanguageCode: (languages: string | string[]) => string | strin
31
37
  */
32
38
  declare function _isSameLanguage(...codes: string[]): boolean;
33
39
  declare function _isSameLanguage(codes: string[]): boolean;
34
- export { _getLanguageObject, _getLanguageName, _getLanguageCode, _isSameLanguage };
40
+ export { _isValidLanguageCode, _getLanguageObject, _getLanguageName, _getLanguageCode, _isSameLanguage };
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  };
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports._getLanguageCode = exports._getLanguageName = void 0;
8
+ exports._isValidLanguageCode = _isValidLanguageCode;
8
9
  exports._getLanguageObject = _getLanguageObject;
9
10
  exports._isSameLanguage = _isSameLanguage;
10
11
  // Import modules for mapping ISO 639 codes to language names and vice versa
@@ -30,6 +31,23 @@ const CodeToRegion = CodeToRegion_json_1.default;
30
31
  // Import predefined common regions
31
32
  const Predefined_json_1 = __importDefault(require("./predefined/Predefined.json"));
32
33
  const Predefined = Predefined_json_1.default;
34
+ // ----- VALIDITY CHECKS ----- //
35
+ /**
36
+ * Check if a given language-country-script code is valid.
37
+ * @param {string} code - The language-country-script code to validate.
38
+ * @returns {boolean} - Returns true if valid, false otherwise.
39
+ */
40
+ function _isValidLanguageCode(code) {
41
+ if (!code)
42
+ return false;
43
+ try {
44
+ const locale = new Intl.Locale(code);
45
+ return locale.baseName === code; // The baseName includes only the canonical language, script, and region
46
+ }
47
+ catch (error) {
48
+ return false;
49
+ }
50
+ }
33
51
  // ----- FORMATTING HELPER FUNCTIONS ----- //
34
52
  /**
35
53
  * Capitalizes the first letter of a code and converts the rest to lowercase.
@@ -112,41 +130,19 @@ function _getLanguageObject(codes) {
112
130
  * @returns {LanguageObject|null} The language object.
113
131
  */
114
132
  const _handleGetLanguageObject = (code) => {
115
- if (!code)
116
- return null;
117
- let languageObject = {
118
- language: '',
119
- script: '',
120
- region: ''
121
- };
122
- const subtags = code.split('-');
123
- // Look for language
124
- const languageCode = subtags[0];
125
- languageObject.language = _mapCodeToLanguage(languageCode);
126
- // Look for script and region
127
- if (subtags.length === 3) { // language-script-region
128
- languageObject.script = _mapCodeToScript(subtags[1]);
129
- languageObject.region = _mapCodeToRegion(subtags[2]);
133
+ try {
134
+ const locale = new Intl.Locale(code);
135
+ let languageObject = {
136
+ language: _mapCodeToLanguage(locale.language) || '',
137
+ script: locale.script ? _mapCodeToScript(locale.script) : '' || '',
138
+ region: locale.region ? _mapCodeToRegion(locale.region) : '' || ''
139
+ };
140
+ return languageObject.language ? languageObject : null;
130
141
  }
131
- else if (subtags.length === 2) { // either language-script or language-region
132
- if (_isScriptCode(subtags[1])) {
133
- languageObject.script = _mapCodeToScript(subtags[1]);
134
- }
135
- else {
136
- languageObject.region = _mapCodeToRegion(subtags[1]);
137
- }
142
+ catch (error) {
143
+ // If the code is not a valid locale, return null
144
+ return null;
138
145
  }
139
- return languageObject.language ? languageObject : null;
140
- };
141
- /**
142
- * Helper function to determine if a code is a script code.
143
- * @param {string} code - The code to check.
144
- * @returns {boolean} True if the code is a script code, false otherwise.
145
- */
146
- const _isScriptCode = (code) => {
147
- if (code.length !== 4)
148
- return false;
149
- return true;
150
146
  };
151
147
  // ----- LANGUAGE NAMES FROM CODES ----- //
152
148
  /**
@@ -164,7 +160,7 @@ exports._getLanguageName = _getLanguageName;
164
160
  * @returns {string} The language name.
165
161
  */
166
162
  const _handleGetLanguageName = (code) => {
167
- if (!code)
163
+ if (!_isValidLanguageCode(code))
168
164
  return '';
169
165
  if (Predefined[code])
170
166
  return Predefined[code];
@@ -245,7 +241,7 @@ const _handleGetLanguageCodeFromObject = (languageObject) => {
245
241
  if (languageObject.region) {
246
242
  code += `-${languageObject.region.toUpperCase()}`;
247
243
  }
248
- return code;
244
+ return _isValidLanguageCode(code) ? code : '';
249
245
  };
250
246
  function _isSameLanguage(...codes) {
251
247
  // Flatten the array in case the codes are provided as an array
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { _getLanguageObject, _isSameLanguage } from './codes/codes';
1
+ import { _isValidLanguageCode, _getLanguageObject, _isSameLanguage } from './codes/codes';
2
2
  /**
3
3
  * Type representing the constructor parameters for the GT class.
4
4
  */
@@ -80,6 +80,7 @@ declare class GT {
80
80
  bundleRequests(requests: any[]): Promise<Array<any | null>>;
81
81
  }
82
82
  export default GT;
83
+ export declare const isValidLanguageCode: typeof _isValidLanguageCode;
83
84
  export declare const getLanguageObject: typeof _getLanguageObject;
84
85
  export declare const getLanguageCode: (languages: string | string[]) => string | string[];
85
86
  export declare const getLanguageName: (codes: string | string[]) => string | string[];
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ 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 = void 0;
17
+ exports.isSameLanguage = exports.getLanguageName = exports.getLanguageCode = exports.getLanguageObject = exports.isValidLanguageCode = void 0;
18
18
  // ----- IMPORTS ----- //
19
19
  const codes_1 = require("./codes/codes");
20
20
  const _bundleRequests_1 = __importDefault(require("./translation/_bundleRequests"));
@@ -105,6 +105,7 @@ class GT {
105
105
  // Export the class
106
106
  exports.default = GT;
107
107
  // Export the functions
108
+ exports.isValidLanguageCode = codes_1._isValidLanguageCode;
108
109
  exports.getLanguageObject = codes_1._getLanguageObject;
109
110
  exports.getLanguageCode = codes_1._getLanguageCode;
110
111
  exports.getLanguageName = codes_1._getLanguageName;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generaltranslation",
3
- "version": "2.0.11",
3
+ "version": "2.0.13",
4
4
  "description": "A language toolkit for AI developers",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -12,10 +12,12 @@
12
12
  "url": "git+https://github.com/General-Translation/generaltranslation.git"
13
13
  },
14
14
  "keywords": [
15
- "LLM",
16
- "prompting",
17
15
  "language",
18
16
  "translation",
17
+ "internationalization",
18
+ "localization",
19
+ "translate",
20
+ "i18n",
19
21
  "toolkit"
20
22
  ],
21
23
  "author": "General Translation, Inc.",