-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
171 lines (153 loc) · 6.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const languages = require("./languages");
// import languages from "./languages";
/**
* Validates the provided language code.
* @param {string} langcode - The language code to validate.
* @returns {string} - Validation message.
* @throws {Error} - If the langcode is invalid
*/
exports.isValid = function (langcode) {
if (!langcode || typeof langcode !== "string") return null;
const language = languages.find(
(lang) => lang.languageCodes === langcode.toLowerCase()
);
if (language) {
return `${language.name} (${langcode}) is a valid language code.`;
}
return `${langcode} is not a valid language code.`;
};
/**
* Retrieves the language name based on the language code.
* Searches across languageCodes, locale, and nested codes in a case-insensitive manner.
* If no exact match is found, it will attempt to match the base language (e.g., 'es' for 'es-419').
* @param {string} langcode - The language code to look up.
* @returns {string | null} - The name of the language or null if not found.
* @throws {Error} - If the langcode is invalid.
*/
exports.getLanguageName = function (langcode) {
if (!langcode || typeof langcode !== "string") {
throw new Error("Invalid language code provided.");
}
const lowerLangCode = langcode.toLowerCase();
// Function to check if a language object has a matching code
const matchCode = (codes) => {
if (typeof codes === "string") {
return codes.toLowerCase() === lowerLangCode;
} else if (Array.isArray(codes)) {
return codes.some((code) => code.toLowerCase() === lowerLangCode);
}
return false;
};
// Find the exact match first
let language = languages.find(
(lang) =>
matchCode(lang.languageCodes) ||
(lang.locale && matchCode(lang.locale)) ||
(lang.awsTranscribe?.code && matchCode(lang.awsTranscribe.code)) ||
(lang.googleStt?.code && matchCode(lang.googleStt.code)) ||
(lang.googleTTS?.code && matchCode(lang.googleTTS.code)) ||
(lang.awsPolly?.code && matchCode(lang.awsPolly.code)) ||
(Array.isArray(lang.googleTTS?.voice) &&
lang.googleTTS.voice.some((voice) => matchCode(voice.languageCodes)))
);
// If no exact match, attempt a fallback to the base language (first two characters)
if (!language && lowerLangCode.length > 2) {
const baseLangCode = lowerLangCode.slice(0, 2);
language = languages.find(
(lang) =>
matchCode(lang.languageCodes) ||
(lang.locale && matchCode(lang.locale)) ||
(lang.awsTranscribe?.code &&
matchCode(lang.awsTranscribe.code.slice(0, 2))) ||
(lang.googleStt?.code && matchCode(lang.googleStt.code.slice(0, 2))) ||
(lang.googleTTS?.code && matchCode(lang.googleTTS.code.slice(0, 2))) ||
(lang.awsPolly?.code && matchCode(lang.awsPolly.code.slice(0, 2))) ||
(Array.isArray(lang.googleTTS?.voice) &&
lang.googleTTS.voice.some((voice) =>
voice.languageCodes.some((code) =>
code.toLowerCase().startsWith(baseLangCode)
)
))
);
}
return language ? language.name : null;
};
/**
* Retrieves the writing style of a specified language, either LTR (Left-to-Right) or RTL (Right-to-Left).
*
* @param {string} langcode - The language code to validate and retrieve the writing style.
* @returns {string|null} - Returns the writing style of the language if found; otherwise, returns null.
* @throws {Error} - Throws an error if the provided langcode is invalid (null or not a string).
*/
exports.getLanguageWritingStyle = function (langcode) {
if (!langcode || typeof langcode !== "string") {
return null;
}
const language = languages?.find(
(lang) => lang.languageCodes === langcode.toLowerCase()
);
return language ? language.writingStyle : null;
};
/**
* Checks the translation availability for a given language code.
* @param {string} code - The language code to check.
* @returns {Array | null} - An array of available translations or null if none.
*/
exports.checkTranslationAvailability = function (code) {
const matchedLanguages = languages.filter(
(lang) => lang.languageCodes === code
);
if (matchedLanguages.length > 0) {
return matchedLanguages.map((language) => ({
name: language.name,
locale: language.locale,
googleTranslate: language.googleTranslate || false,
microsoftTranslate: language.microsoftTranslate || false,
deeplTranslate: language.deeplTranslate || false,
awsTranslate: language.awsTranslate || false,
ibmTranslate: language.ibmTranslate || false,
awsTranscribe: !!language.awsTranscribe
? language.awsTranscribe.code
: false,
googleStt: !!language.googleStt ? language.googleStt.code : false,
googleTTS: !!language.googleTTS ? language.googleTTS.code : false,
awsPolly: !!language.awsPolly ? language.awsPolly.code : false,
}));
}
return null;
};
/**
* Checks the translation availability for a given language code by passing locale.
* @param {string} locale - The language locale to check.
* @returns {Array | null} - An array of available translations or null if none.
*/
exports.checkTranslationAvailabilityUsingLocale = function (locale) {
const matchedLanguages = languages.filter(
(lang) => lang.locale.toLowerCase() === locale.toLocaleLowerCase()
);
if (matchedLanguages.length > 0) {
return matchedLanguages.map((language) => ({
name: language.name,
locale: language.locale,
googleTranslate: language.googleTranslate || false,
microsoftTranslate: language.microsoftTranslate || false,
deeplTranslate: language.deeplTranslate || false,
awsTranslate: language.awsTranslate || false,
ibmTranslate: language.ibmTranslate || false,
awsTranscribe: !!language.awsTranscribe
? language.awsTranscribe.code
: false,
googleStt: !!language.googleStt ? language.googleStt.code : false,
googleTTS: !!language.googleTTS ? language.googleTTS.code : false,
awsPolly: !!language.awsPolly ? language.awsPolly.code : false,
}));
}
return null;
};
exports.getAllLanguages = function () {
return languages.map(({ name, languageCodes, locale }) => ({
name,
languageCode: languageCodes,
locale,
}));
};