Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locale support : Now template.yaml supports localization #239

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ above:

Templates are stored in a file called `templates.yaml` located in the application root. Checkout the `Tidepooler example <https://github.com/johnwheeler/flask-ask/tree/master/samples/tidepooler>`_ to see why it makes sense to extract speech out of the code and into templates as the number of spoken phrases grow.

Multi-Language Skills:
---------------------

If you write a multi-language skill, you can set many localizations of the messages
adding the locale as suffix to the message in templates UAML file, for example :

.. code-block:: yaml

hello_en_US: Beautiful appartement {{ firstname }} !
hello_en_UK: Beautiful flat {{firstname}} !
hello_fr: Bel appartement {{ firstname }} !
hello: Beautiful appartement or flat {{firstname}} !

Notice that if the right localization is not found, the unlocalized version is used.

Features
===============

Expand Down
23 changes: 18 additions & 5 deletions flask_ask/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ def _flask_view_func(self, *args, **kwargs):
ask_payload = self._alexa_request(verify=self.ask_verify_requests)
dbgdump(ask_payload)
request_body = models._Field(ask_payload)

self.request = request_body.request
self.version = request_body.version
self.context = getattr(request_body, 'context', models._Field())
Expand Down Expand Up @@ -875,24 +875,37 @@ def _map_params_to_view_args(self, view_name, arg_names):


class YamlLoader(BaseLoader):

def __init__(self, app, path):
self.path = app.root_path + os.path.sep + path
self.mapping = {}
self._reload_mapping()



def _reload_mapping(self):
if os.path.isfile(self.path):
self.last_mtime = os.path.getmtime(self.path)
with open(self.path) as f:
with open(self.path, encoding="utf-8") as f:
self.mapping = yaml.safe_load(f.read())

def get_source(self, environment, template):
if not os.path.isfile(self.path):
return None, None, None
if self.last_mtime != os.path.getmtime(self.path):
self._reload_mapping()
if template in self.mapping:

locale = getattr(_app_ctx_stack.top, '_ask_request').locale
source = None
for sfx in (locale.replace('-','_'), locale.split('-',1)[0]):
key = template+"_"+sfx
if key in self.mapping:
source = self.mapping[key]
break
if not source:
logger.warn("No localized template found for locale %r and template %r, falling back to default.", locale, template)
source = self.mapping[template]

if source:
return source, None, lambda: source == self.mapping.get(template)

raise TemplateNotFound(template)