diff --git a/i18nilize/src/internationalize/diffing_processor.py b/i18nilize/src/internationalize/diffing_processor.py index f024ff7..73cd3c6 100644 --- a/i18nilize/src/internationalize/diffing_processor.py +++ b/i18nilize/src/internationalize/diffing_processor.py @@ -2,7 +2,9 @@ import hashlib import json from dirsync import sync +from . import globals from src.internationalize.helpers import compute_hash, compute_hashes, read_json_file +from src.internationalize.error_handler import ErrorHandler JSON_EXTENSION = ".json" @@ -57,6 +59,13 @@ def update_metadata(self, hash_dict): json.dump(hash_dict, outfile) def sync_translations(self): + handler = ErrorHandler(globals.LANGUAGES_DIR) + errors = handler.verify_languages() + if errors: + print("Errors detected in language files:") + for file, error in errors.items(): + print(f"{file}: {error}") + return sync(self.curr_translation_files_dir, self.diff_state_files_dir, "sync", purge=True) """ diff --git a/i18nilize/src/internationalize/error_handler.py b/i18nilize/src/internationalize/error_handler.py new file mode 100644 index 0000000..2abbbb9 --- /dev/null +++ b/i18nilize/src/internationalize/error_handler.py @@ -0,0 +1,92 @@ +import json +import os + +""" +Error Handler Class +""" +class ErrorHandler(): + """ + Sets up the directory where the languages are located + """ + def __init__(self, translations_dir): + self.translations_dir = translations_dir + + """ + Verify each language file is valid + """ + def verify_languages(self): + """ + key: language containing an error + value: error message + """ + errors = {} + + all_language_files = os.listdir(self.translations_dir) + + for language_file in all_language_files: + error = self.handle_error(language_file) + if error != "": + errors[language_file] = error + + return errors + + + """ + Used to handle an error from a language + Input: string: language_file (source of error), boolean: error_expected (is the error expected?)\ + Output: descriptive string about the error from the language file + """ + def handle_error(self, language_file, error_expected=False): + invalid_file_result = "" + # Verify if file is invalid + invalid_file_result = self.handle_invalid_file(language_file) + # print(invalid_file_result) + if invalid_file_result != "": + return invalid_file_result + # Verify if any keys are invalid + invalid_keys_result = self.handle_invalid_keys(language_file) + if invalid_keys_result != "": + return invalid_keys_result + return "" + + """ + Checks if given language is in an invalid file + Output: + - An empty string if there isn't any errors + - A descriptive message about the error + """ + def handle_invalid_file(self, language_file): + language_location = os.path.join(self.translations_dir, language_file) + try: + with open(language_location, "r") as file: + json.load(file) + except json.JSONDecodeError as e: + return "Invalid Language File, try fixing the json format." + except Exception as e: + print(f"Unexpected Error: {e}") + raise e + # return empty string if no error found + return "" + + """ + Checks if given language contains any invalid keys + Output: + - An empty string if there aren't any errors + - A descriptive message about the invalid key(s) + """ + def handle_invalid_keys(self, language_file): + language_location = os.path.join(self.translations_dir, language_file) + language = {} + try: + with open(language_location, "r") as file: + language = json.load(file) + for key in language: + stripped_key = key.strip() + if stripped_key == "": + return "Key is empty or contains only whitespace." + if not isinstance(language[key], str): + return f"Value is not a string." + except Exception as e: + print(f"Unexpected Error: {e}") + raise e + return "" diff --git a/i18nilize/src/internationalize/helpers.py b/i18nilize/src/internationalize/helpers.py index e95cabb..a5c399f 100644 --- a/i18nilize/src/internationalize/helpers.py +++ b/i18nilize/src/internationalize/helpers.py @@ -4,6 +4,7 @@ import hashlib import requests from . import globals +from internationalize.error_handler import ErrorHandler # Function to parse json file, given its path def get_json(file_path): @@ -38,13 +39,30 @@ def add_language(language): # Adds/updates a translated word under the given language in the default JSON file def add_update_translated_word(language, original_word, translated_word): file_path = os.path.join(globals.LANGUAGES_DIR, f"{language.lower()}.json") - + handler = ErrorHandler(globals.LANGUAGES_DIR) + if not os.path.exists(file_path): print(f"Error: Language '{language}' does not exist. Add the language before adding a translation.") sys.exit(1) + if not original_word.strip(): + print("Error: Original word cannot be empty or contain only whitespace.") + sys.exit(1) + if not translated_word.strip(): + print("Error: Translated word cannot be empty or contain only whitespace.") + sys.exit(1) + try: + data = get_json(file_path) + except json.JSONDecodeError as e: + result = handler.handle_error(f"{language.lower()}.json", True) + sys.exit(1) - data = get_json(file_path) - + result = handler.handle_error(f"{language.lower()}.json", True) + if (result == "Key is empty or contains only whitespace."): + print(result) + sys.exit(1) + elif (result != ""): + print(result) + sys.exit(1) data[original_word] = translated_word with open(file_path, 'w') as file: json.dump(data, file, indent=4) @@ -53,12 +71,31 @@ def add_update_translated_word(language, original_word, translated_word): # Deletes a translated word for the given language def delete_translation(language, original_word, translated_word): file_path = os.path.join(globals.LANGUAGES_DIR, f"{language.lower()}.json") - + handler = ErrorHandler(globals.LANGUAGES_DIR) if not os.path.exists(file_path): print(f"Error: Language '{language}' does not exist.") sys.exit(1) + try: + data = get_json(file_path) + except json.JSONDecodeError as e: + result = handler.handle_error(f"{language.lower()}.json", True) + sys.exit(1) + + result = handler.handle_error(f"{language.lower()}.json", True) + if (result == "Key is empty or contains only whitespace."): + print(result) + sys.exit(1) + elif (result != ""): + print(result) + sys.exit(1) - data = get_json(file_path) + if not original_word.strip(): + print("Error: Original word cannot be empty or contain only whitespace.") + sys.exit(1) + + if not translated_word.strip(): + print("Error: Translated word cannot be empty or contain only whitespace.") + sys.exit(1) if original_word not in data: print(f"Error: Original word '{original_word}' does not exist in language '{language}'.") diff --git a/i18nilize/src/internationalize/languages/turkish.json b/i18nilize/src/internationalize/languages/turkish.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/i18nilize/src/internationalize/languages/turkish.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/i18nilize/src/internationalize/languages/urdu.json b/i18nilize/src/internationalize/languages/urdu.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/i18nilize/src/internationalize/languages/urdu.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/i18nilize/src/localization.egg-info/PKG-INFO b/i18nilize/src/localization.egg-info/PKG-INFO new file mode 100644 index 0000000..dd59d81 --- /dev/null +++ b/i18nilize/src/localization.egg-info/PKG-INFO @@ -0,0 +1,25 @@ +Metadata-Version: 2.1 +Name: localization +Version: 1.0.0 +Summary: A localization package for microservices +Author: UBC Launchpad +Author-email: strategy@ubclaunchpad.com +Classifier: Programming Language :: Python :: 3 +Classifier: License :: OSI Approved :: MIT License +Classifier: Operating System :: OS Independent +Requires-Python: >=3.6 +Description-Content-Type: text/markdown +License-File: LICENSE.txt + +### L10n +A simple, lightweight interface for Python-based applications handle anything l10n/i18n +MIT License + +Copyright (c) [2024] [UBC Launchpad] + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/i18nilize/src/localization.egg-info/SOURCES.txt b/i18nilize/src/localization.egg-info/SOURCES.txt new file mode 100644 index 0000000..5cb9de1 --- /dev/null +++ b/i18nilize/src/localization.egg-info/SOURCES.txt @@ -0,0 +1,19 @@ +LICENSE.txt +README.md +pyproject.toml +setup.cfg +src/internationalize/__init__.py +src/internationalize/command_line.py +src/internationalize/globals.py +src/internationalize/helpers.py +src/internationalize/internationalize.py +src/internationalize/localize.py +src/localization.egg-info/PKG-INFO +src/localization.egg-info/SOURCES.txt +src/localization.egg-info/dependency_links.txt +src/localization.egg-info/top_level.txt +tests/test.py +tests/test_cli.py +tests/test_generate_file.py +tests/test_parse_json.py +tests/test_read_file.py \ No newline at end of file diff --git a/i18nilize/src/localization.egg-info/dependency_links.txt b/i18nilize/src/localization.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/i18nilize/src/localization.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/i18nilize/src/localization.egg-info/top_level.txt b/i18nilize/src/localization.egg-info/top_level.txt new file mode 100644 index 0000000..1d2fe09 --- /dev/null +++ b/i18nilize/src/localization.egg-info/top_level.txt @@ -0,0 +1 @@ +internationalize diff --git a/i18nilize/tests/resources/error_handling/expected.json b/i18nilize/tests/resources/error_handling/expected.json new file mode 100644 index 0000000..1d2fe3f --- /dev/null +++ b/i18nilize/tests/resources/error_handling/expected.json @@ -0,0 +1,6 @@ +{ + "invalid_file.json": "Invalid Language File, try fixing the json format.", + "empty_keys.json": "Key is empty or contains only whitespace.", + "non_string_values.json": "Value is not a string.", + "combined_issues.json": "Value is not a string." +} \ No newline at end of file diff --git a/i18nilize/tests/resources/error_handling/translations/combined_issues.json b/i18nilize/tests/resources/error_handling/translations/combined_issues.json new file mode 100644 index 0000000..e5cdc9b --- /dev/null +++ b/i18nilize/tests/resources/error_handling/translations/combined_issues.json @@ -0,0 +1,5 @@ +{ + "Hi": 123, + " ": "merci", + "hello-world": "salut" +} \ No newline at end of file diff --git a/i18nilize/tests/resources/error_handling/translations/empty_keys.json b/i18nilize/tests/resources/error_handling/translations/empty_keys.json new file mode 100644 index 0000000..bdcca93 --- /dev/null +++ b/i18nilize/tests/resources/error_handling/translations/empty_keys.json @@ -0,0 +1,4 @@ +{ + "": "as", + " ": "merci" +} \ No newline at end of file diff --git a/i18nilize/tests/resources/error_handling/translations/invalid_file.json b/i18nilize/tests/resources/error_handling/translations/invalid_file.json new file mode 100644 index 0000000..fd230e1 --- /dev/null +++ b/i18nilize/tests/resources/error_handling/translations/invalid_file.json @@ -0,0 +1,4 @@ +{ + "hello": "bonjour" + "thanks": "merci" +} \ No newline at end of file diff --git a/i18nilize/tests/resources/error_handling/translations/non_string_values.json b/i18nilize/tests/resources/error_handling/translations/non_string_values.json new file mode 100644 index 0000000..962915c --- /dev/null +++ b/i18nilize/tests/resources/error_handling/translations/non_string_values.json @@ -0,0 +1,4 @@ +{ + "Hi": 123, + "Thanks": "merci" +} \ No newline at end of file diff --git a/i18nilize/tests/resources/error_handling/translations/valid_keys.json b/i18nilize/tests/resources/error_handling/translations/valid_keys.json new file mode 100644 index 0000000..6d51899 --- /dev/null +++ b/i18nilize/tests/resources/error_handling/translations/valid_keys.json @@ -0,0 +1,4 @@ +{ + "hello": "bonjour", + "thanks": "merci" +} \ No newline at end of file diff --git a/i18nilize/tests/test_error_handler.py b/i18nilize/tests/test_error_handler.py new file mode 100644 index 0000000..1c11cc6 --- /dev/null +++ b/i18nilize/tests/test_error_handler.py @@ -0,0 +1,57 @@ +import json +import os +import unittest +from src.internationalize.error_handler import ErrorHandler + +class TestErrorHandler(unittest.TestCase): + def setUp(self): + self.test_folder = os.path.join("tests", "resources", "error_handling") + self.translations_folder = os.path.join(self.test_folder, "translations") + self.handler = ErrorHandler(self.translations_folder) + + # ================== Test Keys ================================ + def test_valid_keys(self): + language = "valid_keys.json" + self.assertEqual(self.handler.handle_invalid_keys(language), "") + + def test_non_string_values(self): + language = "non_string_values.json" + self.assertEqual(self.handler.handle_invalid_keys(language), "Value is not a string.") + + def test_empty_or_whitespace_keys(self): + language = "empty_keys.json" + self.assertEqual(self.handler.handle_invalid_keys(language), "Key is empty or contains only whitespace.") + + def test_combined_issues(self): + language = "combined_issues.json" + self.assertEqual(self.handler.handle_invalid_keys(language), "Value is not a string.") + + # ================== Test Invalid Files ================================ + + def test_invalid_file_individual(self): + language = "invalid_file.json" + self.assertEqual(self.handler.handle_invalid_file(language), "Invalid Language File, try fixing the json format.") + + # ================== Test Error Handler ================================ + def test_invalid_file(self): + print("invalid File Test: ") + language = "invalid_file.json" + self.assertEqual(self.handler.handle_error(language), "Invalid Language File, try fixing the json format.") + + def test_non_string_values(self): + language = "non_string_values.json" + self.assertEqual(self.handler.handle_error(language), "Value is not a string.") + + def test_valid_expected(self): + language = "valid_keys.json" + self.assertEqual(self.handler.handle_error(language), "") + + # ================== Test Folder ================================ + def test_invalid_folder(self): + with open(os.path.join(self.test_folder, "expected.json"), "r") as file: + expected = json.load(file) + + self.assertEqual(self.handler.verify_languages(), expected) + +if __name__ == "__main__": + unittest.main() diff --git a/src/internationalize/languages/croatian.json b/src/internationalize/languages/croatian.json new file mode 100644 index 0000000..fd230e1 --- /dev/null +++ b/src/internationalize/languages/croatian.json @@ -0,0 +1,4 @@ +{ + "hello": "bonjour" + "thanks": "merci" +} \ No newline at end of file