Skip to content

Instantly share code, notes, and snippets.

@flodolo
Last active February 15, 2024 06:30
Show Gist options
  • Save flodolo/d64b7756d32be76661cd184c333ddc5c to your computer and use it in GitHub Desktop.
Save flodolo/d64b7756d32be76661cd184c333ddc5c to your computer and use it in GitHub Desktop.
Check survey files exported from Smartling
from glob import glob
import csv
import os
import re
import sys
# Pass a path as an argument, the script will go through all .csv files included.
if len(sys.argv) < 2:
sys.exit("Missing path argument")
path = os.path.abspath(sys.argv[1])
for file in glob(f"{path}/**/*.csv", recursive=True):
errors = []
with open(file) as f:
reader = csv.reader(f, delimiter=',', quotechar='"')
for row in reader:
# Check if question references are preserved in the translation.
# Example: ${q://QID68/ChoiceGroup/SelectedChoices}
id = row[0]
source = row[1]
source_matches = re.findall(r"\$\{q://QID\d+[/a-z]+}", source, flags=re.IGNORECASE)
translation = row[3]
translation_matches = re.findall(r"\$\{q://QID\d+[/a-z]+}", translation, flags=re.IGNORECASE)
if source_matches != translation_matches:
errors.append(
"\n----"
f"\nERROR: question references don't match for string {id}"
f"\n\nSource matches: {source_matches}"
f"\nSource string: {source}"
f"\n\nTranslation matches: {translation_matches}"
f"\nTranslation: {translation}"
)
# Check numeric values
try:
num = int(source)
if str(num) == source and translation != source:
errors.append(
"\n----"
f"\nWARNING: numeric value changed for string {id}"
f"\n\nSource string: {source}"
f"\n\nTranslation: {translation}"
)
except ValueError:
pass
"""
# Check for untranslated strings
if source == translation:
if id.startswith(
(
"QID8_",
"QID9_",
"QID12_",
"QID18_",
"QID19_",
"QID12_",
"QID45_",
"QID87_",
"QID88_",
)
):
continue
print("\n----\n")
print("WARNING: translation is identical to source")
print(f"ID: {id}")
print(f"Source string: {source}")
print(f"Translation: {translation}")
"""
if errors:
print(f"\nFile: {file}")
print("\n".join(errors))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment