def test_must_be_subset_of_django_languages(self): with self.assertRaises(ImproperlyConfigured) as e: get_content_languages() self.assertEqual( e.exception.args, ("The language zh is specified in WAGTAIL_CONTENT_LANGUAGES but not LANGUAGES. WAGTAIL_CONTENT_LANGUAGES must be a subset of LANGUAGES.", ), )
class LocaleForm(forms.ModelForm): required_css_class = "required" language_code = forms.ChoiceField(label=_("Language"), choices=get_content_languages().items()) def __init__(self, *args, **kwargs): instance = kwargs.get("instance") super().__init__(*args, **kwargs) # Get language codes that are already used used_language_codes = Locale.objects.values_list("language_code", flat=True) self.fields["language_code"].choices = [ (language_code, display_name) for language_code, display_name in get_content_languages().items() if language_code not in used_language_codes or ( instance and instance.language_code == language_code) ] # If the existing language code is invalid, add an empty value so Django doesn't automatically select a random language if instance and not instance.language_code_is_valid(): self.fields["language_code"].choices.insert( 0, (None, _("Select a new language"))) class Meta: model = Locale fields = ["language_code"]
def test_can_be_a_translation_proxy(self): self.assertEqual( get_content_languages(), { "de": "German", "en": "English", }, )
def test_can_be_different_to_django_languages(self): self.assertEqual( get_content_languages(), { "de": "German", "en": "English", }, )
def test_get_content_languages(self): self.assertEqual( get_content_languages(), { "de": "German", "de-at": "Austrian German", "en": "English", "pt-br": "Portuguese (Brazil)", }, )
def format_message(self, log_entry): try: return _( "Copied for translation from %(title)s (%(locale)s)") % { "title": log_entry.data["source"]["title"], "locale": get_content_languages().get( log_entry.data["source_locale"]["language_code"]) or "", } except KeyError: return _("Copied for translation")
def purge_urls_from_cache(urls, backend_settings=None, backends=None): # Convert each url to urls one for each managed language (WAGTAILFRONTENDCACHE_LANGUAGES setting). # The managed languages are common to all the defined backends. # This depends on settings.USE_I18N # If WAGTAIL_I18N_ENABLED is True, this defaults to WAGTAIL_CONTENT_LANGUAGES wagtail_i18n_enabled = getattr(settings, "WAGTAIL_I18N_ENABLED", False) content_languages = get_content_languages() if wagtail_i18n_enabled else {} languages = getattr(settings, "WAGTAILFRONTENDCACHE_LANGUAGES", list(content_languages.keys())) if settings.USE_I18N and languages: langs_regex = "^/(%s)/" % "|".join(languages) new_urls = [] # Purge the given url for each managed language for isocode in languages: for url in urls: up = urlparse(url) new_url = urlunparse(( up.scheme, up.netloc, re.sub(langs_regex, "/%s/" % isocode, up.path), up.params, up.query, up.fragment, )) # Check for best performance. True if re.sub found no match # It happens when i18n_patterns was not used in urls.py to serve content for different languages from different URLs if new_url in new_urls: continue new_urls.append(new_url) urls = new_urls for backend_name, backend in get_backends(backend_settings, backends).items(): for url in urls: logger.info("[%s] Purging URL: %s", backend_name, url) backend.purge_batch(urls)
def get_display_name(self): return get_content_languages().get(self.language_code)
def language_code_is_valid(self): return self.language_code in get_content_languages()