generaltranslation 2.0.12 → 2.0.14

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.
@@ -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,25 @@ 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
+ const { language, script, region } = locale;
46
+ const constructedCode = `${language}${script ? '-' + script : ''}${region ? '-' + region : ''}`;
47
+ return constructedCode === code;
48
+ }
49
+ catch (error) {
50
+ return false;
51
+ }
52
+ }
33
53
  // ----- FORMATTING HELPER FUNCTIONS ----- //
34
54
  /**
35
55
  * Capitalizes the first letter of a code and converts the rest to lowercase.
@@ -112,41 +132,19 @@ function _getLanguageObject(codes) {
112
132
  * @returns {LanguageObject|null} The language object.
113
133
  */
114
134
  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]);
135
+ try {
136
+ const locale = new Intl.Locale(code);
137
+ let languageObject = {
138
+ language: _mapCodeToLanguage(locale.language) || '',
139
+ script: locale.script ? _mapCodeToScript(locale.script) : '' || '',
140
+ region: locale.region ? _mapCodeToRegion(locale.region) : '' || ''
141
+ };
142
+ return languageObject.language ? languageObject : null;
130
143
  }
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
- }
144
+ catch (error) {
145
+ // If the code is not a valid locale, return null
146
+ return null;
138
147
  }
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
148
  };
151
149
  // ----- LANGUAGE NAMES FROM CODES ----- //
152
150
  /**
@@ -164,7 +162,7 @@ exports._getLanguageName = _getLanguageName;
164
162
  * @returns {string} The language name.
165
163
  */
166
164
  const _handleGetLanguageName = (code) => {
167
- if (!code)
165
+ if (!_isValidLanguageCode(code))
168
166
  return '';
169
167
  if (Predefined[code])
170
168
  return Predefined[code];
@@ -245,7 +243,7 @@ const _handleGetLanguageCodeFromObject = (languageObject) => {
245
243
  if (languageObject.region) {
246
244
  code += `-${languageObject.region.toUpperCase()}`;
247
245
  }
248
- return code;
246
+ return _isValidLanguageCode(code) ? code : '';
249
247
  };
250
248
  function _isSameLanguage(...codes) {
251
249
  // 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.12",
3
+ "version": "2.0.14",
4
4
  "description": "A language toolkit for AI developers",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {