Our reading app serves Portuguese as Brazilian Portuguese, everywhere: você , arquivo , cadastro , café da manhã . The book generator that feeds the catalogue did not get that memo. Left to itself it wrote European Portuguese, and 29 public books had to be converted to Brazilian on the production database. The generator's original bundles were still lying around, so a single re-upload of an old file would have quietly put the European text back.
The fix is a gate in the importer: a pt book that contains European forms is refused. This post walks through how that check works, what it catches, and where a regex-based language-variant detector bites.
The shape of the check
The detector is a table of labelled regular expressions and a function that returns every hit with some context around it:
def european_portuguese ( text ) : """[(label, snippet)] — the European Portuguese forms in `text`.""" out = [ ] for label , rx in PT_EUROPEAN : for m in re . finditer ( rx , text or "" , re . IGNORECASE | re .…