Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lang): migrate outdated plural definitions #12784

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Not yet released.

**Bug fixes**

* Update outdated plural definitions during the database migration.

**Compatibility**

**Upgrading**
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ dependencies = [
"translate-toolkit>=3.14.1,<3.15",
"translation-finder>=2.16,<3.0",
"user-agents>=2.0,<2.3",
"weblate-language-data>=2024.6",
"weblate-language-data>=2024.9",
"weblate-schemas==2024.1"
]
description = "A web-based continuous localization system with tight version control integration"
Expand Down
32 changes: 30 additions & 2 deletions weblate/lang/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import re
from collections import defaultdict
from gettext import c2py

Check failure on line 9 in weblate/lang/models.py

View workflow job for this annotation

GitHub Actions / mypy

Module "gettext" has no attribute "c2py"
from itertools import chain
from operator import itemgetter
from typing import TYPE_CHECKING
Expand All @@ -30,6 +30,7 @@
from weblate.checks.format import BaseFormatCheck
from weblate.checks.models import CHECKS
from weblate.lang import data
from weblate.lang.data import FORMULA_WITH_ZERO
from weblate.logger import LOGGER
from weblate.trans.defines import LANGUAGE_CODE_LENGTH, LANGUAGE_NAME_LENGTH
from weblate.trans.mixins import CacheKeyMixin
Expand Down Expand Up @@ -448,7 +449,7 @@
return self.prefetch_related("plural_set")


class LanguageManager(models.Manager.from_queryset(LanguageQuerySet)):

Check failure on line 452 in weblate/lang/models.py

View workflow job for this annotation

GitHub Actions / mypy

Unsupported dynamic base class "models.Manager.from_queryset"
use_in_migrations = True

def flush_object_cache(self) -> None:
Expand All @@ -460,7 +461,7 @@
"""Return English language object."""
return self.get(code=settings.DEFAULT_LANGUAGE, skip_cache=True)

def setup(self, update, logger=lambda x: x) -> None:
def setup(self, update, logger=lambda x: x) -> None: # noqa: C901
"""
Create basic set of languages.

Expand All @@ -472,7 +473,7 @@
# Invalidate cache, we might change languages
self.flush_object_cache()
languages = {language.code: language for language in self.prefetch()}
plurals: dict[str, dict[int, list[str]]] = {}
plurals: dict[str, dict[int, list[Plural]]] = {}
# Create Weblate languages
for code, name, nplurals, plural_formula in LANGUAGES:
population = POPULATION[code]
Expand Down Expand Up @@ -555,8 +556,35 @@
formula=plural_formula,
type=get_plural_type(lang.base_code, plural_formula),
)
plurals[code][source].append(plural)
logger(f"Created plural {plural_formula} for language {code}")

# CLDR and QT plurals should have just a single of them
if source != Plural.SOURCE_GETTEXT and len(plurals[code][source]) > 1:
logger(

Check warning on line 564 in weblate/lang/models.py

View check run for this annotation

Codecov / codecov/patch

weblate/lang/models.py#L564

Added line #L564 was not covered by tests
f"Removing extra {source} plurals for language {code} ({len(plurals[code][source])})!"
)
for extra_plural in plurals[code][source]:
if extra_plural != plural:
extra_plural.translation_set.update(plural=plural)
extra_plural.delete()
plurals[code][source] = [plural]

Check warning on line 571 in weblate/lang/models.py

View check run for this annotation

Codecov / codecov/patch

weblate/lang/models.py#L569-L571

Added lines #L569 - L571 were not covered by tests

# Sync FORMULA_WITH_ZERO
for code, language_plurals in plurals.items():
if Plural.SOURCE_CLDR_ZERO in language_plurals:
if Plural.SOURCE_CLDR not in language_plurals:
logger(f"Missing CLDR plural for {code}!")
continue
zero_plural = language_plurals[Plural.SOURCE_CLDR_ZERO][0]
cldr_plural = language_plurals[Plural.SOURCE_CLDR][0]
current_formula = FORMULA_WITH_ZERO[cldr_plural.formula]

Check warning on line 581 in weblate/lang/models.py

View check run for this annotation

Codecov / codecov/patch

weblate/lang/models.py#L577-L581

Added lines #L577 - L581 were not covered by tests
if zero_plural.formula != current_formula:
logger(f"Updating CLDR plural with zero for {code}")
zero_plural.formula = current_formula
zero_plural.number = cldr_plural.number + 1
zero_plural.save(update_fields=["formula", "number"])

Check warning on line 586 in weblate/lang/models.py

View check run for this annotation

Codecov / codecov/patch

weblate/lang/models.py#L583-L586

Added lines #L583 - L586 were not covered by tests

self._fixup_plural_types(logger)

def _fixup_plural_types(self, logger) -> None:
Expand Down
Loading