Skip to content

Commit

Permalink
Merge pull request google#108 from google/i18n
Browse files Browse the repository at this point in the history
Move i18n to aiy.i18n and always use that.
  • Loading branch information
enetor authored Jul 20, 2017
2 parents bce53b9 + bdf5226 commit 15971cc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/aiy/_apis/_speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import grpc
from six.moves import queue

import i18n
import aiy.i18n

logger = logging.getLogger('speech')

Expand Down Expand Up @@ -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.")
Expand Down
4 changes: 2 additions & 2 deletions src/aiy/_drivers/_tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)


Expand Down
26 changes: 19 additions & 7 deletions src/i18n.py → src/aiy/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@
import gettext
import os

DEFAULT_LANGUAGE_CODE = 'en-US'
_DEFAULT_LANGUAGE_CODE = 'en-US'
_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
_locale_dir = None


def set_locale_dir(locale_dir):
"""Sets the directory that contains the language bundles.
This is only required if you call set_language_code with gettext_install=True.
"""
global _locale_dir
if not locale_dir:
raise ValueError('locale_dir must be valid')
_locale_dir = locale_dir


def set_language_code(code, gettext_install=False):
Expand All @@ -33,12 +43,14 @@ 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
_language_code = code.replace('_', '-')

if gettext_install:
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)
t = gettext.translation(_LOCALE_DOMAIN, _locale_dir, [language_id], fallback=True)
t.install()


Expand Down
9 changes: 7 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import configargparse

import aiy.audio
import aiy.i18n
import auth_helpers
import action
import i18n
import speech

# =============================================================================
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -149,7 +153,8 @@ def main():
args = parser.parse_args()

create_pid_file(args.pid_file)
i18n.set_language_code(args.language, gettext_install=True)
aiy.i18n.set_locale_dir(LOCALE_DIR)
aiy.i18n.set_language_code(args.language, gettext_install=True)

player = aiy.audio.get_player()

Expand Down
4 changes: 2 additions & 2 deletions src/speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import grpc
from six.moves import queue

import i18n
import aiy.i18n

logger = logging.getLogger('speech')

Expand Down Expand Up @@ -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.")
Expand Down

0 comments on commit 15971cc

Please sign in to comment.