diff --git a/i18nilize/src/internationalize/localize.py b/i18nilize/src/internationalize/localize.py index 7aca8ac..9d15a02 100644 --- a/i18nilize/src/internationalize/localize.py +++ b/i18nilize/src/internationalize/localize.py @@ -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" \ No newline at end of file +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") diff --git a/i18nilize/tests/test_localize.py b/i18nilize/tests/test_localize.py new file mode 100644 index 0000000..557118d --- /dev/null +++ b/i18nilize/tests/test_localize.py @@ -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() diff --git a/i18nilize/tests/test_read_file.py b/i18nilize/tests/test_read_file.py deleted file mode 100644 index cf03ad3..0000000 --- a/i18nilize/tests/test_read_file.py +++ /dev/null @@ -1,27 +0,0 @@ -import unittest -from src.internationalize.localize import get_translation -from src.internationalize.helpers import get_token - -# run tests using python -m tests.test_read_file at i18nilize directory level - -class TestLocalize(unittest.TestCase): - file_path = "tests/resources/basic.json" - - def test_read_token(self): - expected_token = "85124f79-0829-4b80-8b5c-d52700d86e46" - self.assertEqual(get_token(self.file_path), expected_token) - - def test_read_translations(self): - self.assertEqual(get_translation(self.file_path, "French", "hello"), "bonjour") - self.assertEqual(get_translation(self.file_path, "French", "No"), "Non") - self.assertEqual(get_translation(self.file_path, "French", "Why"), "pourquoi") - self.assertEqual(get_translation(self.file_path, "Spanish", "hello"), "Hola") - - def test_read_bad_translations(self): - self.assertEqual(get_translation(self.file_path, "French", "bonjour"), "'bonjour' not found in French") - self.assertEqual(get_translation(self.file_path, "Frrench", "hello"), "'Frrench' not found") - - - -if __name__ == '__main__': - unittest.main() \ No newline at end of file