Merge pull request #2411 from MycroftAI/domcross-issue-2120

Fallback to 'en-us' when resource fails
pull/2412/head
Åke 2019-12-05 12:27:42 +01:00 committed by GitHub
commit 438e68ecb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 2 deletions

View File

@ -675,9 +675,22 @@ class MycroftSkill:
Returns: Returns:
string: The full path to the resource file or None if not found 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: if res_dirname:
# Try the old translated directory (dialog/vocab/regex) # 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): if exists(path):
return path return path
@ -687,7 +700,7 @@ class MycroftSkill:
return path return path
# New scheme: search for res_name under the 'locale' folder # 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): for path, _, files in walk(root_path):
if res_name in files: if res_name in files:
return join(path, res_name) return join(path, res_name)