Skip to content

Commit

Permalink
fix(cli): add deprecated language fallback support to CLI
Browse files Browse the repository at this point in the history
fix: add error case for when a custom tld gives a 404

docs(cli): use correct lang code and valid tld
  • Loading branch information
pndurette committed Apr 29, 2023
1 parent 0c13f02 commit 5a860ed
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ Read "c'est la vie" in French to ``cestlavie.mp3``::

$ gtts-cli "c'est la vie" --lang fr --output cestlavie.mp3

Read '你好' to ``你好.mp3`` (in Mandarin, using google.cn)::
Read '你好' to ``你好.mp3`` (in Mandarin, using google.com.hk)::

$ gtts-cli '你好' --tld cn --lang zh-cn --output 你好.mp3
$ gtts-cli '你好' --tld .com.hk --lang zh-CN --output 你好.mp3

Read 'slow' slowly to ``slow.mp3``::

Expand Down
5 changes: 4 additions & 1 deletion gtts/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from gtts import gTTS, gTTSError, __version__
from gtts.lang import tts_langs
from gtts.lang import tts_langs, _fallback_deprecated_lang
import click
import logging
import logging.config
Expand Down Expand Up @@ -46,6 +46,9 @@ def validate_lang(ctx, param, lang):
if ctx.params["nocheck"]:
return lang

# Fallback from deprecated language if needed
lang = _fallback_deprecated_lang(lang)

try:
if lang not in tts_langs():
raise click.UsageError(
Expand Down
4 changes: 3 additions & 1 deletion gtts/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class gTTS:
can produce different localized 'accents' for a given
language. This is also useful when ``google.com`` might be blocked
within a network but a local or different Google host
(e.g. ``google.cn``) is not. Default is ``com``.
(e.g. ``google.com.hk``) is not. Default is ``com``.
lang (string, optional): The language (IETF language tag) to
read the text in. Default is ``en``.
slow (bool, optional): Reads text more slowly. Defaults to ``False``.
Expand Down Expand Up @@ -364,6 +364,8 @@ def infer_msg(self, tts, rsp=None):

if status == 403:
cause = "Bad token or upstream API changes"
elif status == 404 and tts.tld != "com":
cause = "Unsupported tld '{}'".format(tts.tld)
elif status == 200 and not tts.lang_check:
cause = (
"No audio stream in response. Unsupported language '%s'"
Expand Down

0 comments on commit 5a860ed

Please sign in to comment.