Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localize class #16

Merged
merged 6 commits into from
Nov 28, 2024
Merged
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
44 changes: 28 additions & 16 deletions i18nilize/src/internationalize/localize.py
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")
43 changes: 43 additions & 0 deletions i18nilize/tests/test_localize.py
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()
27 changes: 0 additions & 27 deletions i18nilize/tests/test_read_file.py

This file was deleted.

Loading