Skip to content

i18n: Fix untranslated strings in views and errors #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion invenio_stats/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

"""Errors used in Invenio-Stats."""

from invenio_i18n import gettext as _
from invenio_rest.errors import RESTException

##
Expand Down Expand Up @@ -71,6 +72,8 @@ def __init__(self, query_name):
"""
super(RESTException, self).__init__()
self.query_name = query_name
self.description = "Unknown statistic '{}'".format(query_name)
self.description = _(
"Unknown statistic '%(query_name)s'", query_name=query_name
)
Comment on lines +75 to +77
Copy link
Member

@Samk13 Samk13 Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Did you try if this style works with gettext, or should we use the following instead?

Suggested change
self.description = _(
"Unknown statistic '%(query_name)s'", query_name=query_name
)
self.description = _(
"Unknown statistic '%(query_name)s'"
) % { "query_name": query_name }

The same question applies to the rest of the strings with gettext.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just retried in invenio shell and it works for me:

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and for non-english:

image


code = 400
18 changes: 11 additions & 7 deletions invenio_stats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""InvenioStats views."""

from flask import Blueprint, abort, jsonify, request
from invenio_i18n import gettext as _
from invenio_rest.views import ContentNegotiatedMethodView
from invenio_search.engine import search

Expand Down Expand Up @@ -38,7 +39,7 @@ def __init__(self, **kwargs):
"GET": "application/json",
},
default_media_type="application/json",
**kwargs
**kwargs,
)

def post(self, **kwargs):
Expand All @@ -60,9 +61,11 @@ def post(self, **kwargs):
# 'config' has to be a dictionary with mandatory 'stat' key and
# optional 'params' key, and nothing else
raise InvalidRequestInputError(
"Invalid Input. It should be of the form "
'{ STATISTIC_NAME: { "stat": STAT_TYPE, '
'"params": STAT_PARAMS }}'
_(
"Invalid Input. It should be of the form "
'{ STATISTIC_NAME: { "stat": STAT_TYPE, '
'"params": STAT_PARAMS }}'
)
)

stat = config["stat"]
Expand All @@ -74,10 +77,11 @@ def post(self, **kwargs):

permission = current_stats.permission_factory(stat, params)
if permission is not None and not permission.can():
message = (
message = _(
"You do not have a permission to query the "
'statistic "{}" with those '
"parameters".format(stat)
'statistic "%(stat)s" with those '
"parameters",
stat=stat,
)

if current_user.is_authenticated:
Expand Down
1 change: 0 additions & 1 deletion tests/test_aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"""Aggregation tests."""

import datetime
from turtle import pd
from unittest.mock import patch

import pytest
Expand Down
Loading