Skip to content

Commit

Permalink
Automate /locales/ page based on settings (fixes mozilla#6454)
Browse files Browse the repository at this point in the history
  • Loading branch information
robhudson committed Apr 27, 2022
1 parent 16235b5 commit 9c5b826
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 198 deletions.
102 changes: 11 additions & 91 deletions bedrock/mozorg/templates/mozorg/locales.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
file, You can obtain one at https://mozilla.org/MPL/2.0/.
#}

{% extends "base-protocol-mozilla.html" %}
{% extends "base-protocol-mozilla.html" %}

{% block page_title %}Choose your language or locale to browse Mozilla.org{% endblock %}

Expand All @@ -26,95 +26,15 @@
<header class="c-simple-header">
<h1 class="c-simple-header-title">Choose your language</h1>
</header>
<section id="america" class="c-block-list">
<h2 class="c-block-list-title">America</h2>
<ul>
<li lang="cak"><a href="/cak/">Maya Kaqchikel</a></li>
<li lang="en-CA"><a href="/en-CA/">English (Canadian)</a></li>
<li lang="en-US"><a href="/en-US/">English (US)</a></li>
<li lang="es-AR"><a href="/es-AR/">Español (de Argentina)</a></li>
<li lang="es-CL"><a href="/es-CL/">Español (de Chile)</a></li>
<li lang="es-MX"><a href="/es-MX/">Español (de México)</a></li>
<li lang="gn"><a href="/gn/">Avañe'ẽ</a></li>
<li lang="pt-BR"><a href="/pt-BR/">Português (do Brasil)</a></li>
<li lang="trs"><a href="/trs/">Triqui</a></li>
</ul>
</section>
<section id="asia-pacific" class="c-block-list">
<h2 class="c-block-list-title">Asia Pacific</h2>
<ul>
<li lang="gu-IN"><a href="/gu-IN/">જરાતી (ભારત)</a></li>
<li lang="hi-IN"><a href="/hi-IN/">हिन्दी (भारत)</a></li>
<li lang="id"><a href="/id/">Bahasa Indonesia</a></li>
<li lang="ja"><a href="/ja/">日本語</a></li>
<li lang="ko"><a href="/ko/">한국어</a></li>
<li lang="ml"><a href="/ml/">മലയാളം</a></li>
<li lang="mr"><a href="/mr/">मराठी</a></li>
<li lang="ms"><a href="/ms/">Melayu</a></li>
<li lang="pa-IN"><a href="/pa-IN/">ਪੰਜਾਬੀ (ਭਾਰਤ)</a></li>
<li lang="ta"><a href="/ta/">தமிழ்</a></li>
<li lang="th"><a href="/th/">ไทย</a></li>
<li lang="zh-CN"><a href="/zh-CN/">中文 (简体)</a></li>
<li lang="zh-TW"><a href="/zh-TW/">正體中文 (繁體)</a></li>
</ul>
</section>
<section id="europe" class="c-block-list">
<h2 class="c-block-list-title">Europe</h2>
<ul>
<li lang="an"><a href="/an/">aragonés</a></li>
<li lang="ast"><a href="/ast/">Asturianu</a></li>
<li lang="be"><a href="/be/">Беларуская</a></li>
<li lang="bg"><a href="/bg/">Български</a></li>
<li lang="bs"><a href="/bs/">Bosanski</a></li>
<li lang="ca"><a href="/ca/">Català</a></li>
<li lang="cs"><a href="/cs/">Čeština</a></li>
<li lang="cy"><a href="/cy/">Cymraeg</a></li>
<li lang="da"><a href="/da/">Dansk</a></li>
<li lang="de"><a href="/de/">Deutsch</a></li>
<li lang="dsb"><a href="/dsb/">Dolnoserbšćina</a></li>
<li lang="el"><a href="/el/">Ελληνικά</a></li>
<li lang="en-GB"><a href="/en-GB/">English (British)</a></li>
<li lang="eo"><a href="/eo/">Esperanto</a></li>
<li lang="es-ES"><a href="/es-ES/">Español (de España)</a></li>
<li lang="et"><a href="/et/">Eesti keel</a></li>
<li lang="eu"><a href="/eu/">Euskara</a></li>
<li lang="fi"><a href="/fi/">suomi</a></li>
<li lang="fr"><a href="/fr/">Français</a></li>
<li lang="fy-NL"><a href="/fy-NL/">Frysk</a></li>
<li lang="gl"><a href="/gl/">Galego</a></li>
<li lang="hsb"><a href="/hsb/">Hornjoserbsce</a></li>
<li lang="hu"><a href="/hu/">magyar</a></li>
<li lang="hy-AM"><a href="/hy-AM/">Հայերեն</a></li>
<li lang="ia"><a href="/ia/">Interlingua</a></li>
<li lang="it"><a href="/it/">Italiano</a></li>
<li lang="ka"><a href="/ka/">ქართული</a></li>
<li lang="lij"><a href="/lij/">Ligure</a></li>
<li lang="lt"><a href="/lt/">Lietuvių</a></li>
<li lang="nb-NO"><a href="/nb-NO/">Norsk bokmål</a></li>
<li lang="nl"><a href="/nl/">Nederlands</a></li>
<li lang="nn-NO"><a href="/nn-NO/">Norsk nynorsk</a></li>
<li lang="pl"><a href="/pl/">Polski</a></li>
<li lang="pt-PT"><a href="/pt-PT/">Português (Europeu)</a></li>
<li lang="rm"><a href="/rm/">rumantsch</a></li>
<li lang="ro"><a href="/ro/">Română</a></li>
<li lang="ru"><a href="/ru/">Русский</a></li>
<li lang="sk"><a href="/sk/">slovenčina</a></li>
<li lang="sl"><a href="/sl/">Slovenščina</a></li>
<li lang="sq"><a href="/sq/">Shqip</a></li>
<li lang="sr"><a href="/sr/">Српски</a></li>
<li lang="sv-SE"><a href="/sv-SE/">Svenska</a></li>
<li lang="tr"><a href="/tr/">Türkçe</a></li>
<li lang="uk"><a href="/uk/">Українська</a></li>
</ul>
</section>
<section id="middle-east" class="c-block-list">
<h2 class="c-block-list-title">Middle East and Africa</h2>
<ul>
<li lang="ar"><a href="/ar/">عربي</a></li>
<li lang="az"><a href="/az/">Azərbaycanca</a></li>
<li lang="fa"><a href="/fa/">فارسی</a></li>
<li lang="kab"><a href="/kab/">Taqbaylit</a></li>
</ul>
</section>
{% for region, locales in settings.LOCALES_BY_REGION.items() %}
<section id="{{ region|slugify }}" class="c-block-list">
<h2 class="c-block-list-title">{{ region }}</h2>
<ul>
{% for loc in locales %}
<li lang="{{ loc }}"><a href="/{{ loc }}/" title="Browse Mozilla.org in the {{ languages[loc]['English'] }} language">{{ languages[loc]["native"] }}</a></li>
{% endfor %}
</ul>
</section>
{% endfor %}
</main>
{% endblock %}
12 changes: 12 additions & 0 deletions bedrock/mozorg/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
from unittest.mock import ANY, patch

from django.conf import settings
from django.test import RequestFactory

from bedrock.mozorg.tests import TestCase
Expand Down Expand Up @@ -66,3 +67,14 @@ def test_url_pattern_no_slash(self, l10n_mock):
"The url route should pass through unchanged"
url = page("dude/abides.json", "dude/abides.html", donny="ashes")
assert str(url.pattern) == "dude/abides.json"


class TestProdLocales(TestCase):
def test_no_dupes(self):
# Make sure we didn't duplicate a locale in more than one region.
assert set.intersection(*[set(locales) for locales in settings.LOCALES_BY_REGION.values()]) == set()

def test_inclusive(self):
# Make sure all locales are included in `PROD_LANGUAGES`.
# We add 1 for the "ja-JP-mac" exception.
assert sum([len(locales) for locales in settings.LOCALES_BY_REGION.values()]) + 1 == len(settings.PROD_LANGUAGES)
2 changes: 1 addition & 1 deletion bedrock/mozorg/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
path("microsummaries/0.1", views.namespaces, {"namespace": "microsummaries"}),
path("projects/xforms/2005/type", views.namespaces, {"namespace": "xforms-type"}),
path("xbl", views.namespaces, {"namespace": "xbl"}),
page("locales/", "mozorg/locales.html"),
path("locales/", views.locales, name="mozorg.locales"),
# Diversity and inclusion redirect
redirect(r"^diversity/$", "mozorg.diversity.2021.index", name="diversity", locale_prefix=False),
# Main paths
Expand Down
7 changes: 7 additions & 0 deletions bedrock/mozorg/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.views.generic import TemplateView

from commonware.decorators import xframe_allow
from product_details import product_details
from sentry_sdk import capture_exception

from bedrock.base.waffle import switch
Expand Down Expand Up @@ -104,6 +105,12 @@ def get_context_data(self, **kwargs):
}


@require_safe
def locales(request):
context = {"languages": product_details.languages}
return l10n_utils.render(request, "mozorg/locales.html", context)


def namespaces(request, namespace):
context = NAMESPACES[namespace]
context["slug"] = namespace
Expand Down
192 changes: 88 additions & 104 deletions bedrock/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,110 +128,94 @@ def data_path(*args):
# path to updated p-d data for testing before loading into DB
PROD_DETAILS_TEST_DIR = str(Path(PROD_DETAILS_JSON_REPO_PATH).joinpath("public", "1.0"))

# Accepted locales
PROD_LANGUAGES = (
"ach",
"af",
"an",
"ar",
"ast",
"az",
"azz",
"be",
"bg",
"bn",
"br",
"bs",
"ca",
"cak",
"cs",
"cy",
"da",
"de",
"dsb",
"el",
"en-CA",
"en-GB",
"en-US",
"eo",
"es-AR",
"es-CL",
"es-ES",
"es-MX",
"et",
"eu",
"fa",
"ff",
"fi",
"fr",
"fy-NL",
"ga-IE",
"gd",
"gl",
"gn",
"gu-IN",
"he",
"hi-IN",
"hr",
"hsb",
"hu",
"hy-AM",
"ia",
"id",
"is",
"it",
"ja",
"ja-JP-mac",
"ka",
"kab",
"kk",
"km",
"kn",
"ko",
"lij",
"lt",
"ltg",
"lv",
"mk",
"ml",
"mr",
"ms",
"my",
"nb-NO",
"ne-NP",
"nl",
"nn-NO",
"oc",
"pa-IN",
"pl",
"pt-BR",
"pt-PT",
"rm",
"ro",
"ru",
"sco",
"si",
"sk",
"sl",
"son",
"sq",
"sr",
"sv-SE",
"ta",
"te",
"th",
"tl",
"tr",
"trs",
"uk",
"ur",
"uz",
"vi",
"xh",
"zh-CN",
"zh-TW",
"zu",
)
# Regions defined on the `/locales/` page.
LOCALES_BY_REGION = {
"Americas": ["azz", "cak", "en-CA", "en-US", "es-AR", "es-CL", "es-MX", "gn", "is", "pt-BR", "trs"],
"Asia Pacific": [
"bn",
"hi-IN",
"id",
"ja",
"kk",
"km",
"ko",
"ml",
"mr",
"ms",
"my",
"ne-NP",
"pa-IN",
"si",
"ta",
"te",
"th",
"tl",
"ur",
"vi",
"zh-CN",
"zh-TW",
],
"Europe": [
"an",
"ast",
"be",
"bg",
"br",
"bs",
"ca",
"cs",
"cy",
"da",
"de",
"dsb",
"el",
"en-GB",
"eo",
"es-ES",
"et",
"eu",
"fi",
"fr",
"fy-NL",
"ga-IE",
"gd",
"gl",
"hr",
"hsb",
"hu",
"hy-AM",
"ia",
"it",
"ka",
"lij",
"lt",
"ltg",
"lv",
"mk",
"nb-NO",
"nl",
"nn-NO",
"oc",
"pl",
"pt-PT",
"rm",
"ro",
"ru",
"sco",
"sk",
"sl",
"sq",
"sr",
"sv-SE",
"tr",
"uk",
"uz",
],
"Middle East and Africa": ["ach", "af", "ar", "az", "fa", "ff", "gu-IN", "he", "kab", "kn", "son", "xh", "zu"],
}

# Our accepted production locales are the values from the above, plus an exception.
PROD_LANGUAGES = sorted(sum(LOCALES_BY_REGION.values(), []) + ["ja-JP-mac"])

GITHUB_REPO = "https://github.com/mozilla/bedrock"

Expand Down
4 changes: 2 additions & 2 deletions tests/pages/locales.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class LocalesPage(BasePage):

_URL_TEMPLATE = "/locales/"

_america_locales_locator = (By.CSS_SELECTOR, "#america ul > li > a")
_america_locales_locator = (By.CSS_SELECTOR, "#americas ul > li > a")
_asia_pacific_locales_locator = (By.CSS_SELECTOR, "#asia-pacific ul > li > a")
_europe_locales_locator = (By.CSS_SELECTOR, "#europe ul > li > a")
_middle_east_locales_locator = (By.CSS_SELECTOR, "#middle-east ul > li > a")
_middle_east_locales_locator = (By.CSS_SELECTOR, "#middle-east-and-africa ul > li > a")

@property
def number_of_america_locales(self):
Expand Down

0 comments on commit 9c5b826

Please sign in to comment.