Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 3x 3x 3x 3x 24x 24x 14x 2x 12x 3x 17x 17x 17x 17x 17x 17x 7x 1x 8x | import assertInputTypes from './assertInputTypes';
import splitLongText from './splitLongText';
import url from 'url';
interface Option {
lang?: string;
slow?: boolean;
host?: string;
}
/**
* Generate "Google TTS" audio URL
*
* @param {string} text length should be less than 200 characters
* @param {object?} option
* @param {string?} option.lang default is "en"
* @param {boolean?} option.slow default is false
* @param {string?} option.host default is "https://translate.google.com"
* @return {string} url
*/
export const getAudioUrl = (
text: string,
{ lang = 'en', slow = false, host = 'https://translate.google.com' }: Option = {}
): string => {
assertInputTypes(text, lang, slow, host);
if (text.length > 200) {
throw new RangeError(
`text length (${text.length}) should be less than 200 characters. Try "getAllAudioUrls(text, [option])" for long text.`
);
}
return (
host +
'/translate_tts' +
url.format({
query: {
ie: 'UTF-8',
q: text,
tl: lang,
total: 1,
idx: 0,
textlen: text.length,
client: 'tw-ob',
prev: 'input',
ttsspeed: slow ? 0.24 : 1,
},
})
);
};
interface LongTextOption extends Option {
splitPunct?: string;
}
/**
* @typedef {object} Result
* @property {string} shortText
* @property {string} url
*/
/**
* Split the long text into multiple short text and generate audio URL list
*
* @param {string} text
* @param {object?} option
* @param {string?} option.lang default is "en"
* @param {boolean?} option.slow default is false
* @param {string?} option.host default is "https://translate.google.com"
* @param {string?} option.splitPunct split punctuation
* @return {Result[]} the list with short text and audio url
*/
export const getAllAudioUrls = (
text: string,
{
lang = 'en',
slow = false,
host = 'https://translate.google.com',
splitPunct = '',
}: LongTextOption = {}
): { shortText: string; url: string }[] => {
assertInputTypes(text, lang, slow, host);
if (typeof splitPunct !== 'string') {
throw new TypeError('splitPunct should be a string');
}
return splitLongText(text, { splitPunct }).map((shortText) => ({
shortText,
url: getAudioUrl(shortText, { lang, slow, host }),
}));
};
|