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

[google translate]Implementing Language Selection Memory and Non-translation of Code Blocks #3111

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
47 changes: 43 additions & 4 deletions extensions/google_translate/script.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import gradio as gr
from deep_translator import GoogleTranslator
import os
import re
import uuid

params = {
"activate": True,
"language string": "ja",
"language string": "ko",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not necessary.

}

language_codes = {'Afrikaans': 'af', 'Albanian': 'sq', 'Amharic': 'am', 'Arabic': 'ar', 'Armenian': 'hy', 'Azerbaijani': 'az', 'Basque': 'eu', 'Belarusian': 'be', 'Bengali': 'bn', 'Bosnian': 'bs', 'Bulgarian': 'bg', 'Catalan': 'ca', 'Cebuano': 'ceb', 'Chinese (Simplified)': 'zh-CN', 'Chinese (Traditional)': 'zh-TW', 'Corsican': 'co', 'Croatian': 'hr', 'Czech': 'cs', 'Danish': 'da', 'Dutch': 'nl', 'English': 'en', 'Esperanto': 'eo', 'Estonian': 'et', 'Finnish': 'fi', 'French': 'fr', 'Frisian': 'fy', 'Galician': 'gl', 'Georgian': 'ka', 'German': 'de', 'Greek': 'el', 'Gujarati': 'gu', 'Haitian Creole': 'ht', 'Hausa': 'ha', 'Hawaiian': 'haw', 'Hebrew': 'iw', 'Hindi': 'hi', 'Hmong': 'hmn', 'Hungarian': 'hu', 'Icelandic': 'is', 'Igbo': 'ig', 'Indonesian': 'id', 'Irish': 'ga', 'Italian': 'it', 'Japanese': 'ja', 'Javanese': 'jw', 'Kannada': 'kn', 'Kazakh': 'kk', 'Khmer': 'km', 'Korean': 'ko', 'Kurdish': 'ku', 'Kyrgyz': 'ky', 'Lao': 'lo', 'Latin': 'la', 'Latvian': 'lv', 'Lithuanian': 'lt', 'Luxembourgish': 'lb', 'Macedonian': 'mk', 'Malagasy': 'mg', 'Malay': 'ms', 'Malayalam': 'ml', 'Maltese': 'mt', 'Maori': 'mi', 'Marathi': 'mr', 'Mongolian': 'mn', 'Myanmar (Burmese)': 'my', 'Nepali': 'ne', 'Norwegian': 'no', 'Nyanja (Chichewa)': 'ny', 'Pashto': 'ps', 'Persian': 'fa', 'Polish': 'pl', 'Portuguese (Portugal, Brazil)': 'pt', 'Punjabi': 'pa', 'Romanian': 'ro', 'Russian': 'ru', 'Samoan': 'sm', 'Scots Gaelic': 'gd', 'Serbian': 'sr', 'Sesotho': 'st', 'Shona': 'sn', 'Sindhi': 'sd', 'Sinhala (Sinhalese)': 'si', 'Slovak': 'sk', 'Slovenian': 'sl', 'Somali': 'so', 'Spanish': 'es', 'Sundanese': 'su', 'Swahili': 'sw', 'Swedish': 'sv', 'Tagalog (Filipino)': 'tl', 'Tajik': 'tg', 'Tamil': 'ta', 'Telugu': 'te', 'Thai': 'th', 'Turkish': 'tr', 'Ukrainian': 'uk', 'Urdu': 'ur', 'Uzbek': 'uz', 'Vietnamese': 'vi', 'Welsh': 'cy', 'Xhosa': 'xh', 'Yiddish': 'yi', 'Yoruba': 'yo', 'Zulu': 'zu'}

def modify_string(string, source, target):
pattern = re.compile(r'```(.*?)\n(.*?)```', re.DOTALL)
blocks = [(str(uuid.uuid4()), m.group(1), m.group(2)) for m in re.finditer(pattern, string)]
string_without_blocks = string
for uuid_str, _, _ in blocks:
string_without_blocks = re.sub(pattern, uuid_str, string_without_blocks, count=1)
translated_string = GoogleTranslator(source=source, target=target).translate(string_without_blocks)
for uuid_str, lang, block in blocks:
# Remove leading and trailing whitespaces from each block
block = block.strip()
translated_string = translated_string.replace(uuid_str, '```' + lang + '\n' + block + '\n```')
return translated_string


def input_modifier(string):
"""
Expand All @@ -17,7 +33,7 @@ def input_modifier(string):
if not params['activate']:
return string

return GoogleTranslator(source=params['language string'], target='en').translate(string)
return modify_string(string, params['language string'], 'en')


def output_modifier(string):
Expand All @@ -27,7 +43,7 @@ def output_modifier(string):
if not params['activate']:
return string

return GoogleTranslator(source='en', target=params['language string']).translate(string)
return modify_string(string, 'en', params['language string'])


def bot_prefix_modifier(string):
Expand All @@ -41,6 +57,9 @@ def bot_prefix_modifier(string):


def ui():
params['language string'] = read_language_code()


# Finding the language name from the language code to use as the default value
language_name = list(language_codes.keys())[list(language_codes.values()).index(params['language string'])]

Expand All @@ -53,4 +72,24 @@ def ui():

# Event functions to update the parameters in the backend
activate.change(lambda x: params.update({"activate": x}), activate, None)
language.change(lambda x: params.update({"language string": language_codes[x]}), language, None)
# language.change(lambda x: params.update({"language string": language_codes[x]}), language, None)
language.change(lambda x: (new_language_code := language_codes[x], params.update({"language string": new_language_code}), write_language_code(new_language_code)), language, None)


def read_language_code(filename="setting/latest_use_language.txt"):
try:
with open(filename, "r") as file:
language_code = file.read().strip()
return language_code
except FileNotFoundError:
print(f"Cannot find the file {filename}.")
return 'ko'

def write_language_code(language_code, filename="setting/latest_use_language.txt"):
# Check if the directory exists, if not, create it
directory = os.path.dirname(filename)
if not os.path.exists(directory):
os.makedirs(directory)

with open(filename, "w") as file:
file.write(language_code)