Commit 64a21aee authored by Michal Čihař's avatar Michal Čihař

Add hook script to add/remove units based on template from resx

Issue #113
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 9961bb97
...@@ -544,6 +544,12 @@ POST_UPDATE_SCRIPTS ...@@ -544,6 +544,12 @@ POST_UPDATE_SCRIPTS
List of scripts which are allowed as post update scripts. The script needs to be List of scripts which are allowed as post update scripts. The script needs to be
later enabled in the :ref:`component`. later enabled in the :ref:`component`.
Weblate comes with few example hook scripts which you might find useful:
:file:`examples/hook-update-resx`
Updates resx file to match template by adding new translations and removing
obsolete ones.
.. seealso:: .. seealso::
:ref:`processing` :ref:`processing`
......
#! /usr/bin/env python
"""
Post update hook script to update message IDs in the resx file.
"""
from __future__ import print_function
import glob
import os
import subprocess
import sys
from translate.storage.resx import RESXFile
def build_index(storage):
index = {}
for unit in storage.units:
index[unit.getid()] = unit
return index
def update_file(template, index, filename):
storage = RESXFile.parsefile(filename)
sindex = build_index(storage)
# Add missing units
for unit in template.units:
if unit.getid() not in sindex:
storage.addunit(unit, True)
# Remove extra units
for unit in storage.units:
if unit.getid() not in index:
storage.body.remove(unit.xmlelement)
storage.save()
def main():
if os.environ.get('WL_FILE_FORMAT') != 'resx':
print('Invalid file format!')
sys.exit(1)
if not os.environ.get('WL_TEMPLATE'):
print('Missing template!')
sys.exit(1)
if not os.environ.get('WL_FILEMASK'):
print('Missing filemask!')
sys.exit(1)
if not os.environ.get('WL_PATH'):
print('Missing path!')
sys.exit(1)
os.chdir(os.environ.get('WL_PATH'))
templatename = os.environ.get('WL_TEMPLATE')
template = RESXFile.parsefile(templatename)
index = build_index(template)
for filename in glob.glob(os.environ.get('WL_FILEMASK')):
if filename != templatename:
update_file(template, index, filename)
if subprocess.call(['git', 'commit', '--dry-run', '-a']) == 0:
ret = subprocess.call(
['git', 'commit', '-a', '-m', 'Updated translation files']
)
sys.exit(ret)
if __name__ == '__main__':
main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment