Skip to content

Instantly share code, notes, and snippets.

@BoboTiG
Last active July 28, 2016 20:01
Show Gist options
  • Save BoboTiG/bad9dcdc68c280d8686b451ce71fafa7 to your computer and use it in GitHub Desktop.
Save BoboTiG/bad9dcdc68c280d8686b451ce71fafa7 to your computer and use it in GitHub Desktop.
Find unused translations in BlogoText.
#!/usr/bin/env python
# coding: utf-8
#
# Find unused translations.
# Python 3.
#
from collections import OrderedDict
from glob import glob
import re
def find_used_translations(path_files):
''' Find uniq translations. '''
regexp = re.compile(r"\$GLOBALS\['lang']\['([\w]+)']")
translations = OrderedDict()
files = []
# Retrieve PHP files
for file in path_files:
if file.endswith('/'):
# A directory
for file in glob('{}/*.php'.format(file)):
files.append(file)
else:
files.append(file)
# Find used translations
for file in files:
with open(file, 'r') as fileh:
for trans in re.findall(regexp, fileh.read()):
if trans not in translations:
translations[trans] = 1
return (files, translations)
def find_translations(file):
''' Find defined translations. '''
regexp = re.compile(r"^'([\w]+)'")
translations = OrderedDict()
with open(file, 'r') as fileh:
for line in fileh.read().splitlines():
for trans in re.findall(regexp, line):
if trans not in translations:
translations[trans] = 1
return translations
def main():
paths = ['addons/*/', 'admin/', 'inc/', 'index.php', 'atom.php', 'rss.php']
files, translations = find_used_translations(paths)
print('Fichiers traités :', len(files))
print('Traductions utilisées :', len(translations))
print()
translations_fr = find_translations('inc/lang/fr_FR.php')
print('Traductions FR :', len(translations_fr))
translations_en = find_translations('inc/lang/en_EN.php')
print('Traductions EN :', len(translations_en))
# Find unused translations
print()
for translation in translations:
if translation in translations_fr:
del translations_fr[translation]
if translation in translations_en:
del translations_en[translation]
print('Traductions inutilisée FR :', len(translations_fr))
print('Traductions inutilisée EN :', len(translations_en))
# Print unused translations
num = 1
for translation in translations_fr:
if translation in translations_en:
del translations_en[translation]
print(num, 'FR|EN', translation)
else:
print(num, 'FR', translation)
num += 1
for translation in translations_en:
print(num, 'EN', translation)
num += 1
return 0
if __name__ == '__main__':
exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment