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

Add normalize language function and tests #41

Open
wants to merge 2 commits 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
19 changes: 18 additions & 1 deletion _tests/test_createJson.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import json
from _validate import (
createJson,
addonManifest
addonManifest,
manifestLoader
)

TOP_DIR = os.path.abspath(os.path.dirname(__file__))
Expand Down Expand Up @@ -110,3 +111,19 @@ def test_validVersion(self):
"1.2.0.json",
msg="Name of the output file should be named based on version number"
)


class Test_normalizeLanguage(unittest.TestCase):
"""Set of unit tests for `manifestLoader.normalizeLanguage`."""

def test_normalization_no_country_info(self):
"""Makes sure that if no country info is provided language is normalized to lower case."""
self.assertEqual("en", manifestLoader.normalizeLanguage("en"))
self.assertEqual("en", manifestLoader.normalizeLanguage("EN"))
self.assertEqual("kmr", manifestLoader.normalizeLanguage("kmr"))

def test_underscore_used_as_separator_after_normalization(self):
"""Ensures that underscore is used to separate country info from language.
Also implicitly test the fact that country code is converted to upper case."""
self.assertEqual("pt_BR", manifestLoader.normalizeLanguage("pt_BR"))
self.assertEqual("pt_BR", manifestLoader.normalizeLanguage("pt-BR"))
18 changes: 17 additions & 1 deletion _validate/manifestLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ def getAddonManifestLocalizations(
languageCode = pathlib.Path(translationFile).parent.name
try:
translatedManifest = AddonManifest(translationFile)
yield languageCode, translatedManifest
yield normalizeLanguage(languageCode), translatedManifest
except Exception:
print(f"Error in {translationFile}")


def normalizeLanguage(lang: str) -> str:
"""
Normalizes a language-dialect string into a standard form we can deal with.
Converts any dash to underline, and makes sure that language is lowercase and dialect is uppercase.
Based on NVDA`s `languageHandler` module.
:param lang: A language code.
:return: A normalized language code.
"""
lang = lang.replace("-", "_")
ld = lang.split("_")
ld[0] = ld[0].lower()
if len(ld) >= 2:
ld[1] = ld[1].upper()
return "_".join(ld)