-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
7 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,32 @@ | ||
import asyncio | ||
|
||
import yaml | ||
import asyncio | ||
from gpytranslate import Translator | ||
|
||
|
||
async def translate_yaml(input_file, output_file, target_language): | ||
with open(input_file, "r", encoding="utf-8") as file: | ||
with open(input_file, 'r', encoding='utf-8') as file: | ||
data = yaml.safe_load(file) | ||
|
||
translator = Translator() | ||
|
||
for key, value in data.items(): | ||
if isinstance(value, str): | ||
translation = await translator.translate( | ||
value, target_language=target_language | ||
) | ||
translation = await translator.translate(value, target_language=target_language) | ||
data[key] = translation | ||
elif isinstance(value, list): | ||
translated_list = [] | ||
for item in value: | ||
if isinstance(item, str): | ||
translation = await translator.translate( | ||
item, target_language=target_language | ||
) | ||
translation = await translator.translate(item, target_language=target_language) | ||
translated_list.append(translation) | ||
else: | ||
translated_list.append(item) | ||
data[key] = translated_list | ||
|
||
with open(output_file, "w", encoding="utf-8") as file: | ||
yaml.dump(data, file, allow_unicode=True, default_flow_style=False) | ||
|
||
with open(output_file, 'w', encoding='utf-8') as file: | ||
yaml.safe_dump(data, file, allow_unicode=True) | ||
|
||
async def main(): | ||
await translate_yaml("langs/strings/id.yml", "langs/strings/en.yml", "en") | ||
|
||
await translate_yaml('langs/strings/id.yml', 'langs/strings/en.yml', 'en') | ||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |