Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Issue 2120 - Fallback to 'en-us' when resource loading fails #2392

Closed
wants to merge 4 commits into from
Closed
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
23 changes: 22 additions & 1 deletion mycroft/dialog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,13 @@ class DialogLoader:
def __init__(self, renderer_factory=MustacheDialogRenderer):
self.__renderer = renderer_factory()

def load(self, dialog_dir):
def load(self, dialog_dir, lang=None):
"""
Load all dialog files within the specified directory.

Args:
dialog_dir (str): directory that contains dialog files
lang (str): language string (optional)

Returns:
a loaded instance of a dialog renderer
Expand All @@ -147,12 +148,32 @@ def load(self, dialog_dir):
LOG.warning("No dialog files found: {}".format(dialog_dir))
return self.__renderer

if not lang:
from mycroft.configuration import Configuration
lang = Configuration.get().get("lang")
loaded_dialogs = []

for path, _, files in os.walk(str(directory)):
for f in files:
if f.endswith(".dialog"):
if lang != 'en-us':
# remember all loaded dialogs
loaded_dialogs.append(f)
self.__renderer.load_template_file(
f.replace('.dialog', ''),
join(path, f))

if lang != 'en-us':
en_us_dir = dialog_dir.replace(lang, 'en-us')
for path, _, files in os.walk(str(en_us_dir)):
for f in files:
if f.endswith(".dialog") and f not in loaded_dialogs:
self.__renderer.load_template_file(
f.replace('.dialog', ''),
join(path, f))
LOG.warning("Dialog '{}' for lang '{}' not found: "
"using 'en-us'".format(f, lang))

return self.__renderer


Expand Down
17 changes: 15 additions & 2 deletions mycroft/skills/mycroft_skill/mycroft_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,9 +675,22 @@ def find_resource(self, res_name, res_dirname=None):
Returns:
string: The full path to the resource file or None if not found
"""
result = self._find_resource(res_name, self.lang, res_dirname)
if not result and self.lang != 'en-us':
# when resource not found try fallback to en-us
LOG.warning(
"Resource '{}' for lang '{}' not found: trying 'en-us'"
.format(res_name, self.lang)
)
result = self._find_resource(res_name, 'en-us', res_dirname)
return result

def _find_resource(self, res_name, lang, res_dirname=None):
"""Finds a resource by name, lang and dir
"""
if res_dirname:
# Try the old translated directory (dialog/vocab/regex)
path = join(self.root_dir, res_dirname, self.lang, res_name)
path = join(self.root_dir, res_dirname, lang, res_name)
if exists(path):
return path

Expand All @@ -687,7 +700,7 @@ def find_resource(self, res_name, res_dirname=None):
return path

# New scheme: search for res_name under the 'locale' folder
root_path = join(self.root_dir, 'locale', self.lang)
root_path = join(self.root_dir, 'locale', lang)
for path, _, files in walk(root_path):
if res_name in files:
return join(path, res_name)
Expand Down