Initial commit

This commit is contained in:
Julien Palard 2017-08-11 19:53:34 +02:00
commit dbbe502e37
5 changed files with 229 additions and 0 deletions

62
.gitignore vendored Normal file
View File

@ -0,0 +1,62 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# pyenv python configuration file
.python-version

33
README.rst Normal file
View File

@ -0,0 +1,33 @@
=======
pomerge
=======
.. image:: https://img.shields.io/pypi/v/pomerge.svg
:target: https://pypi.python.org/pypi/pomerge
Script to merge translations from a set of po files to other set of po files.
Usage
-----
To merge translations from contributors to your files::
pomerge --from ../contributors/*.po ../contributors/**/*.po --to *.po **/*.po
To merge translations from inside a single repository, usefull when simple
strings can appear in multiple .po files::
pomerge --from *.po **.*.po --to *.po **/*.po
Note that ``pomerge`` does not care about po file names, a translation
from one file can land in another as long as their msgid are identical.
The style in your ``.po`` files may change a lot, completly destroying
the readability of git diffs, to fix this I use
`poindent <https://pypi.python.org/pypi/poindent>`_.
* Free software: MIT license

0
pomerge/__init__.py Normal file
View File

97
pomerge/pomerge.py Executable file
View File

@ -0,0 +1,97 @@
#!/usr/bin/env python3
"""Merge translations from one or many po files into one or many others.
"""
import sys
from difflib import SequenceMatcher as SM
import polib
from tqdm import tqdm
def find_best_match(possibilities, to_find):
best_match = (0, None)
for possibility in [possib for possib in possibilities
if possib[:5] == to_find[:5]]:
match = SM(None, possibility, to_find).ratio()
if match > best_match[0]:
best_match = (match, possibility)
if best_match[0] > .9:
return best_match[1]
def find_translations(files):
known_translations = {}
# Aggregate all known translations
for po_file in tqdm(files, desc="Searching translations"):
try:
po_file = polib.pofile(po_file)
except OSError as err:
print("Skipping {}: {}".format(po_file, str(err)), sys.stderr)
continue
for entry in po_file:
if 'fuzzy' not in entry.flags and entry.msgstr != '':
known_translations[entry.msgid] = entry.msgstr
return known_translations
def write_translations(translations, files, fuzzy=False):
for po_file in tqdm(files, desc="Updating translations"):
po_file = polib.pofile(po_file)
for entry in po_file:
if entry.msgid in translations:
entry.msgstr = translations[entry.msgid]
if 'fuzzy' in entry.flags:
entry.flags.remove('fuzzy')
elif fuzzy:
candidate = find_best_match(list(translations.keys()),
entry.msgid)
if candidate:
entry.msgstr = translations[candidate]
entry.flags.append('fuzzy')
po_file.save()
def merge_po_files(from_files, to_files, fuzzy=False):
"""Find known translations from each given files in from_files,
and update them in files in to_files.
"""
translations = find_translations(from_files)
write_translations(translations, to_files, fuzzy)
def main():
import argparse
parser = argparse.ArgumentParser(
description="""Replicate known translations between sets of po files.
To propagate known translation in a single set of po files,
give it as a source and a destination, like:
pomerge --from *.po --to *.po
Translations already existing in the destinations po files will be
updated by translations from the source po files.
To find po files recursively, use the globstar option of bash, or your
shell equivalent, like:
shopt -s globstar
pomerge --from *.po **/*.po --to *.po **/*.po
""")
parser.add_argument(
'--fuzzy', action='store_true',
help='Also replicate nearly identical strings, '
'but when doing so, add a fuzzy flag.')
parser.add_argument(
'--from-files', '-f', nargs='+',
help='File in which known translations are searched')
parser.add_argument(
'--to-files', '-t', nargs='+',
help='File in which translations will be added or updated')
args = parser.parse_args()
merge_po_files(args.from_files, args.to_files, args.fuzzy)
if __name__ == '__main__':
main()

37
setup.py Normal file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from setuptools import setup
with open('README.rst') as readme_file:
readme = readme_file.read()
setup(
name='pomerge',
version='0.1.0',
description="Merge known translations between .po files.",
long_description=readme,
author="Julien Palard",
author_email='julien@palard.fr',
url='https://github.com/JulienPalard/pomerge',
packages=['pomerge',],
package_dir={'pomerge': 'pomerge'},
entry_points={
'console_scripts': ['pomerge=pomerge.pomerge:main']
},
include_package_data=True,
install_requires=['tqdm', 'polib'],
license="MIT license",
zip_safe=False,
keywords='pomerge',
classifiers=[
'Development Status :: 2 - Pre-Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Natural Language :: English',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6'
]
)