-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3d71fd
commit 4830125
Showing
9 changed files
with
314 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from homeassistant import exceptions | ||
|
||
|
||
class SensorNotFound(exceptions.HomeAssistantError): | ||
"""Error to indicate a sensor is not found.""" | ||
|
||
|
||
class OperationNotFound(exceptions.HomeAssistantError): | ||
"""Error to indicate the operation specified is not valid.""" | ||
|
||
|
||
class IntervalNotValid(exceptions.HomeAssistantError): | ||
"""Error to indicate the interval specified is not valid.""" | ||
|
||
|
||
class NotUnique(exceptions.HomeAssistantError): | ||
"""Error to indicate that the name is not unique.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import logging | ||
import json | ||
import os | ||
from .const import LANGUAGE_FILES_DIR, SUPPORTED_LANGUAGES | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def localize(string, language): | ||
# try opening language file | ||
language = language.lower() | ||
translated_string = None | ||
main_path = os.path.dirname(__file__) | ||
stringpath = string.split(".") | ||
try: | ||
# if the language is not english and the language is supported | ||
if language != "en" and language in SUPPORTED_LANGUAGES: | ||
with open( | ||
os.path.join( | ||
main_path, LANGUAGE_FILES_DIR + os.sep + language + ".json" | ||
) | ||
) as f: | ||
data = json.load(f) | ||
translated_string = get_string_from_data(stringpath, data) | ||
# fallback to english in case string wasn't found | ||
if language == "en" or not isinstance(translated_string, str): | ||
with open( | ||
os.path.join(main_path, LANGUAGE_FILES_DIR + os.sep + "en.json") | ||
) as f: | ||
data = json.load(f) | ||
translated_string = get_string_from_data(stringpath, data) | ||
# if still not found, just return the string parameter | ||
if isinstance(translated_string, str): | ||
return translated_string | ||
else: | ||
return string | ||
except OSError: | ||
_LOGGER.error( | ||
"Couldn't load translations language file for {}".format(language) | ||
) | ||
|
||
|
||
def get_string_from_data(stringpath, data): | ||
data_to_walk = data | ||
for p in stringpath: | ||
if isinstance(data_to_walk, str): | ||
return data_to_walk | ||
if p in data_to_walk: | ||
data_to_walk = data_to_walk[p] | ||
return data_to_walk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.