Esempio n. 1
0
def language_selection():
    """Ensures a language has been selected before proceeding with call flow, by:
        - If a get param is sent, store it and return.
        - If the context already has a language, return.
        - If a language choice was passed back, pull it out of settings, store it and return.
        - If nothing was sent, prompt the user with available choices.
       This only needs to be set once during the life of a call.
    """

    # Twimlet-style params always override language settings
    if 'language' in g.request_params.keys():
        sel = g.request_params['language']
        try:
            sel = int(sel)
            write_context('language', settings.LANGUAGES[sel - 1][0])
        except ValueError:
            if sel in [lang[0] for lang in settings.LANGUAGES]:
                write_context('language', sel)

    # Internal app use
    if not get_lang():
        errors = []
        r = twiml.Response()

        # Collect and wipe digits if a choice was submitted
        if 'Digits' in g.request_params.keys():
            sel = int(g.request_params.get('Digits', 1))
            try:
                write_context('language', settings.LANGUAGES[sel - 1][0])
            except:
                errors.append('%d is not a valid selection, please try again.')

            del g.request_params['Digits']

        # Prompt and gather if language is not valid or no choice was submitted
        if not get_lang():
            with r.gather(numDigits=1, timeout=settings.INPUT_TIMEOUT) as rg:
                if not len(errors):
                    rg.say("""Welcome to Call on Congress, the Sunlight Foundation's
                              free service that helps you keep our lawmakers accountable.
                           """)
                else:
                    rg.say(' '.join(errors))
                rg.say('Press 1 to continue in English.', language='en')
                rg.say('Presione 2 para continuar en espanol.', language='es')

            r.redirect(request.path)
            return r

    return True
Esempio n. 2
0
    def __init__(self, text, **kwargs):
        if 'language' not in kwargs.keys():
            lang = get_lang(default=settings.DEFAULT_LANGUAGE)
            kwargs.update(language=lang)

        if 'voice' not in kwargs.keys():
            kwargs.update(voice=g.request_params.get('voice', settings.DEFAULT_VOICE))

        super(Say, self).__init__(text, **kwargs)
        self.body = translate(text, **kwargs)
Esempio n. 3
0
def translate(s, **kwargs):
    query = {
        'lang': kwargs.get('language', get_lang(default=settings.DEFAULT_LANGUAGE)),
        'hash': hashlib.md5(s).hexdigest(),
    }
    if query.get('lang') == 'en':
        return s

    trans = g.db.translations.find_one(query)
    if trans:
        return trans['translation']
    else:
        try:
            trans = translator.translate(s, target=query.get('lang'))
            query.update(translation=trans.translatedText)
            s = query.get('translation')
            g.db.translations.save(query)
        except GTranslatorError:
            pass
    return s
Esempio n. 4
0
    def __new__(cls, text, **kwargs):
        if 'language' not in kwargs.keys():
            lang = get_lang(default=settings.DEFAULT_LANGUAGE)
            kwargs.update(language=lang)

        lang = kwargs['language']

        url = translate_audio(audio_filename_for(text), language=lang)
        exists = False
        try:
            exists = (requests.head(url, timeout=1.5).status_code == 200)
        except:
            pass
        # Play audio if it exists. If a voice was passed explicitly, never play audio.
        if exists and 'voice' not in g.request_params.keys():
            play = Play(audio_filename_for(text), **kwargs)
            return play
        else:  # Only adjust language via accent map if we don't have audio.
            kwargs['language'] = ACCENT_MAP.get(lang, lang)

        return super(Say, cls).__new__(cls, text, **kwargs)
Esempio n. 5
0
def translate_audio(filename, **kwargs):
    if 'language' not in kwargs.keys():
        kwargs.update(language=get_lang(default=settings.DEFAULT_LANGUAGE))
    return "%s/%s/%s" % (audio_root_as_url(), kwargs.get('language'), filename)