From d16c86d2788221aa6ba374073fdd4a31f2b46199 Mon Sep 17 00:00:00 2001 From: Fan Deng Date: Wed, 19 Jul 2017 12:25:10 -0700 Subject: [PATCH 1/3] Move i18n to aiy.i18n and always use that. Tested: Ran src/main.py, src/main.py --cloud-speech, src/cloudspeech_demo.py, src/assistant_grpc_demo.py --- src/aiy/_apis/_speech.py | 4 ++-- src/aiy/_drivers/_tts.py | 4 ++-- src/{ => aiy}/i18n.py | 12 ++++++------ src/main.py | 4 ++-- src/speech.py | 4 ++-- src/tts.py | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) rename src/{ => aiy}/i18n.py (86%) diff --git a/src/aiy/_apis/_speech.py b/src/aiy/_apis/_speech.py index 49bb6b9b..329cba69 100644 --- a/src/aiy/_apis/_speech.py +++ b/src/aiy/_apis/_speech.py @@ -31,7 +31,7 @@ import grpc from six.moves import queue -import i18n +import aiy.i18n logger = logging.getLogger('speech') @@ -284,7 +284,7 @@ def __init__(self, credentials_file): super().__init__('speech.googleapis.com', credentials) - self.language_code = i18n.get_language_code() + self.language_code = aiy.i18n.get_language_code() if not hasattr(cloud_speech, 'StreamingRecognizeRequest'): raise ValueError("cloud_speech_pb2.py doesn't have StreamingRecognizeRequest.") diff --git a/src/aiy/_drivers/_tts.py b/src/aiy/_drivers/_tts.py index 3aa93bc6..d1fa78a7 100644 --- a/src/aiy/_drivers/_tts.py +++ b/src/aiy/_drivers/_tts.py @@ -23,7 +23,7 @@ import numpy as np from scipy import signal -import i18n +import aiy.i18n # Path to a tmpfs directory to avoid SD card wear TMP_DIR = '/run/user/%d' % os.getuid() @@ -63,7 +63,7 @@ def create_say(player): """Return a function say(words) for the given player, using the default EQ filter. """ - lang = i18n.get_language_code() + lang = aiy.i18n.get_language_code() return functools.partial(say, player, eq_filter=create_eq_filter(), lang=lang) diff --git a/src/i18n.py b/src/aiy/i18n.py similarity index 86% rename from src/i18n.py rename to src/aiy/i18n.py index 9b321a16..973d9e02 100644 --- a/src/i18n.py +++ b/src/aiy/i18n.py @@ -17,13 +17,13 @@ import gettext import os -DEFAULT_LANGUAGE_CODE = 'en-US' +_DEFAULT_LANGUAGE_CODE = 'en-US' -LOCALE_DIR = os.path.realpath( - os.path.join(os.path.abspath(os.path.dirname(__file__)), '../po')) -LOCALE_DOMAIN = 'voice-recognizer' +_LOCALE_DIR = os.path.realpath( + os.path.join(os.path.abspath(os.path.dirname(__file__)), '../../po')) +_LOCALE_DOMAIN = 'voice-recognizer' -_language_code = DEFAULT_LANGUAGE_CODE +_language_code = _DEFAULT_LANGUAGE_CODE def set_language_code(code, gettext_install=False): @@ -38,7 +38,7 @@ def set_language_code(code, gettext_install=False): if gettext_install: language_id = code.replace('-', '_') - t = gettext.translation(LOCALE_DOMAIN, LOCALE_DIR, [language_id], fallback=True) + t = gettext.translation(_LOCALE_DOMAIN, _LOCALE_DIR, [language_id], fallback=True) t.install() diff --git a/src/main.py b/src/main.py index 454227fe..a53b6cd4 100755 --- a/src/main.py +++ b/src/main.py @@ -26,9 +26,9 @@ import configargparse import aiy.audio +import aiy.i18n import auth_helpers import action -import i18n import speech import tts @@ -150,7 +150,7 @@ def main(): args = parser.parse_args() create_pid_file(args.pid_file) - i18n.set_language_code(args.language, gettext_install=True) + aiy.i18n.set_language_code(args.language, gettext_install=True) player = aiy.audio.get_player() diff --git a/src/speech.py b/src/speech.py index 40132cfd..feca3a71 100644 --- a/src/speech.py +++ b/src/speech.py @@ -31,7 +31,7 @@ import grpc from six.moves import queue -import i18n +import aiy.i18n logger = logging.getLogger('speech') @@ -280,7 +280,7 @@ def __init__(self, credentials_file): super().__init__('speech.googleapis.com', credentials) - self.language_code = i18n.get_language_code() + self.language_code = aiy.i18n.get_language_code() if not hasattr(cloud_speech, 'StreamingRecognizeRequest'): raise ValueError("cloud_speech_pb2.py doesn't have StreamingRecognizeRequest.") diff --git a/src/tts.py b/src/tts.py index 94d24fa1..32d1918b 100644 --- a/src/tts.py +++ b/src/tts.py @@ -23,7 +23,7 @@ import numpy as np from scipy import signal -import i18n +import aiy.i18n # Path to a tmpfs directory to avoid SD card wear TMP_DIR = '/run/user/%d' % os.getuid() @@ -63,7 +63,7 @@ def create_say(player): """Return a function say(words) for the given player, using the default EQ filter. """ - lang = i18n.get_language_code() + lang = aiy.i18n.get_language_code() return functools.partial(say, player, eq_filter=create_eq_filter(), lang=lang) From 345e089f17ac5ab062a4245aa0a438149e4dc15d Mon Sep 17 00:00:00 2001 From: Fan Deng Date: Thu, 20 Jul 2017 10:18:24 -0700 Subject: [PATCH 2/3] Add a setter to aiy.i18n to set the locale dir. Now that aiy.i18n is an independent library we cannot hard-code the locale dir. Instead, callers are expected to set the locale dir in their application. Tested: Ran src/main.py --- src/aiy/i18n.py | 24 +++++++++++++++++++----- src/main.py | 5 +++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/aiy/i18n.py b/src/aiy/i18n.py index 973d9e02..0307741d 100644 --- a/src/aiy/i18n.py +++ b/src/aiy/i18n.py @@ -18,13 +18,24 @@ import os _DEFAULT_LANGUAGE_CODE = 'en-US' - -_LOCALE_DIR = os.path.realpath( - os.path.join(os.path.abspath(os.path.dirname(__file__)), '../../po')) _LOCALE_DOMAIN = 'voice-recognizer' _language_code = _DEFAULT_LANGUAGE_CODE +_locale_dir = None + + +def set_locale_dir(locale_dir): + """Sets the directory that contains the language bundles. + + This is only needed if you need to call set_language_code with + gettext_install=True. + """ + global _locale_dir + if locale_dir is None: + raise ValueError('locale_dir must be valid') + _locale_dir = locale_dir + def set_language_code(code, gettext_install=False): """Set the BCP-47 language code that the speech systems should use. @@ -33,12 +44,15 @@ def set_language_code(code, gettext_install=False): gettext_install: if True, gettext's _() will be installed in as a builtin. As this has global effect, it should only be done by applications. """ - global _language_code # pylint: disable=global-statement + global _language_code + global _locale_dir _language_code = code.replace('_', '-') if gettext_install: + if _locale_dir is None: + raise ValueError('locale_dir is not set. Please call set_locale_dir().') language_id = code.replace('-', '_') - t = gettext.translation(_LOCALE_DOMAIN, _LOCALE_DIR, [language_id], fallback=True) + t = gettext.translation(_LOCALE_DOMAIN, _locale_dir, [language_id], fallback=True) t.install() diff --git a/src/main.py b/src/main.py index 6a5844f0..34c0327a 100755 --- a/src/main.py +++ b/src/main.py @@ -64,6 +64,10 @@ os.path.join(VR_CACHE_DIR, 'assistant_credentials.json') ) +# Where the locale/language bundles are stored +LOCALE_DIR = os.path.realpath( + os.path.join(os.path.abspath(os.path.dirname(__file__)), '../po')) + def try_to_get_credentials(client_secrets): """Try to get credentials, or print an error and quit on failure.""" @@ -149,6 +153,7 @@ def main(): args = parser.parse_args() create_pid_file(args.pid_file) + aiy.i18n.set_locale_dir(LOCALE_DIR) aiy.i18n.set_language_code(args.language, gettext_install=True) player = aiy.audio.get_player() From bdf522677c7a0dbb6f99bd56e47fc61e957a7a2e Mon Sep 17 00:00:00 2001 From: Fan Deng Date: Thu, 20 Jul 2017 12:07:57 -0700 Subject: [PATCH 3/3] Minor updates to aiy.i18n --- src/aiy/i18n.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/aiy/i18n.py b/src/aiy/i18n.py index 0307741d..79ca9939 100644 --- a/src/aiy/i18n.py +++ b/src/aiy/i18n.py @@ -28,11 +28,10 @@ def set_locale_dir(locale_dir): """Sets the directory that contains the language bundles. - This is only needed if you need to call set_language_code with - gettext_install=True. + This is only required if you call set_language_code with gettext_install=True. """ global _locale_dir - if locale_dir is None: + if not locale_dir: raise ValueError('locale_dir must be valid') _locale_dir = locale_dir @@ -45,11 +44,10 @@ def set_language_code(code, gettext_install=False): As this has global effect, it should only be done by applications. """ global _language_code - global _locale_dir _language_code = code.replace('_', '-') if gettext_install: - if _locale_dir is None: + if not _locale_dir: raise ValueError('locale_dir is not set. Please call set_locale_dir().') language_id = code.replace('-', '_') t = gettext.translation(_LOCALE_DOMAIN, _locale_dir, [language_id], fallback=True)