diff --git a/README.rst b/README.rst index 63b4d34..5b5bf28 100644 --- a/README.rst +++ b/README.rst @@ -55,6 +55,21 @@ above: Templates are stored in a file called `templates.yaml` located in the application root. Checkout the `Tidepooler example `_ 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 =============== diff --git a/flask_ask/core.py b/flask_ask/core.py index 987659e..1065b1f 100644 --- a/flask_ask/core.py +++ b/flask_ask/core.py @@ -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()) @@ -875,16 +875,17 @@ 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): @@ -892,7 +893,19 @@ def get_source(self, environment, template): 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)