You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.4 KiB

#!/usr/bin/env python3
# this script takes one po file from the repo and compares it to the new file
# generated by lektor to see if there are new strings to add.
# written by emmapeel, sponsored by the Tor Project.
# this script is published under the GPL v3 license.
# requires debian packages: python3-polib, gettext
This script checks a directory with .po files for errors with links.
Translation errors with links are the most common errors on the
Tor Project websites.
Where branchname is the name of the translation branch and transifex component,
and [CONFIGS_INIT_FILE] the name of a language file different from
the one at `configs/i18n.ini`
import sys
import os
import requests
import logging
import polib
except ImportError:
logging.warning("You need to install python3-polib to use this program.")
BRANCHNAME = sys.argv[1]
except IndexError:
logging.warning("Please run me with a lektor-based translation branch, i.e. `../ 'communitytpo-contentspot'`")
logging.warning("the available branches are: communitytpo-contentspot, support-portal, tbmanual-contentspot, tpo-web, or gettor-website-contentspot")
CONFIG_FILE = sys.argv[2]
except IndexError:
CONFIG_FILE = 'configs/i18n.ini'
def get_langs():
'''gets a list of all locales configured on this website'''
mylangs = []
with open (CONFIG_FILE, 'rt') as config_file:
for config_option in config_file.readlines():
if config_option.startswith('translations'):
mylangs = [name.strip() for name in config_option.split('=')[1].split(',')]
print('configured languages: ' + str(mylangs))
return mylangs
def check_po_completion():
"looks on the enabled languages and counts translated strings"
"for each of them"
mylangs = get_langs()
for onelang in mylangs:
pofile = polib.pofile('i18n/contents+' + onelang + '.po')
print(' locale ' + onelang + ' : ' + str(pofile.percent_translated()) + ' %')
except OSError:
print(f"even less translations for {onelang}")
def get_remote_po():
we download a .po file from our translation repo so we can compare it to
the new file we have generated after building the website
po_link = f'{ BRANCHNAME }/contents+fr.po'
r = requests.get(po_link, allow_redirects=True)
open('oldcontents+fr.po', 'wb').write(r.content)
pofile = polib.pofile('oldcontents+fr.po')
except OSError:
print("Please run me with a lektor-based translation branch, i.e. `../ 'communitytpo-contentspot'`")
print("the available branches are: communitytpo-contentspot, support-portal, tbmanual-contentspot, tpo-web, or gettor-website-contentspot")
return pofile
def get_local_po():
''' this is the local file that may have been modified by the build'''
pofile = polib.pofile('i18n/contents+fr.po')
except OSError:
sys.exit("Please run me after building lektor at least once")
return pofile
def compare_pos(oldpo, newpo):
''' we compare both .po files to see if there are changes'''
newstrings, stalestrings = [], []
for entry in newpo:
if entry not in oldpo:
print(F'new string: {entry.msgid}')
for entry in oldpo:
if entry not in newpo:
print(F'stale string: {entry.msgid}')
return newstrings, stalestrings
# first we check how complete the enabled languages are
# now we look if there are any strings
# that are not available in transifex
# or any strings in transifex that are
# not needed anymore here
newstrings, stalestrings = compare_pos(get_remote_po(), get_local_po())
if newstrings != []:
logging.warning(F'There are {len(newstrings)} new strings that are not in Transifex')
if stalestrings != []:
logging.warning(F'There are {len(stalestrings)} strings in Transifex that are not needed anymore')
if newstrings != [] or stalestrings != []:
if newstrings == [] and stalestrings == []:
print('strings are synced!')