зеркало из https://github.com/mozilla/bedrock.git
Allow views to specify multiple ftl or template files for lang activation list
Re #9251
This commit is contained in:
Родитель
0a54283890
Коммит
70b37047a6
|
@ -663,7 +663,11 @@ class DownloadThanksView(L10nTemplateView):
|
|||
'firefox/new/trailhead/exp-thanks.html': ['firefox/new/download'],
|
||||
'firefox/new/desktop/thanks.html': ['firefox/new/desktop'],
|
||||
}
|
||||
ftl_activations = ['firefox/new/download', 'firefox/new/desktop']
|
||||
activation_files = [
|
||||
'firefox/new/download',
|
||||
'firefox/new/desktop',
|
||||
'firefox/new/protocol/thanks.html',
|
||||
]
|
||||
|
||||
# place expected ?v= values in this list
|
||||
variations = ['a', 'b', 'c']
|
||||
|
@ -706,7 +710,11 @@ class NewView(L10nTemplateView):
|
|||
'firefox/new/trailhead/download-yandex.html': ['firefox/new/download', 'banners/firefox-mobile'],
|
||||
'firefox/new/desktop/download.html': ['firefox/new/desktop'],
|
||||
}
|
||||
ftl_activations = ['firefox/new/download', 'firefox/new/desktop']
|
||||
activation_files = [
|
||||
'firefox/new/download',
|
||||
'firefox/new/desktop',
|
||||
'firefox/new/protocol/download.html',
|
||||
]
|
||||
|
||||
# place expected ?v= values in this list
|
||||
variations = ['a', 'b']
|
||||
|
@ -776,6 +784,7 @@ class FirefoxHomeView(L10nTemplateView):
|
|||
ftl_files_map = {
|
||||
'firefox/home/index-master.html': ['firefox/home']
|
||||
}
|
||||
activation_files = ['firefox/home', 'firefox/home/index-quantum.html']
|
||||
|
||||
def get_template_names(self):
|
||||
if ftl_file_is_active('firefox/home'):
|
||||
|
|
|
@ -352,9 +352,10 @@ FTL files for that template (or a single file name if that's all you need).
|
|||
|
||||
If you need for your URL to use multiple Fluent files to determine the full list of active locales,
|
||||
for example when you are redesigning a page and have multiple templates in use for a single URL depending
|
||||
on locale, you can use the `ftl_activations` parameter. This should be a list of FTL filenames that should
|
||||
all be used when determining the full list of translations for the URL. Bedrock will gather the full list
|
||||
for each file and combine them into a single list so that the footer language switcher works properly.
|
||||
on locale, you can use the `activation_files` parameter. This should be a list of FTL filenames
|
||||
(or template file names if they are still using .lang) that should all be used when determining the full
|
||||
list of translations for the URL. Bedrock will gather the full list for each file and combine them into a
|
||||
single list so that the footer language switcher works properly.
|
||||
|
||||
Using in a view function
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
|
|
@ -32,7 +32,12 @@ def template_source_url(template):
|
|||
return '%s/tree/master/%s' % (settings.GITHUB_REPO, relative_path)
|
||||
|
||||
|
||||
def render(request, template, context=None, ftl_files=None, ftl_activations=None, **kwargs):
|
||||
def render(request,
|
||||
template,
|
||||
context=None,
|
||||
ftl_files=None,
|
||||
activation_files=None,
|
||||
**kwargs):
|
||||
"""
|
||||
Same as django's render() shortcut, but with l10n template support.
|
||||
If used like this::
|
||||
|
@ -85,11 +90,18 @@ def render(request, template, context=None, ftl_files=None, ftl_activations=None
|
|||
translations = context['active_locales']
|
||||
del context['active_locales']
|
||||
else:
|
||||
if l10n:
|
||||
if ftl_activations:
|
||||
translations = ftl_active_locales(ftl_activations)
|
||||
else:
|
||||
translations = l10n.active_locales
|
||||
if activation_files:
|
||||
translations = set()
|
||||
for af in activation_files:
|
||||
if af.endswith('.html'):
|
||||
translations.update(translations_for_template(af))
|
||||
else:
|
||||
translations.update(ftl_active_locales(af))
|
||||
|
||||
translations = sorted(translations)
|
||||
|
||||
elif l10n:
|
||||
translations = l10n.active_locales
|
||||
else:
|
||||
translations = translations_for_template(template)
|
||||
|
||||
|
@ -196,9 +208,9 @@ class LangFilesMixin:
|
|||
ftl_files = None
|
||||
# a dict of template names to ftl files
|
||||
ftl_files_map = None
|
||||
# a list of ftl files to use to determine the full list of active locales
|
||||
# a list of ftl or template files to use to determine the full list of active locales
|
||||
# mostly useful during a redesign where multiple templates are used for a single URL
|
||||
ftl_activations = None
|
||||
activation_files = None
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
ctx = super().get_context_data(**kwargs)
|
||||
|
@ -222,7 +234,7 @@ class LangFilesMixin:
|
|||
template_names = self.get_template_names()
|
||||
return render(self.request, template_names, context,
|
||||
ftl_files=self.get_ftl_files(template_names),
|
||||
ftl_activations=self.ftl_activations, **response_kwargs)
|
||||
activation_files=self.activation_files, **response_kwargs)
|
||||
|
||||
|
||||
class L10nTemplateView(LangFilesMixin, TemplateView):
|
||||
|
|
|
@ -5,7 +5,7 @@ from django.test.client import RequestFactory
|
|||
from django.test.utils import override_settings
|
||||
|
||||
from django_jinja.backend import Jinja2
|
||||
from mock import ANY, patch
|
||||
from mock import ANY, patch, call
|
||||
|
||||
from lib import l10n_utils
|
||||
|
||||
|
@ -104,6 +104,22 @@ class TestRender(TestCase):
|
|||
l10n_utils.render(req, template, ftl_files=ftl_files)
|
||||
assert ftl_files == ['dude', 'walter']
|
||||
|
||||
@patch.object(l10n_utils, 'django_render')
|
||||
@patch.object(l10n_utils, 'translations_for_template')
|
||||
@patch.object(l10n_utils, 'ftl_active_locales')
|
||||
def test_activation_files(self, fal_mock, tft_mock, dr_mock):
|
||||
ftl_files = ['dude', 'walter']
|
||||
path = '/firefox/new/'
|
||||
template = 'firefox/new.html'
|
||||
activation_files = ftl_files + [template]
|
||||
req = RequestFactory().get(path)
|
||||
l10n_utils.render(req, template, activation_files=activation_files)
|
||||
fal_mock.assert_has_calls([
|
||||
call('dude'),
|
||||
call('walter'),
|
||||
], any_order=True)
|
||||
tft_mock.assert_called_with(template)
|
||||
|
||||
|
||||
class TestGetAcceptLanguages(TestCase):
|
||||
def _test(self, accept_lang, list):
|
||||
|
@ -140,23 +156,23 @@ class TestL10nTemplateView(TestCase):
|
|||
view = l10n_utils.L10nTemplateView.as_view(template_name='dude.html',
|
||||
ftl_files='dude')
|
||||
view(self.req)
|
||||
render_mock.assert_called_with(self.req, ['dude.html'], ANY, ftl_files='dude', ftl_activations=None)
|
||||
render_mock.assert_called_with(self.req, ['dude.html'], ANY, ftl_files='dude', activation_files=None)
|
||||
|
||||
def test_ftl_files_map(self, render_mock):
|
||||
view = l10n_utils.L10nTemplateView.as_view(template_name='dude.html',
|
||||
ftl_files_map={'dude.html': 'dude'})
|
||||
view(self.req)
|
||||
render_mock.assert_called_with(self.req, ['dude.html'], ANY, ftl_files='dude', ftl_activations=None)
|
||||
render_mock.assert_called_with(self.req, ['dude.html'], ANY, ftl_files='dude', activation_files=None)
|
||||
# no match means no FTL files
|
||||
view = l10n_utils.L10nTemplateView.as_view(template_name='dude.html',
|
||||
ftl_files_map={'donny.html': 'donny'})
|
||||
view(self.req)
|
||||
render_mock.assert_called_with(self.req, ['dude.html'], ANY, ftl_files=None, ftl_activations=None)
|
||||
render_mock.assert_called_with(self.req, ['dude.html'], ANY, ftl_files=None, activation_files=None)
|
||||
|
||||
def test_ftl_activations(self, render_mock):
|
||||
view = l10n_utils.L10nTemplateView.as_view(template_name='dude.html',
|
||||
ftl_files='dude',
|
||||
ftl_activations=['dude', 'donny'])
|
||||
activation_files=['dude', 'donny'])
|
||||
view(self.req)
|
||||
render_mock.assert_called_with(self.req, ['dude.html'], ANY, ftl_files='dude',
|
||||
ftl_activations=['dude', 'donny'])
|
||||
activation_files=['dude', 'donny'])
|
||||
|
|
Загрузка…
Ссылка в новой задаче