Created
June 25, 2023 00:07
-
-
Save mgyong/408ec07308a7fee2b34cbf0bf4bb3ce1 to your computer and use it in GitHub Desktop.
python script for imagine mobile translation using google cloud translation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def translate_text(target: str, text: str) -> dict: | |
"""Translates text into the target language. | |
Target must be an ISO 639-1 language code. | |
See https://g.co/cloud/translate/v2/translate-reference#supported_languages | |
""" | |
from google.cloud import translate_v2 as translate | |
translate_client = translate.Client() | |
if isinstance(text, bytes): | |
text = text.decode("utf-8") | |
# Text can also be a sequence of strings, in which case this method | |
# will return a sequence of results for each text. | |
result = translate_client.translate(text, target_language=target) | |
print("Text: {}".format(result["input"])) | |
print("Translation: {}".format(result["translatedText"])) | |
print("Detected source language: {}".format(result["detectedSourceLanguage"])) | |
return result | |
def read_file(source: str) -> dict: | |
file_dict = {} | |
pattern = r'^""$' | |
f = open(source, 'r') | |
lines = f.readlines() | |
for line in lines: | |
matches = regex(line.strip()) | |
if len(matches) > 1: | |
file_dict[matches[0]] = matches[1] | |
if len(matches)==1: | |
file_dict[matches[0]] = "" | |
return file_dict | |
def append_dict_to_file(dictionary:dict, filename:str): | |
with open(filename, 'a') as file: | |
for key, value in dictionary.items(): | |
if value != "": | |
line_out = '"'+ key +'"' + " = " + '"'+value+ '"'+";" | |
else: | |
line_out = key | |
file.write(line_out + '\n') | |
def translate_file(target:str, source:str, output:str ): | |
dict_source = read_file(source) | |
output_dict={} | |
for key in dict_source: | |
if dict_source[key] != "": | |
result = translate_text(target, dict_source[key]) | |
output_dict[key] = result["translatedText"] | |
else: | |
output_dict[key] = "" | |
append_dict_to_file(output_dict, output) | |
def regex(string): | |
# Define the regular expression pattern | |
pattern = r'"([^"]*)"' | |
# Find all matches using the pattern | |
matches = re.findall(pattern, string) | |
# Extract the value between double quotation marks | |
if matches and len(matches)>1: | |
extracted_key = matches[0] | |
extracted_value = matches[1] | |
print(extracted_key, extracted_value) | |
else: | |
print("No match found.") | |
matches = {} | |
matches[0] = string | |
return matches | |
#output = read_file('./Localizable_en.txt') | |
#print(output) | |
#translate_file("ja",'./Localizable.string', './japanese/Localizable.string' ) #japanese | |
#translate_file("ko",'./Localizable.string', './korean/Localizable.string' ) #korean | |
#translate_file("zh",'./Localizable.string', './chinese/Localizable.string' ) #chinese | |
translate_file("de",'./Localizable.string', './german/Localizable.string' ) #german | |
translate_file("it",'./Localizable.string', './italian/Localizable.string' ) #italian | |
translate_file("es",'./Localizable.string', './spanish/Localizable.string' ) #spanish | |
translate_file("fr",'./Localizable.string', './french/Localizable.string' ) #french |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment