-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from ubclaunchpad/localize
Localize class
- Loading branch information
Showing
3 changed files
with
71 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,28 @@ | ||
from src.internationalize.helpers import get_json | ||
|
||
# Input: | ||
# - file_path: path of json file | ||
# - language: chosen language to translate to | ||
# - word: chosen word to translate | ||
# Output: translated word based on chosen language | ||
def get_translation(file_path, language, word): | ||
data = get_json(file_path) | ||
for translation in data["translations"]: | ||
if translation["language"] == language: | ||
new_word = translation.get(word) | ||
if (new_word == None): | ||
return f"'{word}' not found in {language}" | ||
return new_word | ||
return f"'{language}' not found" | ||
import json | ||
import os | ||
|
||
|
||
class Localize: | ||
languages_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "languages") | ||
translations_map = {} | ||
|
||
@classmethod | ||
def load_language(cls, language): | ||
""" | ||
Load the translation file for a specific language if not already loaded. | ||
""" | ||
if language not in cls.translations_map: | ||
file_path = os.path.join(cls.languages_dir, f"{language}.json") | ||
if os.path.exists(file_path): | ||
with open(file_path, "r") as file: | ||
cls.translations_map[language] = json.load(file) | ||
else: | ||
raise FileNotFoundError(f"Translations for {language} not found.") | ||
|
||
@classmethod | ||
def translate(cls, word, language): | ||
""" | ||
Get translation for a word in the specified language. | ||
""" | ||
cls.load_language(language) | ||
return cls.translations_map[language].get(word, f"Translation for {word} not found") |
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,43 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
from src.internationalize.localize import Localize | ||
|
||
# to test: | ||
# in i18nilize directory, run python -m tests.test_localize | ||
|
||
class TestLocalize(unittest.TestCase): | ||
def setUp(self): | ||
Localize.translations_map = {} | ||
|
||
@patch("os.path.exists") | ||
def test_load_language_already_in_map(self, mock_path_exists): | ||
Localize.translations_map["french"] = {"hello": "bonjour"} | ||
Localize.load_language("french") | ||
|
||
mock_path_exists.assert_not_called() | ||
|
||
def test_load_existing_language(self): | ||
Localize.load_language("spanish") | ||
|
||
self.assertIn("spanish", Localize.translations_map) | ||
self.assertEqual(Localize.translations_map["spanish"], {"hello": "hola", "thanks": "gracias"}) | ||
|
||
def test_load_non_existing_language(self): | ||
with self.assertRaises(FileNotFoundError) as context: | ||
Localize.load_language("japanese") | ||
|
||
self.assertEqual(str(context.exception), "Translations for japanese not found.") | ||
|
||
def test_translate_valid(self): | ||
self.assertEqual(Localize.translate("hello", "spanish"), "hola", "Translation error: is the translation missing from the language file?") | ||
self.assertEqual(Localize.translate("thanks", "french"), "merci", "Translation error: is the translation missing from the language file?") | ||
|
||
def test_translate_invalid(self): | ||
self.assertEqual(Localize.translate("asdf", "spanish"), "Translation for asdf not found") | ||
|
||
def test_translate_invalid_language(self): | ||
self.assertRaises(FileNotFoundError, Localize.translate, "hello", "asdf") | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file was deleted.
Oops, something went wrong.