def get_available_languages(domain):
    """Lists the available languages for the given translation domain.

    :param domain: the domain to get languages for
    """
    if domain in _AVAILABLE_LANGUAGES:
        return copy.copy(_AVAILABLE_LANGUAGES[domain])

    localedir = os.environ.get(_locale.get_locale_dir_variable_name(domain))
    find = lambda x: gettext.find(domain,
                                  localedir=localedir,
                                  languages=[x])

    # NOTE(mrodden): en_US should always be available (and first in case
    # order matters) since our in-line message strings are en_US
    language_list = ['en_US']
    locale_identifiers = localedata.locale_identifiers()
    language_list.extend(language for language in locale_identifiers
                         if find(language))

    # In Babel 1.3, locale_identifiers() doesn't list some OpenStack supported
    # locales (e.g. 'zh_CN', and 'zh_TW') so we add the locales explicitly if
    # necessary so that they are listed as supported.
    aliases = {'zh': 'zh_CN',
               'zh_Hant_HK': 'zh_HK',
               'zh_Hant': 'zh_TW',
               'fil': 'tl_PH'}

    language_list.extend(alias for locale, alias in six.iteritems(aliases)
                         if (locale in language_list and
                             alias not in language_list))

    _AVAILABLE_LANGUAGES[domain] = language_list
    return copy.copy(language_list)
Beispiel #2
0
def get_available_languages(domain):
    """Lists the available languages for the given translation domain.

    :param domain: the domain to get languages for
    """
    if domain in _AVAILABLE_LANGUAGES:
        return copy.copy(_AVAILABLE_LANGUAGES[domain])

    localedir = os.environ.get(_locale.get_locale_dir_variable_name(domain))
    find = lambda x: gettext.find(domain, localedir=localedir, languages=[x])

    # NOTE(mrodden): en_US should always be available (and first in case
    # order matters) since our in-line message strings are en_US
    language_list = ['en_US']
    locale_identifiers = localedata.locale_identifiers()
    language_list.extend(language for language in locale_identifiers
                         if find(language))

    # In Babel 1.3, locale_identifiers() doesn't list some OpenStack supported
    # locales (e.g. 'zh_CN', and 'zh_TW') so we add the locales explicitly if
    # necessary so that they are listed as supported.
    aliases = {
        'zh': 'zh_CN',
        'zh_Hant_HK': 'zh_HK',
        'zh_Hant': 'zh_TW',
        'fil': 'tl_PH'
    }

    language_list.extend(
        alias for locale, alias in six.iteritems(aliases)
        if (locale in language_list and alias not in language_list))

    _AVAILABLE_LANGUAGES[domain] = language_list
    return copy.copy(language_list)
Beispiel #3
0
    def test_get_available_languages_real(self, m_environ, m_languages_get):
        """Ensure all languages are registered if the localedir is set

        :param m_environ: patch os.environ.get to fake RADLOGGERPY_LOCALEDIR
        :param m_languages_get: reset _factory _AVAILALE_LANGUAGES variable
        :return:
        """
        m_languages_get.return_value = None
        m_environ.get.return_value = 'radloggerpy/locale'

        m_languages = ['en_US']
        locale_identifiers = set(locale.windows_locale.values())
        localedir = os.environ.get(
            _locale.get_locale_dir_variable_name(DOMAIN))

        m_locale = radloggerpy.__path__[0] + '/locale'
        m_locale_dirs = [
            o for o in os.listdir(m_locale)
            if os.path.isdir(os.path.join(m_locale, o))
        ]

        for m_locale_dir in m_locale_dirs:
            m_languages.extend(language for language in locale_identifiers
                               if m_locale_dir in language)

        m_languages.extend(
            alias for alias, _ in _BABEL_ALIASES.items()
            if gettext.find(DOMAIN, localedir=localedir, languages=[alias]))

        self.assertItemsEqual(m_languages, _i18n.get_available_languages())
Beispiel #4
0
    def _translate_msgid(msgid,
                         domain,
                         desired_locale=None,
                         has_contextual_form=False,
                         has_plural_form=False):
        if not desired_locale:
            system_locale = locale.getdefaultlocale()
            # If the system locale is not available to the runtime use English
            desired_locale = system_locale[0] or 'en_US'

        locale_dir = os.environ.get(
            _locale.get_locale_dir_variable_name(domain))
        lang = gettext.translation(domain,
                                   localedir=locale_dir,
                                   languages=[desired_locale],
                                   fallback=True)

        # Primary translation function
        if not has_contextual_form and not has_plural_form:
            translator = lang.gettext if six.PY3 else lang.ugettext

            translated_message = translator(msgid)
            return translated_message

        # Contextual translation function
        if has_contextual_form and not has_plural_form:
            (msgctx, msgtxt) = msgid
            translator = lang.gettext if six.PY3 else lang.ugettext

            msg_with_ctx = "%s%s%s" % (msgctx, CONTEXT_SEPARATOR, msgtxt)
            translated_message = translator(msg_with_ctx)

            if CONTEXT_SEPARATOR in translated_message:
                # Translation not found
                translated_message = msgtxt

            return translated_message

        # Plural translation function
        if not has_contextual_form and has_plural_form:
            (msgsingle, msgplural, msgcount) = msgid
            translator = lang.ngettext if six.PY3 else lang.ungettext

            translated_message = translator(msgsingle, msgplural, msgcount)
            return translated_message

        # Reserved for contextual and plural translation function
        if has_contextual_form and has_plural_form:
            raise ValueError("Unimplemented.")

        raise TypeError("Unknown msgid type.")
Beispiel #5
0
    def __init__(self, domain, localedir=None):
        """Establish a set of translation functions for the domain.

        :param domain: Name of translation domain,
                       specifying a message catalog.
        :type domain: str
        :param localedir: Directory with translation catalogs.
        :type localedir: str
        """
        self.domain = domain
        if localedir is None:
            variable_name = _locale.get_locale_dir_variable_name(domain)
            localedir = os.environ.get(variable_name)
        self.localedir = localedir
Beispiel #6
0
    def __init__(self, domain, localedir=None):
        """Establish a set of translation functions for the domain.

        :param domain: Name of translation domain,
                       specifying a message catalog.
        :type domain: str
        :param localedir: Directory with translation catalogs.
        :type localedir: str
        """
        self.domain = domain
        if localedir is None:
            variable_name = _locale.get_locale_dir_variable_name(domain)
            localedir = os.environ.get(variable_name)
        self.localedir = localedir
Beispiel #7
0
    def _translate_msgid(msgid, domain, desired_locale=None,
                         has_contextual_form=False, has_plural_form=False):
        if not desired_locale:
            system_locale = locale.getdefaultlocale()
            # If the system locale is not available to the runtime use English
            desired_locale = system_locale[0] or 'en_US'

        locale_dir = os.environ.get(
            _locale.get_locale_dir_variable_name(domain)
        )
        lang = gettext.translation(domain,
                                   localedir=locale_dir,
                                   languages=[desired_locale],
                                   fallback=True)

        # Primary translation function
        if not has_contextual_form and not has_plural_form:
            translator = lang.gettext if six.PY3 else lang.ugettext

            translated_message = translator(msgid)
            return translated_message

        # Contextual translation function
        if has_contextual_form and not has_plural_form:
            (msgctx, msgtxt) = msgid
            translator = lang.gettext if six.PY3 else lang.ugettext

            msg_with_ctx = "%s%s%s" % (msgctx, CONTEXT_SEPARATOR, msgtxt)
            translated_message = translator(msg_with_ctx)

            if CONTEXT_SEPARATOR in translated_message:
                # Translation not found
                translated_message = msgtxt

            return translated_message

        # Plural translation function
        if not has_contextual_form and has_plural_form:
            (msgsingle, msgplural, msgcount) = msgid
            translator = lang.ngettext if six.PY3 else lang.ungettext

            translated_message = translator(msgsingle, msgplural, msgcount)
            return translated_message

        # Reserved for contextual and plural translation function
        if has_contextual_form and has_plural_form:
            raise ValueError("Unimplemented.")

        raise TypeError("Unknown msgid type.")
Beispiel #8
0
    def _translate_msgid(msgid, domain, desired_locale=None):
        if not desired_locale:
            system_locale = locale.getdefaultlocale()
            # If the system locale is not available to the runtime use English
            desired_locale = system_locale[0] or 'en_US'

        locale_dir = os.environ.get(
            _locale.get_locale_dir_variable_name(domain))
        lang = gettext.translation(domain,
                                   localedir=locale_dir,
                                   languages=[desired_locale],
                                   fallback=True)
        translator = lang.gettext if six.PY3 else lang.ugettext

        translated_message = translator(msgid)
        return translated_message
Beispiel #9
0
    def _translate_msgid(msgid,
                         domain,
                         desired_locale=None,
                         has_contextual_form=False,
                         has_plural_form=False):
        if not desired_locale:
            system_locale = locale.getdefaultlocale()
            # If the system locale is not available to the runtime use English
            if not system_locale or not system_locale[0]:
                desired_locale = 'en_US'
            else:
                desired_locale = system_locale[0]

        locale_dir = os.environ.get(
            _locale.get_locale_dir_variable_name(domain))
        lang = gettext.translation(domain,
                                   localedir=locale_dir,
                                   languages=[desired_locale],
                                   fallback=True)

        if not has_contextual_form and not has_plural_form:
            # This is the most common case, so check it first.
            translator = lang.gettext if six.PY3 else lang.ugettext
            translated_message = translator(msgid)

        elif has_contextual_form and has_plural_form:
            # Reserved for contextual and plural translation function,
            # which is not yet implemented.
            raise ValueError("Unimplemented.")

        elif has_contextual_form:
            (msgctx, msgtxt) = msgid
            translator = lang.gettext if six.PY3 else lang.ugettext

            msg_with_ctx = "%s%s%s" % (msgctx, CONTEXT_SEPARATOR, msgtxt)
            translated_message = translator(msg_with_ctx)

            if CONTEXT_SEPARATOR in translated_message:
                # Translation not found, use the original text
                translated_message = msgtxt

        elif has_plural_form:
            (msgsingle, msgplural, msgcount) = msgid
            translator = lang.ngettext if six.PY3 else lang.ungettext
            translated_message = translator(msgsingle, msgplural, msgcount)

        return translated_message
Beispiel #10
0
    def _translate_msgid(msgid, domain, desired_locale=None):
        if not desired_locale:
            system_locale = locale.getdefaultlocale()
            # If the system locale is not available to the runtime use English
            desired_locale = system_locale[0] or 'en_US'

        locale_dir = os.environ.get(
            _locale.get_locale_dir_variable_name(domain)
        )
        lang = gettext.translation(domain,
                                   localedir=locale_dir,
                                   languages=[desired_locale],
                                   fallback=True)
        translator = lang.gettext if six.PY3 else lang.ugettext

        translated_message = translator(msgid)
        return translated_message
Beispiel #11
0
def get_available_languages(domain):
    """Lists the available languages for the given translation domain.

    :param domain: the domain to get languages for
    """
    if domain in _AVAILABLE_LANGUAGES:
        return copy.copy(_AVAILABLE_LANGUAGES[domain])

    localedir = os.environ.get(_locale.get_locale_dir_variable_name(domain))
    find = lambda x: gettext.find(domain,
                                  localedir=localedir,
                                  languages=[x])

    # NOTE(mrodden): en_US should always be available (and first in case
    # order matters) since our in-line message strings are en_US
    language_list = ['en_US']
    locale_identifiers = localedata.locale_identifiers()
    language_list.extend(language for language in locale_identifiers
                         if find(language))

    # In Babel 1.3, locale_identifiers() doesn't list some OpenStack supported
    # locales (e.g. 'zh_CN', and 'zh_TW') so we add the locales explicitly if
    # necessary so that they are listed as supported.
    aliases = {'zh': 'zh_CN',
               'zh_Hant_HK': 'zh_HK',
               'zh_Hant': 'zh_TW',
               'fil': 'tl_PH'}
    language_list.extend(alias for locale, alias in aliases.items()
                         if (locale in language_list and
                             alias not in language_list))

    language_list.extend(alias for locale, alias in aliases.items()
                         if (locale not in language_list and
                             find(alias)))

    # In webob.acceptparse, the best_match is just match the first element in
    # the language_list, so make the precise element in front
    result = ['en_US']
    for i in language_list[1:]:
        if '_' in i:
            result.insert(1, i)
        else:
            result.append(i)

    _AVAILABLE_LANGUAGES[domain] = result
    return copy.copy(result)
Beispiel #12
0
    def _translate_msgid(msgid, domain, desired_locale=None,
                         has_contextual_form=False, has_plural_form=False):
        if not desired_locale:
            system_locale = locale.getdefaultlocale()
            # If the system locale is not available to the runtime use English
            if not system_locale or not system_locale[0]:
                desired_locale = 'en_US'
            else:
                desired_locale = system_locale[0]

        locale_dir = os.environ.get(
            _locale.get_locale_dir_variable_name(domain)
        )
        lang = gettext.translation(domain,
                                   localedir=locale_dir,
                                   languages=[desired_locale],
                                   fallback=True)

        if not has_contextual_form and not has_plural_form:
            # This is the most common case, so check it first.
            translator = lang.gettext if six.PY3 else lang.ugettext
            translated_message = translator(msgid)

        elif has_contextual_form and has_plural_form:
            # Reserved for contextual and plural translation function,
            # which is not yet implemented.
            raise ValueError("Unimplemented.")

        elif has_contextual_form:
            (msgctx, msgtxt) = msgid
            translator = lang.gettext if six.PY3 else lang.ugettext

            msg_with_ctx = "%s%s%s" % (msgctx, CONTEXT_SEPARATOR, msgtxt)
            translated_message = translator(msg_with_ctx)

            if CONTEXT_SEPARATOR in translated_message:
                # Translation not found, use the original text
                translated_message = msgtxt

        elif has_plural_form:
            (msgsingle, msgplural, msgcount) = msgid
            translator = lang.ngettext if six.PY3 else lang.ungettext
            translated_message = translator(msgsingle, msgplural, msgcount)

        return translated_message
Beispiel #13
0
def get_available_languages(domain):
    """Lists the available languages for the given translation domain.

    :param domain: the domain to get languages for
    """
    if domain in _AVAILABLE_LANGUAGES:
        return copy.copy(_AVAILABLE_LANGUAGES[domain])

    localedir = os.environ.get(_locale.get_locale_dir_variable_name(domain))
    find = lambda x: gettext.find(domain, localedir=localedir, languages=[x])

    # NOTE(mrodden): en_US should always be available (and first in case
    # order matters) since our in-line message strings are en_US
    language_list = ['en_US']
    locale_identifiers = localedata.locale_identifiers()
    language_list.extend(language for language in locale_identifiers
                         if find(language))

    # In Babel 1.3, locale_identifiers() doesn't list some OpenStack supported
    # locales (e.g. 'zh_CN', and 'zh_TW') so we add the locales explicitly if
    # necessary so that they are listed as supported.
    aliases = {
        'zh': 'zh_CN',
        'zh_Hant_HK': 'zh_HK',
        'zh_Hant': 'zh_TW',
        'fil': 'tl_PH'
    }
    language_list.extend(
        alias for locale, alias in aliases.items()
        if (locale in language_list and alias not in language_list))

    language_list.extend(alias for locale, alias in aliases.items()
                         if (locale not in language_list and find(alias)))

    # In webob.acceptparse, the best_match is just match the first element in
    # the language_list, so make the precise element in front
    result = ['en_US']
    for i in language_list[1:]:
        if '_' in i:
            result.insert(1, i)
        else:
            result.append(i)

    _AVAILABLE_LANGUAGES[domain] = result
    return copy.copy(result)
Beispiel #14
0
def get_available_languages(domain):
    """Lists the available languages for the given translation domain.

    :param domain: the domain to get languages for
    """
    if domain in _AVAILABLE_LANGUAGES:
        return copy.copy(_AVAILABLE_LANGUAGES[domain])

    localedir = os.environ.get(_locale.get_locale_dir_variable_name(domain))

    def find(x):
        return gettext.find(domain, localedir=localedir, languages=[x])

    # NOTE(mrodden): en_US should always be available (and first in case
    # order matters) since our in-line message strings are en_US
    language_list = ['en_US']
    locale_identifiers = set(locale.windows_locale.values())
    language_list.extend(language for language in locale_identifiers
                         if find(language))
    language_list.extend(alias for alias, _ in _BABEL_ALIASES.items()
                         if find(alias))

    _AVAILABLE_LANGUAGES[domain] = language_list
    return copy.copy(language_list)
 def test_make_variable_name(self):
     var = _locale.get_locale_dir_variable_name(self.domain)
     self.assertEqual(self.expected, var)
 def test_make_variable_name(self):
     var = _locale.get_locale_dir_variable_name(self.domain)
     self.assertEqual(self.expected, var)