Skip to content

Commit

Permalink
add known working language codes as exports
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Pierson committed Dec 10, 2020
1 parent 00c0d6b commit aec0ae3
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 149 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (United States)' }

const english = googleTTS.languages.findByName('English (United States)');
console.log(english) // { code: 'en-US', name: 'English (United States)' }

// use code to make request
const url = googleTTS.getAudioUrl('Hello, world!', { lang: english.code });
```

## Options (All options are optional)

| Option | Type | Default | Description |
Expand Down
4 changes: 2 additions & 2 deletions src/assertInputTypes.ts
Original file line number Diff line number Diff line change
@@ -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');
}
Expand Down
4 changes: 2 additions & 2 deletions src/getAudioBase64.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/getAudioUrl.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
13 changes: 10 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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,
};
90 changes: 90 additions & 0 deletions src/languages.ts
Original file line number Diff line number Diff line change
@@ -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,
};
140 changes: 0 additions & 140 deletions src/types.ts

This file was deleted.

26 changes: 26 additions & 0 deletions test/languages.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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);

test("lookup by code", () => {
const english = googleTTS.languages.findByCode("en-US");
expect(english.name).toBe("English (United States)");
});

test("lookup by name", () => {
const english = googleTTS.languages.findByName("English (United States)");
expect(english.code).toBe("en-US");
});

0 comments on commit aec0ae3

Please sign in to comment.