Skip to content

Commit

Permalink
Merge pull request #16 from ubclaunchpad/localize
Browse files Browse the repository at this point in the history
Localize class
  • Loading branch information
vassilyp authored Nov 28, 2024
2 parents 8812162 + 3fdc4fb commit b8a7a86
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 43 deletions.
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.

0 comments on commit b8a7a86

Please sign in to comment.