UNPKG

illyria-scraper

Version:

Google Translate scraper for Illyria Translate

129 lines 4.45 kB
import * as languagesData from './languages.json' with { type: 'json' }; const { languages, exceptions, mappings } = languagesData.default; export const LanguageType = { SOURCE: 'source', TARGET: 'target' }; const isKeyOf = (obj) => (key) => key in obj; /** * Verifies if a string is a valid language code in our system * * @param code - Code to validate (e.g. "en", "zh", "auto") * @param langType - Optional language type to check against specific exceptions * @returns Type guard confirming code is valid * * @example * ```typescript * // Check if valid for any context * if (isValidCode("xyz")) { * // Not valid, won't enter this block * } * * // Check if valid as a source language * if (isValidCode("auto", LanguageType.SOURCE)) { * // Valid as source, will enter this block * } * ``` */ export const isValidCode = (code, langType) => !!code && isKeyOf(languageList[langType ?? 'all'])(code); /** * Replaces language codes with their exceptions based on language type * * Handles special cases like: * - Converting Traditional Chinese (zh_HANT) to standard Chinese (zh) for source languages * - Converting "auto" to "en" for target languages * * @param langType - Source or target language type * @param langCode - Original language code * @returns Potentially replaced language code appropriate for the context * * @example * ```typescript * // Traditional Chinese gets replaced with standard Chinese for source * replaceExceptedCode(LanguageType.SOURCE, "zh_HANT") // returns "zh" * * // "auto" gets replaced with "en" for target * replaceExceptedCode(LanguageType.TARGET, "auto") // returns "en" * ``` * * @see {@link exceptions} for the full mapping of exceptions */ export const replaceExceptedCode = (langType, langCode) => { const langExceptions = exceptions[langType]; const finalCode = isKeyOf(langExceptions)(langCode) ? langExceptions[langCode] : langCode; return finalCode; }; /** * Maps internal language codes to Google Translate API codes * * Google Translate API uses slightly different codes for some languages. * This function converts our internal codes to match Google's expectations. * * @param langCode - Internal language code * @returns Google-compatible language code * * @example * ```typescript * mapGoogleCode("zh") // returns "zh-CN" * mapGoogleCode("zh_HANT") // returns "zh-TW" * mapGoogleCode("en") // returns "en" (unchanged) * ``` * * @see {@link mappings.request} for the full mapping table */ export const mapGoogleCode = (langCode) => { const reqMappings = mappings['request']; const finalCode = isKeyOf(reqMappings)(langCode) ? reqMappings[langCode] : langCode; return finalCode; }; /** * Maps Google Translate API response codes back to internal language codes * * The reverse operation of {@link mapGoogleCode} - converts Google's specific * codes back to our internal representation for consistent handling. * * @param langCode - Google API language code * @returns Internal language code * * @example * ```typescript * mapLingvaCode("zh-CN") // returns "zh" * mapLingvaCode("zh-TW") // returns "zh" * mapLingvaCode("en") // returns "en" (unchanged) * ``` * * @see {@link mappings.response} for the full mapping table */ export const mapLingvaCode = (langCode) => { const resMappings = mappings['response']; const finalCode = isKeyOf(resMappings)(langCode) ? resMappings[langCode] : langCode; return finalCode; }; /** * Creates a filtered copy of the languages object by removing exception codes for a specific language type * * This is used to create separate language lists for source and target languages, * each excluding their respective exception codes. * * @param type - Language type (source or target) to filter exceptions for * @returns A copy of the languages object with exception codes removed * * @see {@link exceptions} for the list of exception codes by language type */ const filteredLanguages = (type) => { const entries = Object.entries(languages); const filteredEntries = entries.filter(([code]) => !Object.keys(exceptions[type]).includes(code)); return Object.fromEntries(filteredEntries); }; export const languageList = { all: languages, source: filteredLanguages(LanguageType.SOURCE), target: filteredLanguages(LanguageType.TARGET) }; //# sourceMappingURL=language.js.map