I have multi files json in a folder I want to add a path in my code and output
files_json = ('/myfiles/*.json') output = /OutputfilesFolder/
import json
import re
from googletrans import Translator
from glob import glob
translator = Translator()
def translate_dict(en_dict):
es_dict = {}
for key, value in en_dict.items():
# Identify placeholders
placeholders = re.findall(r'{.*?}', value)
# Remove placeholders from the original string
for placeholder in placeholders:
value = value.replace(placeholder, '')
# Translate the string without placeholders
translation = translator.translate(value, dest='ar')
# Add the placeholders back to the translated string
translated_value = translation.text
for placeholder in placeholders:
translated_value += ' ' + placeholder
es_dict[key] = translated_value.strip()
return es_dict
files_json = {
"000a1a966f5e5aba0e7c": "Update Settings",
"00a60ec8ca21b351da90": "Terms Url",
#...
# include all the keys and values you want to translate
#...
}
# Get the Spanish translation
es_dict = translate_dict(files_json)
# Print the Spanish dictionary
print(json.dumps(es_dict, ensure_ascii=False))
Using a library glob .I hope to help