Skip to content

Commit

Permalink
unit: Move is_language helper to Language class
Browse files Browse the repository at this point in the history
It more belongs there rather than to the check.
  • Loading branch information
nijel committed Oct 27, 2023
1 parent b344eac commit 55ad30c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
4 changes: 0 additions & 4 deletions weblate/checks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ def check_chars(self, source, target, pos, chars):

return (src in chars) != (tgt in chars)

def is_language(self, unit, vals):
"""Detect whether language is in given list, ignores variants."""
return unit.translation.language.base_code in vals

def get_doc_url(self, user=None):
"""Return link to documentation."""
return get_doc_url("user/checks", self.doc_id, user=user)
Expand Down
40 changes: 20 additions & 20 deletions weblate/checks/chars.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,31 +154,31 @@ def check_single(self, source, target, unit):
if not target:
return False
# Thai and Lojban does not have a full stop
if self.is_language(unit, ("th", "jbo")):
if unit.translation.language.is_base(("th", "jbo")):
return False
# Allow ... to be translated into ellipsis
if source.endswith("...") and target[-1] == "…":
return False
if self.is_language(unit, ("ja",)) and source[-1] in (":", ";"):
if unit.translation.language.is_base(("ja",)) and source[-1] in (":", ";"):
# Japanese sentence might need to end with full stop
# in case it's used before list.
return self.check_chars(source, target, -1, (";", ":", ":", ".", "。"))
if self.is_language(unit, ("hy",)):
if unit.translation.language.is_base(("hy",)):
return self.check_chars(
source,
target,
-1,
(".", "。", "।", "۔", "։", "·", "෴", "។", ":", "՝", "?", "!", "`"),
)
if self.is_language(unit, ("hi", "bn", "or")):
if unit.translation.language.is_base(("hi", "bn", "or")):
# Using | instead of । is not typographically correct, but
# seems to be quite usual. \u0964 is correct, but \u09F7
# is also sometimes used instead in some popular editors.
return self.check_chars(source, target, -1, (".", "\u0964", "\u09F7", "|"))
if self.is_language(unit, ("sat",)):
if unit.translation.language.is_base(("sat",)):
# Santali uses "᱾" as full stop
return self.check_chars(source, target, -1, (".", "᱾"))
if self.is_language(unit, ("my",)):
if unit.translation.language.is_base(("my",)):
return self._check_my(source, target)
return self.check_chars(
source, target, -1, (".", "。", "।", "۔", "։", "·", "෴", "។", "።")
Expand Down Expand Up @@ -207,11 +207,11 @@ def _check_ja(self, source, target):
def check_single(self, source, target, unit):
if not source or not target:
return False
if self.is_language(unit, ("jbo",)):
if unit.translation.language.is_base(("jbo",)):
return False
if self.is_language(unit, ("hy",)):
if unit.translation.language.is_base(("hy",)):
return self._check_hy(source, target)
if self.is_language(unit, ("ja",)):
if unit.translation.language.is_base(("ja",)):
return self._check_ja(source, target)
return self.check_chars(source, target, -1, (":", ":", "៖"))

Expand Down Expand Up @@ -242,13 +242,13 @@ def _check_my(self, source, target):
def check_single(self, source, target, unit):
if not source or not target:
return False
if self.is_language(unit, ("jbo",)):
if unit.translation.language.is_base(("jbo",)):
return False
if self.is_language(unit, ("hy",)):
if unit.translation.language.is_base(("hy",)):
return self._check_hy(source, target)
if self.is_language(unit, ("el",)):
if unit.translation.language.is_base(("el",)):
return self._check_el(source, target)
if self.is_language(unit, ("my",)):
if unit.translation.language.is_base(("my",)):
return self._check_my(source, target)

return self.check_chars(
Expand All @@ -269,15 +269,15 @@ def check_single(self, source, target, unit):
if not source or not target:
return False
if (
self.is_language(unit, ("eu",))
unit.translation.language.is_base(("eu",))
and source[-1] == "!"
and "¡" in target
and "!" in target
):
return False
if self.is_language(unit, ("hy", "jbo")):
if unit.translation.language.is_base(("hy", "jbo")):
return False
if self.is_language(unit, ("my",)):
if unit.translation.language.is_base(("my",)):
return self.check_chars(source, target, -1, ("!", "႟"))
if source.endswith("Texy!") or target.endswith("Texy!"):
return False
Expand All @@ -296,7 +296,7 @@ class EndEllipsisCheck(TargetCheck):
def check_single(self, source, target, unit):
if not target:
return False
if self.is_language(unit, ("jbo",)):
if unit.translation.language.is_base(("jbo",)):
return False
# Allow ... to be translated into ellipsis
if source.endswith("...") and target[-1] == "…":
Expand Down Expand Up @@ -344,7 +344,7 @@ class ZeroWidthSpaceCheck(TargetCheck):
description = gettext_lazy("Translation contains extra zero-width space character")

def check_single(self, source, target, unit):
if self.is_language(unit, ("km",)):
if unit.translation.language.is_base(("km",)):
return False
if "\u200b" in source:
return False
Expand Down Expand Up @@ -381,7 +381,7 @@ class EndSemicolonCheck(TargetCheck):
)

def check_single(self, source, target, unit):
if self.is_language(unit, ("el",)) and source and source[-1] == "?":
if unit.translation.language.is_base(("el",)) and source and source[-1] == "?":
# Complement to question mark check
return False
return self.check_chars(
Expand Down Expand Up @@ -418,7 +418,7 @@ class PunctuationSpacingCheck(TargetCheck):

def check_single(self, source, target, unit):
if (
not self.is_language(unit, ("fr", "br"))
not unit.translation.language.is_base(("fr", "br"))
or unit.translation.language.code == "fr_CA"
):
return False
Expand Down
4 changes: 2 additions & 2 deletions weblate/checks/same.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ def should_skip(self, unit):
# Ignore the check for source language,
# English variants will have most things untranslated
# Interlingua is also quite often similar to English
if self.is_language(unit, source_language) or (
source_language == "en" and self.is_language(unit, ("en", "ia"))
if unit.translation.language.is_base(source_language) or (
source_language == "en" and unit.translation.language.is_base(("en", "ia"))
):
return True

Expand Down
2 changes: 1 addition & 1 deletion weblate/examples/check_czech.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PluralCzechCheck(TargetCheck):

# Real check code
def check_target_unit(self, sources, targets, unit):
if self.is_language(unit, ("cs",)):
if unit.translation.language.is_base(("cs",)):
return targets[1] == targets[2]
return False

Expand Down
10 changes: 7 additions & 3 deletions weblate/lang/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,11 +634,11 @@ def get_html(self):
return format_html('lang="{}" dir="{}"', self.code, self.direction)

@cached_property
def base_code(self):
def base_code(self) -> str:
return self.code.replace("_", "-").split("-")[0]

def uses_ngram(self):
return self.base_code in ("ja", "zh", "ko")
def uses_ngram(self) -> bool:
return self.is_base(("ja", "zh", "ko"))

@cached_property
def plural(self):
Expand All @@ -651,6 +651,10 @@ def plural(self):
def get_aliases_names(self):
return [alias for alias, codename in ALIASES.items() if codename == self.code]

def is_base(self, vals: tuple[str, ...]) -> bool:
"""Detect whether language is in given list, ignores variants."""
return self.base_code in vals


class PluralQuerySet(models.QuerySet):
def order(self):
Expand Down

0 comments on commit 55ad30c

Please sign in to comment.