diff --git a/docs/admin.rst b/docs/admin.rst index 257cb7d8824d..1bc15feb3d0a 100644 --- a/docs/admin.rst +++ b/docs/admin.rst @@ -588,6 +588,23 @@ languages using ``dictionary`` Python module: You can list own class in :setting:`MACHINE_TRANSLATION_SERVICES` and Weblate will start using that. +.. _custom-autofix: + +Custom automatic fixups +----------------------- + +You can also implement own automatic fixup in addition to standard ones and +include them in :setting:`AUTOFIX_LIST`. + +The automatic fixes are powerful, but can also cause damage, be careful when +writing one. + +For example following automatic fixup would replace every occurence of string +``foo`` in translation with ``bar``: + +.. literalinclude:: ../examples/fix_foo.py + :language: python + .. _custom-checks: Customizing checks diff --git a/docs/config.rst b/docs/config.rst index 70a5802971f3..27b807431906 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -75,6 +75,8 @@ For example you can enable only few of them: 'trans.autofixes.chars.ReplaceTrailingDotsWithEllipsis', ) +.. seealso:: :ref:`autofix`, :ref:`custom-autofix` + .. setting:: BACKGROUND_HOOKS BACKGROUND_HOOKS diff --git a/examples/fix_foo.py b/examples/fix_foo.py new file mode 100644 index 000000000000..6a4e3c8d8588 --- /dev/null +++ b/examples/fix_foo.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +# +# Copyright © 2012 - 2013 Michal Čihař +# +# This file is part of Weblate +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +from trans.autofixes.base import AutoFix +from django.utils.translation import ugettext_lazy as _ + + +class ReplaceFooWithBar(AutoFix): + ''' + Replaces foo with bar. + ''' + + name = _('Foobar') + + def fix_single_target(self, target, source, unit): + if 'foo' in target: + return target.replace('foo', 'bar'), True + return target, False