wagtail / wagtail-localize

Translation plugin for Wagtail CMS
https://wagtail-localize.org/
Other
222 stars 84 forks source link

Feature: Utility Command to check missing translations and i18n tag usage in templates #682

Open estyxx opened 1 year ago

estyxx commented 1 year ago

I have been working on translating a website that was not previously developed to be multi-language, so look up to every template and spot the strings that have to be translated with the translate templatetag and loading i18n if it was not already loaded.

To check I didn't miss some strings in the template files or that I accidentally forgot to add {% load i18n %} on top, I created a little script with some regex...

It’s very rudimentary but here’s how I did it

import pathlib
import re

path = pathlib.Path("./path")

for file in path.glob("**/*.html"):
    with open(file, "r") as f:
        contents = f.read()
        # checks the attributes to translate
        matches1 = re.findall(
            r'(placeholder|aria-label|data-action|data-label|data-category)=("[\w\s\n\(\)-]+")',
            contents,
        )
        # check the text between HTML tags
        matches2 = re.findall(
            r"(?<=[>])(?![{}\t]|\s+$)([\w\s,!\-.\n]+)(?=[<\{])", contents
        )
        # removes any empty false positive match
        matches2 = [m for m in matches2 if m.strip()]
        # checks if `i18n` is present in the file
        match_i18n = re.findall(r"\{% load.*\bi18n\b.*%\}", contents)
        if matches1 or matches2:
            print(f"{file}:")

            if matches1:
                for match in matches1:
                    print(f"\t{match[0]}: {match[1]}")
            if matches2:
                for match in matches2:
                    print(f"\t{match}")
            # we have some strings to convert and the `i18n` is not present in the file!
            if not match_i18n:
                print("!!! MISSING i18n!!!")

            print()
        else:
            match_translate = re.findall(r"\{% translate .*%\}", contents)
            if match_translate and not match_i18n:
                # we used `{% translate .... %}` but we forgot to add `{% load i18n %}`!
                print(f"{file}: MISSING i18n!!!")

So my thought was that it could become a nice utility command for wagtail-localise to help people translate the website! Something to run locally like: wagtail-localise translate --check or a custom Django command...