Merge pull request #42 from diox/update-and-refactor-supported-locales

Update supported locales, error if default_locale is invalid (bug 109237)
This commit is contained in:
Mathieu Pillard 2014-11-03 16:45:43 +01:00
Родитель 818a53a068 adb66ac75f
Коммит d5434ce9f8
4 изменённых файлов: 107 добавлений и 57 удалений

Просмотреть файл

@ -64,22 +64,20 @@ PRERELEASE_PERMISSIONS = PERMISSIONS['prerelease']
PRIVILEGED_PERMISSIONS = ALL_PERMISSIONS - PERMISSIONS['certified']
WEB_PERMISSIONS = PERMISSIONS['web']
SHORT_LOCALES = {
'en': 'en-US', 'ga': 'ga-IE', 'pt': 'pt-PT', 'sv': 'sv-SE', 'zh': 'zh-CN'
}
# Taken from Fireplace's hearth/media/js/l10n.js
SUPPORTED_LOCALES = [
'de', 'en-US', 'es', 'fr', 'pl', 'pt-BR',
# List of languages from AMO's settings (excluding mkt's active locales).
'af', 'ar', 'bg', 'ca', 'cs', 'da', 'el', 'eu', 'fa',
'fi', 'ga-IE', 'he', 'hu', 'id', 'it', 'ja', 'ko', 'mn', 'nl',
'pt-PT', 'ro', 'ru', 'sk', 'sl', 'sq', 'sv-SE', 'uk', 'vi',
'zh-CN', 'zh-TW',
# The hidden list from AMO's settings:
'cy', 'sr', 'tr',
]
# Those 3 *_LANGUAGES settings are taken from zamboni's mkt/settings.py
SUPPORTED_LANGUAGES = (
'bg', 'bn-BD', 'ca', 'cs', 'da', 'de', 'el', 'en-US', 'es', 'eu', 'fr',
'ga-IE', 'hr', 'hu', 'it', 'ja', 'ko', 'mk', 'nb-NO', 'nl', 'pa', 'pl',
'pt-BR', 'ro', 'ru', 'sk', 'sq', 'sr', 'sr-Latn', 'ta', 'tr', 'xh',
'zh-CN', 'zh-TW', 'zu',
)
HIDDEN_LANGUAGES = (
'af', 'ar', 'fa', 'fi', 'he', 'id', 'mn', 'pt-PT', 'sl', 'sv-SE',
'uk', 'vi',
'cy',
)
SHORTER_LANGUAGES = {'en': 'en-US', 'ga': 'ga-IE', 'pt': 'pt-PT',
'sv': 'sv-SE', 'zh': 'zh-CN'}
# Graciously provided by @kumar in bug 614574
if (not SPIDERMONKEY_INSTALLATION or

Просмотреть файл

@ -1,9 +1,15 @@
from . import register_test
from appvalidator.constants import SHORT_LOCALES, SUPPORTED_LOCALES
from appvalidator.constants import (HIDDEN_LANGUAGES, SHORTER_LANGUAGES,
SUPPORTED_LANGUAGES)
# The validator accepts every language supported by Marketplace, even hidden
# ones.
ALL_SUPPORTED_LANGUAGES = set(SUPPORTED_LANGUAGES + HIDDEN_LANGUAGES)
def canonicalize(locale):
# Format the locale properly.
"""Reformat locale with wrong capitalization"""
if "-" in locale:
language, region = locale.split('-', 1)
else:
@ -14,14 +20,14 @@ def canonicalize(locale):
region = region.upper()
locale = '%s-%s' % (language, region)
if locale in SUPPORTED_LOCALES:
if locale in ALL_SUPPORTED_LANGUAGES:
return locale
if language in SUPPORTED_LOCALES:
if language in ALL_SUPPORTED_LANGUAGES:
return language
if language in SHORT_LOCALES:
return SHORT_LOCALES[language]
if language in SHORTER_LANGUAGES:
return SHORTER_LANGUAGES[language]
return locale
@ -50,7 +56,21 @@ def validate_locales(err, package=None):
locales = set()
if "default_locale" in manifest:
locales.add(manifest["default_locale"])
default_locale = manifest["default_locale"]
locales.add(default_locale)
# Since default_locale will be used by the Marketplace to decide which
# locale the name/description from the manifest are saved in, it's
# crucial for it to be valid - raise an error if it's an invalid or
# unsupported locale, not just a warning.
if canonicalize(default_locale) not in ALL_SUPPORTED_LANGUAGES:
err.error(
err_id=("default_locale", "not_supported"),
error="Unsupported default_locale provided.",
description=["The default_locale provided in the manifest is "
"not supported by the Firefox Marketplace. If a "
"default_locale is provided, it must be be a "
"supported one.",
"Provided defaut_locale: %s" % default_locale])
if "locales" in manifest:
for locale in manifest["locales"]:
@ -61,7 +81,7 @@ def validate_locales(err, package=None):
if not locales:
return
if not any(canonicalize(loc) in SUPPORTED_LOCALES for loc in locales):
if not any(canonicalize(l) in ALL_SUPPORTED_LANGUAGES for l in locales):
err.error(
err_id=("locales", "none_supported"),
error="No supported locales provided.",
@ -72,11 +92,11 @@ def validate_locales(err, package=None):
return
for locale in locales:
if canonicalize(locale) not in SUPPORTED_LOCALES:
if canonicalize(locale) not in ALL_SUPPORTED_LANGUAGES:
err.warning(
err_id=("locales", "not_supported"),
warning="Unsupported locale provided.",
description=["An locale which is not supported by the Firefox "
description=["A locale which is not supported by the Firefox "
"Marketplace was specified in the manifest. The "
"information listed in this locale will not be "
"stored or displayed to users.",

Двоичные данные
tests/resources/packaged_app.zip

Двоичный файл не отображается.

Просмотреть файл

@ -1,22 +1,18 @@
from nose.tools import eq_
import appvalidator.testcases.locales as locales
from appvalidator.errorbundle import ErrorBundle
from helper import TestCase
def test_canonicalize():
def test(locale, expected_locale):
eq_(locales.canonicalize(locale), expected_locale)
yield test, "en-US", "en-US"
yield test, "EN-us", "en-US"
yield test, "EN", "en-US"
yield test, "en", "en-US"
eq_(locales.canonicalize("en-US"), "en-US")
eq_(locales.canonicalize("EN-us"), "en-US")
eq_(locales.canonicalize("EN"), "en-US")
eq_(locales.canonicalize("en"), "en-US")
# pt-BR is a supported locale, so keep it at that.
yield test, "pt-BR", "pt-BR"
yield test, "pt-PT", "pt-PT"
yield test, "pt-FOO", "pt-PT"
eq_(locales.canonicalize("pt-BR"), "pt-BR")
eq_(locales.canonicalize("pt-PT"), "pt-PT")
eq_(locales.canonicalize("pt-FOO"), "pt-PT")
class TestLocales(TestCase):
@ -24,10 +20,10 @@ class TestLocales(TestCase):
def setUp(self):
self.setup_err()
self.manifest = {
"default_locale": "en-US",
"locales": {},
'default_locale': 'en-US',
'locales': {},
}
self.err.save_resource("manifest", self.manifest)
self.err.save_resource('manifest', self.manifest)
def run(self):
locales.validate_locales(self.err, None)
@ -37,44 +33,80 @@ class TestLocales(TestCase):
self.assert_silent()
def test_passes_no_default(self):
del self.manifest["default_locale"]
del self.manifest['default_locale']
self.run()
self.assert_silent()
def test_passes_locales(self):
self.manifest["locales"]["pt-BR"] = {}
self.manifest['locales']['pt-BR'] = {}
self.run()
self.assert_silent()
def test_passes_default_locale(self):
self.manifest['default_locale'] = 'pt-BR'
self.run()
self.assert_silent()
def test_passes_default_locale_hidden(self):
self.manifest['default_locale'] = 'af'
self.run()
self.assert_silent()
def test_passes_default_locale_shorter(self):
self.manifest['default_locale'] = 'en'
self.run()
self.assert_silent()
def test_warns_locales(self):
self.manifest["locales"]["foo"] = {}
self.manifest['locales']['foo'] = {}
self.run()
# Only a warning since the rest of the content is fine.
self.assert_failed(with_warnings=True)
def test_warns_bad_default_locale(self):
self.manifest["default_locale"] = "foobar"
self.manifest["locales"]["pt-BR"] = {}
def test_bad_default_locale(self):
self.manifest['default_locale'] = 'foobar'
self.manifest['locales']['pt-BR'] = {}
self.run()
self.assert_failed(with_warnings=True)
# We have an invalid default_locale. Even though we have a valid
# 'locales', we should return an error.
self.assert_failed(with_errors=True)
def test_default_locale_invalid(self):
self.manifest["default_locale"] = "asdf"
self.manifest['default_locale'] = 'asdf'
self.run()
# Invalid default_locale, we should return an error.
self.assert_failed(with_errors=True)
def test_locales_locale_invalid(self):
self.manifest["default_locale"] = "asdf"
self.manifest["locales"]["foo"] = {}
def test_default_locale_should_be_unsupported_but_language_is(self):
self.manifest['default_locale'] = 'fr-BR'
self.run()
# Valid because the language part is good.
self.assert_silent()
def test_default_locale_invalid_with_locales_valid(self):
self.manifest['default_locale'] = 'en_US' # Should use '-', not '_'.
self.manifest['locales']['pt-BR'] = {}
self.run()
# We have an invalid default_locale. Even though we have a valid
# 'locales', we should return an error.
self.assert_failed(with_errors=True)
def test_warngs_invalid_default(self):
self.manifest["default_locale"] = "en_US"
self.manifest["locales"]["pt-BR"] = {}
def test_default_locale_and_locales_invalid(self):
self.manifest['default_locale'] = 'asdf'
self.manifest['locales']['foo'] = {}
self.run()
self.assert_failed(with_warnings=True)
# Both are invalid, so we should return an error.
self.assert_failed(with_errors=True)
def test_warngs_invalid_locales(self):
self.manifest["locales"]["pt_BR"] = {}
def test_default_locale_wrong_format(self):
self.manifest['default_locale'] = 'pt_BR'
self.run()
# Invalid, should use '-', not '_'.
self.assert_failed(with_errors=True)
def test_warns_invalid_locales(self):
self.manifest['locales']['pt_BR'] = {}
self.run()
# Invalid, should use '-', not '_', but since it's just one of the
# locales and not the default one, a warning is enough.
self.assert_failed(with_warnings=True)