Skip to content

Commit

Permalink
Add google translate support (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bikatr7 authored May 22, 2024
2 parents 6393bdc + d05c8f1 commit 7acbfc2
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 84 deletions.
7 changes: 6 additions & 1 deletion handlers/json_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class JsonHandler:
with open(FileEnsurer.translation_settings_description_path, 'r', encoding='utf-8') as file:
translation_settings_message = file.read()


##-------------------start-of-validate_json()--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

@staticmethod
Expand Down Expand Up @@ -125,6 +124,12 @@ def validate_json() -> None:
assert all(key in gemini_settings for key in gemini_keys), "gemini settings keys missing"
assert all(key in deepl_settings for key in deepl_keys), "deepl settings keys missing"

## ensure that those sections don't have any extra keys
assert all(key in base_translation_keys for key in base_translation_settings), "base translation settings has extra keys"
assert all(key in openai_keys for key in openai_settings), "openai settings has extra keys"
assert all(key in gemini_keys for key in gemini_settings), "gemini settings has extra keys"
assert all(key in deepl_keys for key in deepl_settings), "deepl settings has extra keys"

## validate each key using the validation rules
for key, validate in validation_rules.items():
if(key in base_translation_settings and not validate(base_translation_settings[key])):
Expand Down
22 changes: 12 additions & 10 deletions kudasai.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import re
import typing
import logging
import argparse

## third-party libraries
from kairyou import Kairyou
Expand Down Expand Up @@ -307,11 +306,12 @@ async def main() -> None:

if(len(sys.argv) <= 1):
await run_console_version()

elif(len(sys.argv) in [2, 3, 4, 5, 6]):
await run_cli_version()

else:
print(f"Invalid number of arguments ({len(sys.argv)}), max of 6. Please use --help for more information.")
print_usage_statement()

except Exception as e:
Expand Down Expand Up @@ -346,8 +346,6 @@ async def run_console_version():

raise e

print("In progress...")

await Kudasai.run_kudasai()

##-------------------start-of-run_cli_version()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Expand All @@ -369,7 +367,7 @@ def determine_argument_type(arg:str) -> str:
"""

conditions = [
(lambda arg: arg in ["deepl", "openai", "gemini"], "translation_method"),
(lambda arg: arg in ["deepl", "openai", "gemini", "google_translate"], "translation_method"),
(lambda arg: os.path.exists(arg) and not ".json" in arg, "text_to_translate"),
(lambda arg: len(arg) > 10 and not os.path.exists(arg), "api_key"),
(lambda arg: arg == "translate", "identifier"),
Expand All @@ -378,11 +376,12 @@ def determine_argument_type(arg:str) -> str:

for condition, result in conditions:
if(condition(arg)):
print(result)
print(f"Determined argument for '{arg}' as '{result}'")
logging.debug(f"Determined argument for '{arg}' as '{result}'")
return result

raise Exception("Invalid argument. Please use 'deepl', 'openai', or 'gemini'.")
raise Exception("Invalid argument. Please use 'deepl', 'openai', or 'gemini', or 'google_translate')")

mode = ""

try:
Expand Down Expand Up @@ -419,7 +418,9 @@ def determine_argument_type(arg:str) -> str:
method_to_translation_mode = {
"openai": "1",
"gemini": "2",
"deepl": "3"
"deepl": "3",
"google_translate": "4",
"google translate": "4"
}

Kudasai.text_to_preprocess = FileEnsurer.standard_read_file(sys.argv[arg_indices['text_to_translate_index']].strip('"'))
Expand Down Expand Up @@ -498,6 +499,7 @@ def print_usage_statement():
Additional Notes:
- All arguments should be enclosed in double quotes if they contain spaces. But double quotes are optional and will be striped. Single quotes are not allowed
- For more information, refer to the documentation at README.md
- For google translate, enter the method as 'google_translate', also google_translate doesn't support the api_key argument
""")


Expand Down
Empty file added modules/common/api_key_util.py
Empty file.
1 change: 1 addition & 0 deletions modules/common/file_ensurer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class FileEnsurer():
deepl_api_key_path = os.path.join(secrets_dir, "deepl_api_key.txt")
openai_api_key_path = os.path.join(secrets_dir,'openai_api_key.txt')
gemini_api_key_path = os.path.join(secrets_dir,'gemini_api_key.txt')
google_translate_service_key_json_path = os.path.join(secrets_dir, "google_translate_service_key.json")

## favicon
favicon_path = os.path.join(gui_lib, "Kudasai_Logo.png")
Expand Down
Loading

0 comments on commit 7acbfc2

Please sign in to comment.