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

File generation #3

Merged
merged 4 commits into from
Oct 25, 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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
32 changes: 31 additions & 1 deletion i18nilize/src/internationalize/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,35 @@ def get_token(file_path):
token = data["Token"]
return token


# Input: a JSON object
# Output: None, but creates a local JSON file containing the object
def create_json(json_object):
with open("src/internationalize/jsonFile/translations.json", "w") as outfile:
outfile.write(json_object)

# Input: None (for now)
# Output: None, but creates a local JSON file containing translations
def generate_file():
file_content = {
"Token": "85124f79-0829-4b80-8b5c-d52700d86e46",
"translations" : [{
"language": "French",
"hello": "bonjour",
"No": "Non",
"Why": "pourquoi",
},
{
"language": "Spanish",
"hello": "Hola",
},
]
}

# transforms the dictionary object above into a JSON object
json_object = json.dumps(file_content, indent=4)
create_json(json_object)

# make hashmap from translations
def make_translation_map(data):
translations_map = {}
Expand All @@ -36,4 +65,5 @@ def make_translation_map(data):

# get translations from hashmap given the language
def get_translation(translations_map, language):
return translations_map.get(language, "Translation not found")
return translations_map.get(language, "Translation not found")

15 changes: 15 additions & 0 deletions i18nilize/src/internationalize/jsonFile/translations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Token": "85124f79-0829-4b80-8b5c-d52700d86e46",
"translations": [
{
"language": "French",
"hello": "bonjour",
"No": "Non",
"Why": "pourquoi"
},
{
"language": "Spanish",
"hello": "Hola"
}
]
}
Binary file added i18nilize/tests/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file not shown.
29 changes: 29 additions & 0 deletions i18nilize/tests/test_generate_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import unittest
import json
from src.internationalize.generate_file import generate_file
from src.internationalize.helpers import get_json

# run tests using python -m tests.test_generate_file at i18nilize directory level

class TestGenerateFile(unittest.TestCase):
generate_file()
data = get_json("src/internationalize/jsonFile/translations.json")

def test_token(self):
self.assertEqual(self.data['Token'], "85124f79-0829-4b80-8b5c-d52700d86e46")

def test_translations(self):
translations = self.data['translations']
self.assertEqual(len(translations), 2)

# French
self.assertEqual(translations[0]['language'], "French")
self.assertEqual(translations[0]['hello'], "bonjour")
self.assertEqual(translations[0]['No'], "Non")
self.assertEqual(translations[0]['Why'], "pourquoi")

# Spanish
self.assertEqual(translations[1]['language'], "Spanish")
self.assertEqual(translations[1]['hello'], "Hola")

unittest.main()
Loading