예제 #1
0
def get_translations(locale: str = None):
    """
    Get all translations for given locale

    You can find more about translations :ref:`here <Translations>`.

    Args:
        locale: locale to get translations from (will override default locale)

    Returns:
        .. code-block:: guess

            {
                'namespace.translation_id' : string
            }

    .. seealso::

        :func:`.get_locales`
    """
    trs = {}
    locale = helpers._get_locale(locale).lower()
    container = i18n.translations.container
    if locale in container:
        trs = container[locale].copy()
    else:
        try:
            translate("general.locale")
            trs = container[locale].copy()
        except exceptions.APIError:
            pass
    return message.Identity("translations", trs)
예제 #2
0
파일: ui.py 프로젝트: yihuishou/happypandax
def translate(t_id: str,
              locale: str = None,
              default: str = None,
              placeholder: str = {},
              count: int = None):
    """
    Get a translation by translation id.
    Raises error if a default value was not provided and no translation was found.

    You can find more about translations :ref:`here <Translations>`.

    Args:
        t_id: translation id
        locale: locale to get translations from (will override default locale)
        default: default text when no translation was found
        placeholder: ?
        count: pluralization

    Returns:
        string

    .. seealso::

        :func:`.get_locales`
    """
    kwargs = {}
    trs = default

    kwargs["locale"] = helpers._get_locale(locale).lower()

    if placeholder:
        kwargs.update(placeholder),
    if count is not None:
        kwargs["count"] = count
    if default:
        kwargs["default"] = default

    if not t_id and default is None:
        raise exceptions.APIError(utils.this_function(),
                                  "Invalid translation id: {}".format(t_id))
    elif t_id:
        try:
            trs = i18n.t(t_id, **kwargs)
        except KeyError as e:
            if default is None:
                raise exceptions.APIError(
                    utils.this_function(),
                    "Translation id '{}' not found".format(t_id))

        except i18n.loaders.loader.I18nFileLoadError as e:
            if default is None:
                log.exception(
                    "Failed to load translation file '{}' with key '{}'".
                    format(
                        locale if locale else config.translation_locale.value,
                        t_id))
                raise exceptions.APIError(
                    utils.this_function(),
                    "Failed to load translation file: {}".format(e.args))
    return message.Identity("translation", trs)
예제 #3
0
파일: ui.py 프로젝트: yihuishou/happypandax
def get_sort_indexes(item_type: enums.ItemType = None,
                     translate: bool = True,
                     locale: str = None):
    """
    Get a list of available sort item indexes and names

    Args:
        item_type: return applicable indexes for a specific item type
        translate: translate the sort expression name
        locale: locale to get translations from (will override default locale)

    Returns:
        .. code-block:: guess

            [
                {
                    'index' : int,
                    'name': str,
                    'item_type': int value of :py:class:`.ItemType`
                },
                ...
            ]
    """
    db_sort = database_cmd.GetDatabaseSort()
    if item_type:
        item_type = enums.ItemType.get(item_type)
        items = [item_type]
    else:
        items = list(enums.ItemType)

    sort_indexes = message.List("indexes", dict)
    for i in items:
        _, db_model = i._msg_and_model()
        for idx, name in db_sort.run(db_model, name=True).items():
            if translate:
                try:
                    name = i18n.t("general.sort-idx-{}".format(idx),
                                  default=name,
                                  locale=helpers._get_locale(locale))
                except Exception:
                    pass
            sort_indexes.append({
                'index': int(idx),
                'name': name,
                'item_type': i.value
            })
    return sort_indexes