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

Fix root global page active locales #15025

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion bedrock/base/templates/404-locale.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ <h1 class="c-simple-header-title">
<section class="c-block-list">
<ul>
{% for locale in available_locales if locale in languages %}
<li lang="{{ locale }}"><a href="/{{ locale }}{{ request.path_info }}" title="{{ 'Browse {0} in the {1} language'|f(request.path_info, languages[locale]["native"]) }}">{{ languages[locale]['native'] }}</a></li>
<li lang="{{ locale }}"><a href="/{{ locale }}{{ request.path_info }}" title="{{ 'Browse {0} in the {1} language'|f(request.path_info, languages[locale]["English"]) }}">{{ languages[locale]['native'] }}</a></li>
{% endfor %}
</ul>
</section>
Expand Down
4 changes: 3 additions & 1 deletion lib/l10n_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ def render(request, template, context=None, ftl_files=None, activation_files=Non
translations.update(ftl_active_locales(af))
translations = sorted(translations) # `sorted` returns a list.
elif l10n:
translations = l10n.active_locales
translations = l10n.active_locales if not is_root_path_with_no_language_clues(request) else l10n.active_home_locales
allowed = settings.PROD_LANGUAGES if not settings.DEV else settings.DEV_LANGUAGES
translations = set(translations).intersection(allowed)
Comment on lines +172 to +174
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 had to pick a place where to allow only approved locales among all the layers it travels through… but it seemed the least intrusive to just do it here, after getting stuff from either context or l10n instance, and also before adding any extra locales in case the extras are intentionally outside of the allowed range, not filtering them out later. Also, not filtering if the language list comes from other source as CMS translations or activation files (=keeping most of the current logic intact and only making changes that may ever impact the is_root case).


# if `add_active_locales` is given then add it to the translations for the template
if "add_active_locales" in context:
Expand Down
5 changes: 5 additions & 0 deletions lib/l10n_utils/fluent.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ def active_locales(self):
# first resource is the one to check for activation
return get_active_locales(self.resource_ids[0])

@cached_property
def active_home_locales(self):
# use mozorg/home to check for activation
return get_active_locales("mozorg/home.ftl")

Comment on lines 84 to +91
Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, umm, instead of butchering the getter above it (potentially affecting gazillion page hits), it felt safer to just add a new one, doing just this one job checking for homepage languages separately, and using that in addition for the is_root condition. Maybe stupid, take this as more of a pseudo-code @stevejalim, and tear it apart as you see fit.

The hardcoded path string feels somewhat fragile here. Is there a better way?
(Also, some clever tests to cover the root specifics?)

Copy link
Member

Choose a reason for hiding this comment

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

The path seems fine as long as we have a test that leverages this. That way if it gets refactored away at some later point in time a test will break to know we depended on that.

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 was thinking whether having an entry in SETTINGS would be more resilient, to keep the value of "homepage" locale file managed and visible in an obvious place, instead of having a string hardcoded somewhere deep in the code.

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'm still not entirely sure the separate method is needed, as from I can infer it looks like the

def active_locales(self):
# first resource is the one to check for activation
return get_active_locales(self.resource_ids[0])

right above the addition only does that, can't seem to find any use besides listing active locales (presumably for home page), but using the first ftl file which is the 404 file — and can't think of use case that would benefit from reading the activation threshold for a locale based on that 404 file per se… so I'm doubting whether a separate method is not overly safe, and the original method shouldn't be updated to look for the metadata in a homepage ftl file, instead of the 404 resource_id. (But wasn't able to inspect all the layers this gets used throughout the app, so can't tell there's not a specific use case that I wasn't able to find…)

@cached_property
def percent_translated(self):
if not self._message_ids:
Expand Down