Skip to content

Commit

Permalink
refactor(map_locale_display_names): use set rather than looping twice…
Browse files Browse the repository at this point in the history
… to disambiguate SUPPORTED_LOCALES

thread: #6406 (comment)
  • Loading branch information
cfm committed May 3, 2022
1 parent 20c59fd commit 9621639
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions securedrop/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#
import collections

from typing import Dict, List, Set
from typing import List, Set

from babel.core import (
Locale,
Expand Down Expand Up @@ -177,22 +177,19 @@ def map_locale_display_names(config: SDConfig) -> None:
to distinguish them. For languages with more than one translation,
like Chinese, we do need the additional detail.
"""
language_locale_counts = collections.defaultdict(int) # type: Dict[str, int]
for l in sorted(config.SUPPORTED_LOCALES):
if Locale.parse(l) not in USABLE_LOCALES:
continue

locale = RequestLocaleInfo(l)
language_locale_counts[locale.language] += 1

seen: Set[str] = set()
locale_map = collections.OrderedDict()
for l in sorted(config.SUPPORTED_LOCALES):
if Locale.parse(l) not in USABLE_LOCALES:
continue

locale = RequestLocaleInfo(l)
if language_locale_counts[locale.language] > 1:
if locale.language in seen:
# Disambiguate translations for this language.
locale.use_display_name = True
else:
seen.add(locale.language)

locale_map[str(locale)] = locale

global LOCALES
Expand Down

0 comments on commit 9621639

Please sign in to comment.