Skip to content

Commit

Permalink
githubへGo!!!!!!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Gorayan-sub committed Aug 17, 2021
0 parents commit 55e14e7
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 0 deletions.
142 changes: 142 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pyyaml~=5.4.1
google-cloud-translate==2.0.1

0 comments on commit 55e14e7

Please sign in to comment.