Closed Bug 1222137 Opened 9 years ago Closed 8 years ago

Support easy locale-based template overrides

Categories

(www.mozilla.org :: Bedrock, defect)

Production
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: pmac, Assigned: jpetto)

Details

(Whiteboard: [kb=1959666] )

I think a system like the following would be useful, but let's discuss it:

Pages often need various text or features to be different on a per-locale basis. This can currently be done with l10n blocks[0] or other conditionals in the template. But often more than that is needed so we do some custom work in the view to load in different templates depending on the locale. This could be done for every page based on the existence of templates. It would be very much like the l10n block method, but the overriding templates would live in bedrock instead of the l10n repo. I envision it working like this:

Given a template (bedrock/firefox/templates/firefox/new.html): you'd like to override bits of it only for en-US.

1. Create a new template file: bedrock/firefox/templates/firefox/new.en-US.html
2. First line of this new file is "{% extends 'firefox/new.html' %}".
3. Override whatever you want and this file will be the template for this page when viewed in the en-US locale.

Note: I'm keeping the locale-specific template in the same dir with the default template so that it will be obvious this method is getting out of hand. It'll also help to eliminate confusion when trying to debug since it'll be more obvious such a file exists than if it were hidden in a l10n specific directory hierarchy for such files.

Hopefully such a system will help us more easily keep up with such requests, as well as eliminate the need for some of the more confusing conditional work we've had to do in templates recently.

[0] http://bedrock.readthedocs.org/en/latest/l10n.html#l10n-blocks
Opinions?
This is a great starting point.

A couple thoughts:

- We may have to add more {% block %}s to the extended template, but that's not really a big deal.

- It seems like this should be opt-in functionality. If so, this would mean each URL requiring such logic would need its own view, right? Not a big deal here either - just getting an idea of how the system would work.

- One potentially tricky aspect would be asset management. In some cases, we would need to customize the CSS and JS just enough to justify a unique (yet very similar) file for the variant, which may present a maintenance/DRY problem. Not sure how big the actual impact would be though. It's likely we'll only have 1 or 2 variants per URL, which should keep asset code duplication limited and manageable. We could employ the same naming and location strategy for assets - new.en-US.less and new.en-US.js right along side the default assets.
(In reply to Jon Petto [:jpetto] from comment #2)
> - We may have to add more {% block %}s to the extended template, but that's
> not really a big deal.

Yeah. I think this will be on a case-by-case basis. The point is that this will allow for the template to be swapped in very easily.
 
> - It seems like this should be opt-in functionality. If so, this would mean
> each URL requiring such logic would need its own view, right? Not a big deal
> here either - just getting an idea of how the system would work.

That's the case now. What I'm suggesting is that changes would be made to our l10n_utils.render function to look for these templates by default and use them if they exist (much like it currently does for l10n blocks). So no, it could just be a page() helper in urls.py but if you create "default-template-name.de.html" then it would be used for the German locale.

> - One potentially tricky aspect would be asset management. In some cases, we
> would need to customize the CSS and JS just enough to justify a unique (yet
> very similar) file for the variant, which may present a maintenance/DRY
> problem. Not sure how big the actual impact would be though. It's likely
> we'll only have 1 or 2 variants per URL, which should keep asset code
> duplication limited and manageable. We could employ the same naming and
> location strategy for assets - new.en-US.less and new.en-US.js right along
> side the default assets.

That seems good. I don't think we need any extra code to support that side since the new template can do anything you need, but conventions will definitely help us keep it under control.
(In reply to Paul [:pmac] McLanahan from comment #3)
> (In reply to Jon Petto [:jpetto] from comment #2)  
> > - It seems like this should be opt-in functionality. If so, this would mean
> > each URL requiring such logic would need its own view, right? Not a big deal
> > here either - just getting an idea of how the system would work.
> 
> That's the case now. What I'm suggesting is that changes would be made to
> our l10n_utils.render function to look for these templates by default and
> use them if they exist (much like it currently does for l10n blocks). So no,
> it could just be a page() helper in urls.py but if you create
> "default-template-name.de.html" then it would be used for the German locale.

No arguments from me on this point. As long as we add the new magic to the docs, should be all good.
I have no useful suggestions for implementation, but I would love to have a feature like this for the site.  We would definitely use it.
Whiteboard: [kb=1894216]
Assignee: nobody → jon
Whiteboard: [kb=1894216] → [kb=1959666]
Commits pushed to master at https://github.com/mozilla/bedrock

https://github.com/mozilla/bedrock/commit/62f0107fa45a90aac5925c90315d36ea44774d43
[fix bug 1222137] Enable bedrock locale-based templates.

https://github.com/mozilla/bedrock/commit/93dd85e41f488b0dc765f75123379c9f3bc17d9f
Merge pull request #3880 from jpetto/bug-1222137-locale-based-templates

[fix bug 1222137] Enable bedrock locale-based templates.
Status: NEW → RESOLVED
Closed: 8 years ago
Resolution: --- → FIXED
Commits pushed to master at https://github.com/mozilla/bedrock

https://github.com/mozilla/bedrock/commit/ec849bbe30898127c38349f373b50b11a4bf81cf
[fix bug 1222137] Add docs for locale templates.

https://github.com/mozilla/bedrock/commit/f9ae034781d6dd9e8cc007320f3696316f837204
Merge pull request #3896 from jpetto/bug-1222137-locale-template-docs

[fix bug 1222137] Add docs for locale templates.
You need to log in before you can comment on or make changes to this bug.