Skip to content
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

feat(validate_locale_configuration): discard and warn on configured but unsupported locale #7443

Merged
merged 3 commits into from
Feb 24, 2025
Merged
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
17 changes: 15 additions & 2 deletions securedrop/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import collections
import json
from pathlib import Path
from typing import DefaultDict, List, OrderedDict, Set

from babel.core import (
Expand All @@ -29,6 +31,8 @@
from flask_babel import Babel
from sdconfig import FALLBACK_LOCALE, SecureDropConfig

I18N_CONF = Path(__file__).parent / "i18n.json"


class RequestLocaleInfo:
"""
Expand Down Expand Up @@ -137,16 +141,25 @@ def validate_locale_configuration(config: SecureDropConfig, babel: Babel) -> Set
available = set(babel.list_translations())
available.add(Locale.parse(FALLBACK_LOCALE))

# These locales are supported in the current version of securedrop-app-code.
try:
with open(I18N_CONF) as i18n_conf_file:
i18n_conf = json.load(i18n_conf_file)
supported = parse_locale_set(i18n_conf["supported_locales"].keys())
# I18N_CONF may not be available under test.
except FileNotFoundError:
Copy link
Member

Choose a reason for hiding this comment

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

In what case would the file not be found? If i18n.json is missing, that feels like a fatal error to me.

Copy link
Member Author

@cfm cfm Feb 21, 2025

Choose a reason for hiding this comment

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

Clarified in ef98aa0. Let me know if this doesn't address your concern.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, but why would it not be available under test? Is something deleting it?

Copy link
Member

Choose a reason for hiding this comment

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

2b1214c has a green check. I'll turn it into a PR, but we don't need to get it into 2.12.

If I had to guess, if it was failing earlier possibly because the path was relative, but now that you changed it to an absolute path, it works?

supported = available

# These locales were configured via "securedrop-admin sdconfig", meaning
# they were present on the Admin Workstation at "securedrop-admin" runtime.
configured = parse_locale_set(config.SUPPORTED_LOCALES)

# The intersection of these sets is the set of locales usable by Babel.
usable = available & configured
usable = available & configured & supported

missing = configured - usable
if missing:
babel.app.logger.error(
babel.app.logger.warning(
f"Configured locales {missing} are not in the set of usable locales {usable}"
)

Expand Down