Skip to content

Commit 75e7a0c

Browse files
authored
Add normalize language function and tests (#41)
Fixes nvaccess/nvda#17527 Summary of the issue In the add-on store, sometimes translated add-ons appear as untranslated. This can be produced when an add-on has a locale folder with a non normalized language, like ES instead of es. In the transformation to build the views Branch using addon-datastore-transform repo, a function is used to get the available languages. Available languages are stored in a set. Moreover, when a folder is created to store json files for each available language, uppercase and lowercase folders with the same name aren't created. Consequently, if a non standard language is available, but in fact add-ons aren't translated to this language, the english translation can be copied in the folder corresponding to the standard language, even overriding the available language for the standard language. This can be observed for "es" (spanish). Development approach A function has been created to normalize language, and this is used in the function to get manifest localizations. Tests have been added just for this function.
1 parent 354918f commit 75e7a0c

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

_tests/test_createJson.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
import json
1111
from _validate import (
1212
createJson,
13-
addonManifest
13+
addonManifest,
14+
manifestLoader
1415
)
1516

1617
TOP_DIR = os.path.abspath(os.path.dirname(__file__))
@@ -110,3 +111,19 @@ def test_validVersion(self):
110111
"1.2.0.json",
111112
msg="Name of the output file should be named based on version number"
112113
)
114+
115+
116+
class Test_normalizeLanguage(unittest.TestCase):
117+
"""Set of unit tests for `manifestLoader.normalizeLanguage`."""
118+
119+
def test_normalization_no_country_info(self):
120+
"""Makes sure that if no country info is provided language is normalized to lower case."""
121+
self.assertEqual("en", manifestLoader.normalizeLanguage("en"))
122+
self.assertEqual("en", manifestLoader.normalizeLanguage("EN"))
123+
self.assertEqual("kmr", manifestLoader.normalizeLanguage("kmr"))
124+
125+
def test_underscore_used_as_separator_after_normalization(self):
126+
"""Ensures that underscore is used to separate country info from language.
127+
Also implicitly test the fact that country code is converted to upper case."""
128+
self.assertEqual("pt_BR", manifestLoader.normalizeLanguage("pt_BR"))
129+
self.assertEqual("pt_BR", manifestLoader.normalizeLanguage("pt-BR"))

_validate/manifestLoader.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ def getAddonManifestLocalizations(
4747
languageCode = pathlib.Path(translationFile).parent.name
4848
try:
4949
translatedManifest = AddonManifest(translationFile)
50-
yield languageCode, translatedManifest
50+
yield normalizeLanguage(languageCode), translatedManifest
5151
except Exception:
5252
print(f"Error in {translationFile}")
53+
54+
55+
def normalizeLanguage(lang: str) -> str:
56+
"""
57+
Normalizes a language-dialect string into a standard form we can deal with.
58+
Converts any dash to underline, and makes sure that language is lowercase and dialect is uppercase.
59+
Based on NVDA`s `languageHandler` module.
60+
:param lang: A language code.
61+
:return: A normalized language code.
62+
"""
63+
lang = lang.replace("-", "_")
64+
ld = lang.split("_")
65+
ld[0] = ld[0].lower()
66+
if len(ld) >= 2:
67+
ld[1] = ld[1].upper()
68+
return "_".join(ld)

0 commit comments

Comments
 (0)