Skip to content

Commit

Permalink
fix(autofixes): skip devanagari danda fix for latin variants
Browse files Browse the repository at this point in the history
This also adds type annotation to autofixes.

Fixes #12517
  • Loading branch information
nijel committed Sep 19, 2024
1 parent 3be8ad2 commit da7279d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Not yet released.

* :ref:`mt-deepl` now supports specifying translation context.
* :ref:`mt-aws` now supports :ref:`glossary-mt`.
* :ref:`autofix` for Devanagari danda now better handles latin script.

**Bug fixes**

Expand Down
12 changes: 10 additions & 2 deletions weblate/trans/autofixes/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# Copyright © Michal Čihař <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from weblate.trans.models import Unit


class AutoFix:
"""Base class for AutoFixes."""
Expand All @@ -16,11 +22,13 @@ def get_identifier(self):
def get_related_checks():
return []

def fix_single_target(self, target, source, unit) -> tuple[str, bool]:
def fix_single_target(
self, target: str, source: str, unit: Unit
) -> tuple[str, bool]:
"""Fix a single target, implement this method in subclasses."""
raise NotImplementedError

def fix_target(self, target, unit):
def fix_target(self, target: str, unit: Unit) -> tuple[list[str], bool]:
"""Return a target translation array with a single fix applied."""
source_strings = unit.get_source_plurals()
if unit.translation.component.is_multivalue:
Expand Down
27 changes: 22 additions & 5 deletions weblate/trans/autofixes/chars.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import annotations

import re
from typing import TYPE_CHECKING

from django.utils.translation import gettext_lazy

Expand All @@ -15,6 +18,9 @@
from weblate.formats.helpers import CONTROLCHARS_TRANS
from weblate.trans.autofixes.base import AutoFix

if TYPE_CHECKING:
from weblate.trans.models import Unit


class ReplaceTrailingDotsWithEllipsis(AutoFix):
"""Replace trailing dots with an ellipsis."""
Expand All @@ -26,7 +32,9 @@ class ReplaceTrailingDotsWithEllipsis(AutoFix):
def get_related_checks():
return [EndEllipsisCheck()]

def fix_single_target(self, target, source, unit):
def fix_single_target(
self, target: str, source: str, unit: Unit
) -> tuple[str, bool]:
if source and source[-1] == "…" and target.endswith("..."):
return f"{target[:-3]}…", True
return target, False
Expand All @@ -42,7 +50,9 @@ class RemoveZeroSpace(AutoFix):
def get_related_checks():
return [ZeroWidthSpaceCheck()]

def fix_single_target(self, target, source, unit):
def fix_single_target(
self, target: str, source: str, unit: Unit
) -> tuple[str, bool]:
if unit.translation.language.base_code == "km":
return target, False
if "\u200b" not in source and "\u200b" in target:
Expand All @@ -56,7 +66,9 @@ class RemoveControlChars(AutoFix):
fix_id = "control-chars"
name = gettext_lazy("Control characters")

def fix_single_target(self, target, source, unit):
def fix_single_target(
self, target: str, source: str, unit: Unit
) -> tuple[str, bool]:
result = target.translate(CONTROLCHARS_TRANS)
return result, result != target

Expand All @@ -67,9 +79,12 @@ class DevanagariDanda(AutoFix):
fix_id = "devanadari-danda"
name = gettext_lazy("Devanagari danda")

def fix_single_target(self, target, source, unit):
def fix_single_target(
self, target: str, source: str, unit: Unit
) -> tuple[str, bool]:
if (
unit.translation.language.is_base(("hi", "bn", "or"))
and "_Latn" not in unit.translation.language.code
and source.endswith(".")
and target.endswith((".", "\u09f7", "|"))
):
Expand All @@ -87,7 +102,9 @@ class PunctuationSpacing(AutoFix):
def get_related_checks():
return [PunctuationSpacingCheck()]

def fix_single_target(self, target, source, unit):
def fix_single_target(
self, target: str, source: str, unit: Unit
) -> tuple[str, bool]:
if (
unit.translation.language.is_base(("fr", "br"))
and unit.translation.language.code != "fr_CA"
Expand Down

0 comments on commit da7279d

Please sign in to comment.