diff --git a/README.md b/README.md index 6778cee..0cb6f35 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,27 @@ Please see [CHANGELOG](https://github.com/zlargon/google-tts/blob/master/CHANGEL .catch(console.error); ``` +### 5. `languages` + +- `.get()` returns a list of known language codes that work +- `findByCode` or `findByName` to search within list +- Example: + ``` + const googleTTS = require('google-tts-api'); + + const list = googleTTS.languages.get(); + console.log(list) // [{ code: '...', name: '...' }, ...] + + const english = googleTTS.languages.findByCode('en-US'); + console.log(english) // { code: 'en-US', name: 'English (US)' } + + const english = googleTTS.languages.findByName('English (US)'); + console.log(english) // { code: 'en-US', name: 'English (US)' } + + // use code to make request + const url = googleTTS.getAudioUrl('Hello, world!', { lang: english.code }); + ``` + ## Options (All options are optional) | Option | Type | Default | Description | diff --git a/src/assertInputTypes.ts b/src/assertInputTypes.ts index a05aa76..1f9e087 100644 --- a/src/assertInputTypes.ts +++ b/src/assertInputTypes.ts @@ -1,6 +1,6 @@ -import type { Language } from './types'; +import type { LanguageCode } from './languages'; -const assertInputTypes = (text: string, lang: Language, slow: boolean, host: string) => { +const assertInputTypes = (text: string, lang: LanguageCode, slow: boolean, host: string) => { if (typeof text !== 'string' || text.length === 0) { throw new TypeError('text should be a string'); } diff --git a/src/getAudioBase64.ts b/src/getAudioBase64.ts index 06c88b2..5943c08 100644 --- a/src/getAudioBase64.ts +++ b/src/getAudioBase64.ts @@ -1,10 +1,10 @@ -import type { Language } from './types'; +import type { LanguageCode } from './languages'; import assertInputTypes from './assertInputTypes'; import axios from 'axios'; import splitLongText from './splitLongText'; interface Option { - lang?: Language; + lang?: LanguageCode; slow?: boolean; host?: string; timeout?: number; diff --git a/src/getAudioUrl.ts b/src/getAudioUrl.ts index d22e432..89cf0a5 100644 --- a/src/getAudioUrl.ts +++ b/src/getAudioUrl.ts @@ -1,10 +1,10 @@ -import type { Language } from './types'; +import type { LanguageCode } from './languages'; import assertInputTypes from './assertInputTypes'; import splitLongText from './splitLongText'; import url from 'url'; interface Option { - lang?: Language; + lang?: LanguageCode; slow?: boolean; host?: string; } diff --git a/src/index.ts b/src/index.ts index cfc61f0..4dcbbb7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,11 @@ -import { getAudioUrl, getAllAudioUrls } from './getAudioUrl'; -import { getAudioBase64, getAllAudioBase64 } from './getAudioBase64'; +import { getAudioUrl, getAllAudioUrls } from "./getAudioUrl"; +import { getAudioBase64, getAllAudioBase64 } from "./getAudioBase64"; +import languages from "./languages"; -export { getAudioUrl, getAllAudioUrls, getAudioBase64, getAllAudioBase64 }; +export { + getAudioUrl, + getAllAudioUrls, + getAudioBase64, + getAllAudioBase64, + languages, +}; diff --git a/src/languages.ts b/src/languages.ts new file mode 100644 index 0000000..b73eeab --- /dev/null +++ b/src/languages.ts @@ -0,0 +1,90 @@ +export type Language = typeof languages[number]; +export type LanguageCode = typeof languages[number]["code"]; +export type LanguageName = typeof languages[number]["name"]; + +// prettier-ignore +const languages = [ + { code: "af-ZA", name: "Afrikaans" }, + { code: "sq", name: "Albanian" }, + { code: "ar", name: "Arabic" }, + { code: "hy", name: "Armenian" }, + { code: "bn-BD", name: "Bengali (Bangladesh)" }, + { code: "bn-IN", name: "Bengali (India)" }, + { code: "bs", name: "Bosnian" }, + { code: "my", name: "Burmese (Myanmar)" }, + { code: "ca-ES", name: "Catalan" }, + { code: "cmn-Hant-TW", name: "Chinese" }, + { code: "hr-HR", name: "Croatian" }, + { code: "cs-CZ", name: "Czech" }, + { code: "da-DK", name: "Danish" }, + { code: "nl-NL", name: "Dutch" }, + { code: "en-AU", name: "English (Australia)" }, + { code: "en-GB", name: "English (United Kingdom)" }, + { code: "en-US", name: "English (United States)" }, + { code: "eo", name: "Esperanto" }, + { code: "et", name: "Estonian" }, + { code: "fil-PH", name: "Filipino" }, + { code: "fi-FI", name: "Finnish" }, + { code: "fr-FR", name: "French" }, + { code: "fr-CA", name: "French (Canada)" }, + { code: "de-DE", name: "German" }, + { code: "el-GR", name: "Greek" }, + { code: "gu", name: "Gujarati" }, + { code: "hi-IN", name: "Hindi" }, + { code: "hu-HU", name: "Hungarian" }, + { code: "is-IS", name: "Icelandic" }, + { code: "id-ID", name: "Indonesian" }, + { code: "it-IT", name: "Italian" }, + { code: "ja-JP", name: "Japanese (Japan)" }, + { code: "kn", name: "Kannada" }, + { code: "km", name: "Khmer" }, + { code: "ko-KR", name: "Korean" }, + { code: "la", name: "Latin" }, + { code: "lv", name: "Latvian" }, + { code: "mk", name: "Macedonian" }, + { code: "ml", name: "Malayalam" }, + { code: "mr", name: "Marathi" }, + { code: "ne", name: "Nepali" }, + { code: "nb-NO", name: "Norwegian" }, + { code: "pl-PL", name: "Polish" }, + { code: "pt-BR", name: "Portuguese" }, + { code: "ro-RO", name: "Romanian" }, + { code: "ru-RU", name: "Russian" }, + { code: "sr-RS", name: "Serbian" }, + { code: "si", name: "Sinhala" }, + { code: "sk-SK", name: "Slovak" }, + { code: "es-MX", name: "Spanish (Mexico)" }, + { code: "es-ES", name: "Spanish (Spain)" }, + { code: "sw", name: "Swahili" }, + { code: "sv-SE", name: "Swedish" }, + { code: "ta", name: "Tamil" }, + { code: "te", name: "Telugu" }, + { code: "th-TH", name: "Thai" }, + { code: "tr-TR", name: "Turkish" }, + { code: "uk-UA", name: "Ukrainian" }, + { code: "ur", name: "Urdu" }, + { code: "vi-VN", name: "Vietnamese" }, + { code: "cy", name: "Welsh" }, +] as const; + +/** + * find language from language code + * + * @param {string} code + */ +const findByCode = (code: LanguageCode): Language | undefined => + languages.find((l) => l.code === code); + +/** + * find language from language name + * + * @param {string} name + */ +const findByName = (name: LanguageName): Language | undefined => + languages.find((l) => l.name === name); + +export default { + get: () => languages, + findByCode, + findByName, +}; diff --git a/src/types.ts b/src/types.ts deleted file mode 100644 index b92376e..0000000 --- a/src/types.ts +++ /dev/null @@ -1,140 +0,0 @@ -// https://cloud.google.com/speech-to-text/docs/languages -export type Language = - | 'af-ZA' - | 'sq-AL' - | 'am-ET' - | 'ar-DZ' - | 'ar-BH' - | 'ar-EG' - | 'ar-IQ' - | 'ar-IL' - | 'ar-JO' - | 'ar-KW' - | 'ar-LB' - | 'ar-MA' - | 'ar-OM' - | 'ar-QA' - | 'ar-SA' - | 'ar-PS' - | 'ar-TN' - | 'ar-AE' - | 'ar-YE' - | 'hy-AM' - | 'az-AZ' - | 'eu-ES' - | 'bn-BD' - | 'bn-IN' - | 'bs-BA' - | 'bg-BG' - | 'my-MM' - | 'ca-ES' - | 'yue-Hant-HK' - | 'zh' - | 'cmn-Hans-CN' // zh - | 'zh-TW' - | 'cmn-Hant-TW' // zh-TW - | 'hr-HR' - | 'cs-CZ' - | 'da-DK' - | 'nl-BE' - | 'nl-NL' - | 'en-AU' - | 'en-CA' - | 'en-GH' - | 'en-HK' - | 'en-IN' - | 'en-IE' - | 'en-KE' - | 'en-NZ' - | 'en-NG' - | 'en-PK' - | 'en-PH' - | 'en-SG' - | 'en-ZA' - | 'en-TZ' - | 'en-GB' - | 'en-US' - | 'et-EE' - | 'fil-PH' - | 'fi-FI' - | 'fr-BE' - | 'fr-CA' - | 'fr-FR' - | 'fr-CH' - | 'gl-ES' - | 'ka-GE' - | 'de-AT' - | 'de-DE' - | 'de-CH' - | 'el-GR' - | 'gu-IN' - | 'iw-IL' - | 'hi-IN' - | 'hu-HU' - | 'is-IS' - | 'id-ID' - | 'it-IT' - | 'it-CH' - | 'ja-JP' - | 'jv-ID' - | 'kn-IN' - | 'km-KH' - | 'ko-KR' - | 'lo-LA' - | 'lv-LV' - | 'lt-LT' - | 'mk-MK' - | 'ms-MY' - | 'ml-IN' - | 'mr-IN' - | 'mn-MN' - | 'ne-NP' - | 'no-NO' - | 'fa-IR' - | 'pl-PL' - | 'pt-BR' - | 'pt-PT' - | 'pa-Guru-IN' - | 'ro-RO' - | 'ru-RU' - | 'sr-RS' - | 'si-LK' - | 'sk-SK' - | 'sl-SI' - | 'es-AR' - | 'es-BO' - | 'es-CL' - | 'es-CO' - | 'es-CR' - | 'es-DO' - | 'es-EC' - | 'es-SV' - | 'es-GT' - | 'es-HN' - | 'es-MX' - | 'es-NI' - | 'es-PA' - | 'es-PY' - | 'es-PE' - | 'es-PR' - | 'es-ES' - | 'es-US' - | 'es-UY' - | 'es-VE' - | 'su-ID' - | 'sw-KE' - | 'sw-TZ' - | 'sv-SE' - | 'ta-IN' - | 'ta-MY' - | 'ta-SG' - | 'ta-LK' - | 'te-IN' - | 'th-TH' - | 'tr-TR' - | 'uk-UA' - | 'ur-IN' - | 'ur-PK' - | 'uz-UZ' - | 'vi-VN' - | 'zu-ZA'; diff --git a/test/languages.test.js b/test/languages.test.js new file mode 100644 index 0000000..c30b806 --- /dev/null +++ b/test/languages.test.js @@ -0,0 +1,16 @@ +const googleTTS = require("../dist/index"); + +test("All voice codes are valid", async (done) => { + const responses = googleTTS.languages.get().map((lang) => { + return googleTTS + .getAudioBase64("test", { lang: lang.code }) + .catch((err) => { + console.log("language code failed: " + lang.code); + console.log(err); + done.fail("language code failed: " + lang.code); + }); + }); + + await Promise.all(responses); + done(); +}, 10000);