Skip to content

Commit

Permalink
Document custom fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed May 6, 2013
1 parent c9ac074 commit bbe2593
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docs/admin.rst
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/config.rst
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions examples/fix_foo.py
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2013 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <http://weblate.org/>
#
# 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 <http://www.gnu.org/licenses/>.
#

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

0 comments on commit bbe2593

Please sign in to comment.