diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ecfa783 --- /dev/null +++ b/.gitignore @@ -0,0 +1,142 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ +*.iml + +test.py +/out/ diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..0dc6efb --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,14 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..de3fcfe --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b8632c4 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..d55d7f6 --- /dev/null +++ b/main.py @@ -0,0 +1,67 @@ +import os +from google.cloud import translate_v2 as translate +import yaml +import sys + + +# Google Cloud Translation API で翻訳 +def transtate(text): + result = client.translate(text, target_language='ja') + text = result["translatedText"] + text = text.translate(str.maketrans({chr(0xFF01 + i): chr(0x21 + i) for i in range(94)})) + print(result["input"] + " ===> " + text) + return text + + +# 再起処理で子要素を翻訳していく +def recursive_transtate(yaml_element): + + if isinstance(yaml_element, list): + + return list(map(recursive_transtate, yaml_element)) + + elif isinstance(yaml_element, dict): + + for key in yaml_element.keys(): + + yaml_element[key] = recursive_transtate(yaml_element[key]) + + return yaml_element + + else: + return transtate(yaml_element) + + +# client取得 +client = translate.Client() + +# 引数から、pathを取得 +args = sys.argv + +if len(args) == 1: + print("[error]第一引数に対象のファイルのパスを入力してください") + exit(1) + +target_file_path = args[1] + +if len(args) == 2: + dest_path = "" +else: + dest_path = args[2] + +# pathからyamlファイルを読み込む +with open(target_file_path, 'r', encoding="utf-8") as yml: + loaded_yaml = yaml.safe_load(yml) + +# 翻訳 +recursive_transtate(loaded_yaml) + +target_file_path = os.path.splitext(target_file_path)[0] +output_path = target_file_path + "_translated.yml" + +if os.path.exists(output_path): + os.remove(output_path) + +# output_pathにカキコ +with open(output_path, 'w', encoding="utf-8") as file: + yaml.dump(loaded_yaml, file, allow_unicode=True) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..3f37d64 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +pyyaml~=5.4.1 +google-cloud-translate==2.0.1 \ No newline at end of file