diff --git a/README.md b/README.md index c47172b..1be3b88 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,16 @@ Installation pip install translate-po ``` -Usage +Script usage ```python from translate_po.main import run -run(fro="en" to="et" src="./untranslated" dest="./translated") +run(fro="en", to="et", src="./untranslated", dest="./translated", recursive=True) +``` + +Command-line usage +```bash +translate-po --fro="en" --to="et" --src="./untranslated" --dest="./translated" --recursive ``` ### Changelog diff --git a/setup.py b/setup.py index 8a526d3..e34a821 100644 --- a/setup.py +++ b/setup.py @@ -30,15 +30,15 @@ }, entry_points={ 'console_scripts': [ - 'sample=sample:main', + 'translate-po=translate_po.main:run', ], }, install_requires=[ 'polib>=1.1.0', - 'googletrans>=3.0.0' + 'googletrans==4.0.0rc1' ], setup_requires=[ 'polib>=1.1.0', - 'googletrans>=3.0.0' + 'googletrans==4.0.0rc1' ], ) diff --git a/translate_po/main.py b/translate_po/main.py index 06577f5..cf8cd9c 100644 --- a/translate_po/main.py +++ b/translate_po/main.py @@ -4,7 +4,7 @@ import polib from googletrans import Translator -from .utilities.constants import UNTRANSLATED_PATH, TRANSLATED_PATH, LANGUAGE_SOURCE, LANGUAGE_DESTINATION +from .utilities.constants import UNTRANSLATED_PATH, TRANSLATED_PATH, LANGUAGE_SOURCE, LANGUAGE_DESTINATION, RECURSIVE from .utilities.io import read_lines, save_lines from .utilities.match import recognize_po_file @@ -47,12 +47,25 @@ def run(**kwargs): default=kwargs.get('src', UNTRANSLATED_PATH)) parser.add_argument('--dest', type=str, help='Destination directory you want to translated files to end up in', default=kwargs.get('dest', TRANSLATED_PATH)) + parser.add_argument('--recursive', action='store_true', help='If provided, treat src and dest as directory hierarchies', + default=kwargs.get('recursive', RECURSIVE)) arguments = parser.parse_args() - for file in os.listdir(arguments.src): - if recognize_po_file(file): - found_files = True - solve(os.path.join(arguments.dest, file), os.path.join(arguments.src, file), arguments) + print(arguments) + + if arguments.recursive == True: + for root, _, files in os.walk(arguments.src): + for file in files: + if recognize_po_file(file): + found_files = True + relative_path = os.path.relpath(root, arguments.src) + if relative_path == '.': relative_path = '' + solve(os.path.join(arguments.dest, relative_path, file), os.path.join(root, file), arguments) + else: + for file in os.listdir(arguments.src): + if recognize_po_file(file): + found_files = True + solve(os.path.join(arguments.dest, file), os.path.join(arguments.src, file), arguments) if not found_files: raise Exception(f"Couldn't find any .po files at: '{arguments.src}'") diff --git a/translate_po/utilities/constants.py b/translate_po/utilities/constants.py index fa1d9e0..79f4e9c 100644 --- a/translate_po/utilities/constants.py +++ b/translate_po/utilities/constants.py @@ -2,3 +2,4 @@ TRANSLATED_PATH = "." LANGUAGE_SOURCE = "en" LANGUAGE_DESTINATION = "et" +RECURSIVE = False