Skip to content

Commit

Permalink
Export lines to update
Browse files Browse the repository at this point in the history
Export the lines with a little Levenshtein distance to update them on Paratranz
  • Loading branch information
NicolasGrosjean committed Dec 31, 2023
1 parent b761aec commit 17443f6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 13 deletions.
51 changes: 41 additions & 10 deletions src/apply_diff_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

DIR_TO_TRANSLATE = "to_translate"
FILE_TO_TRANSLATE_PREFIX = "file_to_translate"
DIR_TO_UPDATE = "to_update"
FILE_TO_UPDATE_OLD_SOURCE_PREFIX = "old_source"
FILE_TO_UPDATE_SOURCE_PREFIX = "source"
FILE_TO_UPDATE_DEST_PREFIX = "dest"


def get_args():
Expand Down Expand Up @@ -55,6 +59,9 @@ def apply_diff_one_file(
source_lang,
existing_translations,
lines_to_translate,
lines_to_update_old_source,
lines_to_update_source,
lines_to_update_dest,
keys_to_ignore,
):
with open(source_file_path, "r", encoding="utf8") as f:
Expand Down Expand Up @@ -102,6 +109,10 @@ def apply_diff_one_file(
# we keep translation but change version number
# OR translation if already translated elsewhere
write_new_line_or_get_existing_translation(f, key, dest_text, existing_translations, None)
if value not in existing_translations:
lines_to_update_old_source.append(" " + key + ':9 "' + old_source_values[key]["value"] + '"\n')
lines_to_update_source.append(" " + key + ':9 "' + value + '"\n')
lines_to_update_dest.append(" " + key + ':9 "' + dest_text + '"\n')
else:
# Add current source text because the previous destination was not translated
# OR translation if already translated elsewhere
Expand Down Expand Up @@ -160,6 +171,10 @@ def apply_diff_all(old_dir, current_src_dir, current_dest_dir, source_lang, dest
existing_translations[old_source_values[source_key]["value"]] = dest_values[source_key]["value"]
# Store lines to translate
lines_to_translate = [f"\ufeffl_{source_lang}:\n"]
# Store lines to update
lines_to_update_old_source = [f"\ufeffl_{source_lang}:\n"]
lines_to_update_source = [f"\ufeffl_{source_lang}:\n"]
lines_to_update_dest = [f"\ufeffl_{dest_lang}:\n"]
# Apply diff with current source texts
for root, _, files in os.walk(current_src_dir):
for file in files:
Expand All @@ -181,18 +196,34 @@ def apply_diff_all(old_dir, current_src_dir, current_dest_dir, source_lang, dest
source_lang,
existing_translations,
lines_to_translate,
lines_to_update_old_source,
lines_to_update_source,
lines_to_update_dest,
keys_to_ignore,
)
# Export lines to translate
if len(lines_to_translate) > 0:
dir_to_translate = os.path.join(current_dest_dir, "..", DIR_TO_TRANSLATE)
os.makedirs(dir_to_translate, exist_ok=True)
with open(
os.path.join(dir_to_translate, f"{FILE_TO_TRANSLATE_PREFIX}_l_{source_lang}.yml"),
"w",
encoding="utf8",
) as f:
f.writelines(lines_to_translate)
# Export lines to and to update
lines_to_export = [lines_to_translate, lines_to_update_old_source, lines_to_update_source, lines_to_update_dest]
directories = [
os.path.join(current_dest_dir, "..", DIR_TO_TRANSLATE),
os.path.join(current_dest_dir, "..", DIR_TO_UPDATE),
os.path.join(current_dest_dir, "..", DIR_TO_UPDATE),
os.path.join(current_dest_dir, "..", DIR_TO_UPDATE)
]
file_names = [
f"{FILE_TO_TRANSLATE_PREFIX}_l_{source_lang}.yml",
f"{FILE_TO_UPDATE_OLD_SOURCE_PREFIX}_l_{source_lang}.yml",
f"{FILE_TO_UPDATE_SOURCE_PREFIX}_l_{source_lang}.yml",
f"{FILE_TO_UPDATE_DEST_PREFIX}_l_{source_lang}.yml"
]
for i in range(len(lines_to_export)):
if len(lines_to_export[i]) > 0:
os.makedirs(directories[i], exist_ok=True)
with open(
os.path.join(directories[i], file_names[i]),
"w",
encoding="utf8",
) as f:
f.writelines(lines_to_export[i])


def apply_diff_all_old_formats(old_dir, current_dir, source_lang, dest_lang, keys_to_ignore):
Expand Down
27 changes: 24 additions & 3 deletions tests/test_apply_diff_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import shutil

from tests.utils import get_data_dir
from src.apply_diff_all import DIR_TO_TRANSLATE, FILE_TO_TRANSLATE_PREFIX, apply_diff_all_old_formats
from src.apply_diff_all import DIR_TO_TRANSLATE, DIR_TO_UPDATE, FILE_TO_TRANSLATE_PREFIX, FILE_TO_UPDATE_DEST_PREFIX, FILE_TO_UPDATE_OLD_SOURCE_PREFIX, FILE_TO_UPDATE_SOURCE_PREFIX, apply_diff_all_old_formats


class TestApplyDiffAll(unittest.TestCase):
Expand Down Expand Up @@ -32,8 +32,12 @@ def tearDownClass(cls):
if file.endswith("l_french.yml"):
os.remove(os.path.join(cls.data_dir, "new", file))
file_to_translate = os.path.join(cls.data_dir, DIR_TO_TRANSLATE, f"{FILE_TO_TRANSLATE_PREFIX}_l_english.yml")
if os.path.exists(file_to_translate):
os.remove(file_to_translate)
old_source = os.path.join(cls.data_dir, DIR_TO_UPDATE, f"{FILE_TO_UPDATE_OLD_SOURCE_PREFIX}_l_english.yml")
source = os.path.join(cls.data_dir, DIR_TO_UPDATE, f"{FILE_TO_UPDATE_SOURCE_PREFIX}_l_english.yml")
dest = os.path.join(cls.data_dir, DIR_TO_UPDATE, f"{FILE_TO_UPDATE_DEST_PREFIX}_l_french.yml")
for file in [file_to_translate, old_source, source, dest]:
if os.path.exists(file):
os.remove(file)

def test_apply_diff_same_sources(self):
with open(os.path.abspath(os.path.join(self.data_dir, "new", "0_l_french.yml")), "r", encoding="utf8") as f:
Expand Down Expand Up @@ -180,3 +184,20 @@ def test_lines_to_translate(self):
self.assertEqual(len(expected_lines), len(lines) - 1)
for line in expected_lines:
self.assertTrue(line in lines, f"{line} not found in {file_to_translate}")

def test_lines_to_update(self):
old_source = os.path.join(self.data_dir, DIR_TO_UPDATE, f"{FILE_TO_UPDATE_OLD_SOURCE_PREFIX}_l_english.yml")
source = os.path.join(self.data_dir, DIR_TO_UPDATE, f"{FILE_TO_UPDATE_SOURCE_PREFIX}_l_english.yml")
dest = os.path.join(self.data_dir, DIR_TO_UPDATE, f"{FILE_TO_UPDATE_DEST_PREFIX}_l_english.yml")
expected_first_line = ["\ufeffl_english:", "\ufeffl_english:", "\ufeffl_french:"]
expected_lines = [[' KEY60:9 "value0"\n'], [' KEY60:9 "value42"\n'], [' KEY60:9 "valeur0"\n']]
i = 0
for file in [old_source, source, dest]:
self.assertTrue(os.path.exists(file), f"{file} doesn't exist")
with open(os.path.abspath(file), "r", encoding="utf8") as f:
lines = f.readlines()
self.assertEqual(lines[0].replace("\n", ""), expected_first_line[i])
self.assertEqual(len(expected_lines[i]), len(lines) - 1)
for line in expected_lines[i]:
self.assertTrue(line in lines, f"{line} not found in {file}")
i += 1

0 comments on commit 17443f6

Please sign in to comment.