/**
* Language Tools class
* @constructor
* @param {Object} langDB - The language database
*/
var LangTools = function(langDB) {
this.langDB = langDB;
}
/**
* Get language by code
* @param {string} code - The language code
* @return {Object} The language object
*/
LangTools.prototype.getLanguage = function(code) {
return this.lookupAlias(this.langDB[code]);
}
/**
* Get a list of languages, excluding those in the blacklist
* @param {Array} blacklist - The list of language codes to exclude
* @return {Object} The list of languages
*/
LangTools.prototype.getLanguageList = function(blacklist=[]) {
const langList = {}
blacklist ||= []
for (let code in this.langDB) {
if (blacklist.includes(code)) continue;
langList[code] = this.langDB[code].name
}
return langList
}
/**
* Lookup alias in the language database
* @param {Object} langObj - The language object
* @return {Object} The language object
*/
LangTools.prototype.lookupAlias = function(langObj) {
if (!langObj) return langObj;
if (!langObj.alias) return langObj
if (!this.langDB[langObj.alias]) return langObj
if (this.langDB[langObj.alias]) return this.langDB[langObj.alias];
}
/**
* Lookup in the language database by key or name
* @param {string} key - The key to lookup
* @param {string} name - The name to lookup
* @return {Object} The language object
*/
LangTools.prototype.lookupDB = function(key="", name="") {
if (!key) return;
key = key.toLowerCase();
if (this.langDB[key]) {
return this.lookupAlias(this.langDB[key]);
}
for (var i in this.langDB) {
if (key == this.langDB[i].idt) this.lookupAlias(this.langDB[i]);
if (key == this.langDB[i].idb) this.lookupAlias(this.langDB[i]);
if (typeof this.langDB[i].name !== "string") continue;
if (this.langDB[i].name.toLowerCase() == name.toLowerCase()) return this.lookupAlias(this.langDB[i]);
}
}
/**
* Get the full name of the language
* @param {Object} langObj - The language object
* @return {string} The full name of the language
*/
LangTools.prototype.getFullName = function(langObj) {
if (langObj.displayName && langObj.name!=langObj.displayName) {
return `${langObj.name}(${langObj.displayName})`;
}
return langObj.name;
}
/**
* Compare language pairs with the language database
* @param {Object} langPairs - The language pairs to compare
* @return {Object} The result of the comparison
*/
LangTools.prototype.compareWithDB = function(langPairs) {
var match = {}
var unmatch = {}
for (var code in langPairs) {
var thisLang = this.lookupDB(code, langPairs[code])
if (thisLang) {
match[code] = thisLang;
} else {
unmatch[code.toLowerCase()] = {
"name": langPairs[code],
"displayName": "",
"displayNameAlt": "",
"description": langPairs[code],
"descriptionAlt": "",
"groupTag": "",
"id": code.toLowerCase(),
"groupName": ""
}
}
}
return {
match:match,
unmatch:unmatch
}
}
/**
* Generate language pairs
* @param {Object} langPairs - The language pairs to generate
* @return {Object} The result of the generation
*/
LangTools.prototype.generateLangPairs = function(langPairs) {
var match = {}
var unmatch = {}
for (var code in langPairs) {
var thisLang = this.lookupDB(code, langPairs[code])
if (thisLang) {
match[thisLang.id] = code;
} else {
unmatch[code.toLowerCase()] = {
"name": langPairs[code],
"displayName": "",
"displayNameAlt": "",
"description": langPairs[code],
"descriptionAlt": "",
"groupTag": "",
"id": code.toLowerCase(),
"groupName": ""
}
}
}
return {
match:match,
unmatch:unmatch
}
}
/**
* Check if a language is CJK (Chinese, Japanese, Korean)
* @param {string} langCode - The language code
* @return {boolean} True if the language is CJK, false otherwise
*/
LangTools.prototype.isCJK = function(langCode) {
if (!langCode) return false;
langCode = langCode.toLowerCase();
let majorLang = (langCode.split("-"))[0];
if (["ja", "ko", "zh"].includes(majorLang)) return true;
return false;
}
module.exports = LangTools;