Internationalization: Backend support to localization based on user browser

User experience
===============
For users it means we can provide localized web-sites to enrich their
overall experiences.

Although for the Blender Cloud this doesn't make much sense (since the
content is in English), Flamenco and Attract can really benefit from
this.

New configuration settings
==========================
There are two new parameters in config.py:

* DEFAULT_LOCALE='en_US'
* SUPPORT_ENGLISH=True

They are both properly documented in the `config.py` file.

Technicall details
==================
We are using the 'Accept-Languages' header to match the
available translations with the user supported languages.

If an extension has a `translations` folder, it's used for translations.
However the main application (e.g., Blender Cloud) is the one that
determines the supported languages based on its `languages` folder.

How to mark strings for translation
===================================
See the documentation in README.md.

But as an example, 404.pug and pillar/__init__.py::handle_sdk_resource_invalid
have marked up strings that will be extracted once you install pillar,
or run any of the translations commangs.

Remember to **gulp** after you update the template files.

How to setup translations
=========================
You will need to create translation for the main project, and for each
extension that you want to see translated. I added a new entry-point to
the installation of Pillar.

So all you need is to use the `translations`
script to initialize, update and compile your translations.

Pending tasks
=============
Aside from marking more strings for extraction and start the translation
effort it would be interesting to replace the pretty_date routine with
momentjs.

Acknowledgement
===============
Many thanks for Sybren Stüvel for the suggestions and throughout code
review. Thanks also to Francesco Siddi for the original documentation
and suggesting me to tackle this. And Kudos for Pablo Vazquez for the
motivational support and for the upcoming "strings mark up" task force!

The core of the implementation is based on Miguel Grinberg i18n chapter
of his great 'The Mega Flask Tutorial'.

Reviewers: sybren

Differential Revision: https://developer.blender.org/D2826
This commit is contained in:
Dalai Felinto
2017-09-09 00:02:24 +02:00
parent b769cc6c3c
commit 303a33c3bf
11 changed files with 305 additions and 8 deletions

View File

@@ -16,6 +16,8 @@ can then be registered to the application at app creation time:
"""
import abc
import inspect
import pathlib
import typing
import flask
@@ -112,6 +114,23 @@ class PillarExtension(object, metaclass=abc.ABCMeta):
"""
return None
@property
def translations_path(self) -> typing.Union[pathlib.Path, None]:
"""Returns the path where the translations for this extension are stored.
This is top folder that contains a "translations" sub-folder
May return None, in which case English will always be used for this extension.
"""
class_filename = pathlib.Path(inspect.getfile(self.__class__))
# Pillar extensions instantiate the PillarExtension from a sub-folder in
# the main project (e.g. //blender_cloud/blender_cloud/__init__.py), but
# the translations folders is in the main project folder.
translations_path = class_filename.parents[1] / 'translations'
return translations_path if translations_path.is_dir() else None
def setup_app(self, app):
"""Called during app startup, after all extensions have loaded."""