feat: ignore plurals when they're not translated (#8516)
This commit is contained in:
1 parent
0e6b9638b4
commit
ed402ec7e6
4 files changed
+143
-6
No files matched your search
@@ -75,3 +75,45 @@ LOCALE_CONFIG: dict[str, LocaleConfig] = {
|
||||
key="zh-TW", name="繁體中文 (Chinese traditional)", plural_food_handling=LocalePluralFoodHandling.NEVER
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def resolve_plural(*, source_singular: str, source_plural: str | None, singular: str, plural: str | None) -> str | None:
|
||||
"""
|
||||
Resolve a term's translated plural form, discarding it when the translation didn't really
|
||||
provide one.
|
||||
|
||||
An empty plural means "this term has no plural form", but Crowdin can't store an empty
|
||||
translation, so translators work around it either by repeating the singular or by leaving
|
||||
the source's English plural untouched. Both of those resolve to None.
|
||||
|
||||
The source arguments are the same term in the source locale (en-US).
|
||||
"""
|
||||
# nothing to keep, or a plural that says the same thing as the singular
|
||||
if not plural or plural == singular:
|
||||
return None
|
||||
|
||||
# the whole term fell back to the source locale, so its plural is in the right language
|
||||
if singular == source_singular:
|
||||
return plural
|
||||
|
||||
# the singular was translated but the plural wasn't, so the plural is in the wrong language
|
||||
if plural == source_plural:
|
||||
return None
|
||||
|
||||
return plural
|
||||
|
||||
|
||||
def resolve_food_plural(
|
||||
*, source_singular: str, source_plural: str | None, singular: str, plural: str | None, locale: str | None
|
||||
) -> str | None:
|
||||
"""
|
||||
Resolve a food's translated plural form for a locale, honoring locales that never pluralize
|
||||
foods at all. Otherwise identical to resolve_plural.
|
||||
"""
|
||||
config = LOCALE_CONFIG.get(locale) if locale else None
|
||||
if config and config.plural_food_handling is LocalePluralFoodHandling.NEVER:
|
||||
return None
|
||||
|
||||
return resolve_plural(
|
||||
source_singular=source_singular, source_plural=source_plural, singular=singular, plural=plural
|
||||
)
|
||||
@@ -1,5 +1,6 @@
|
||||
import json
|
||||
from abc import ABC, abstractmethod
|
||||
from functools import cached_property
|
||||
from logging import Logger
|
||||
from pathlib import Path
|
||||
|
||||
@@ -13,6 +14,7 @@ class AbstractSeeder(ABC):
|
||||
"""
|
||||
|
||||
resources = Path(__file__).parent / "resources"
|
||||
source_locale = "en-US"
|
||||
|
||||
def __init__(self, db: AllRepositories, logger: Logger | None = None):
|
||||
"""
|
||||
@@ -31,5 +33,13 @@ class AbstractSeeder(ABC):
|
||||
def load_file(self, file: Path) -> dict[str, dict]:
|
||||
return json.loads(file.read_text(encoding="utf-8"))
|
||||
|
||||
@cached_property
|
||||
def source_data(self) -> dict[str, dict]:
|
||||
"""
|
||||
The seed data in the source locale, read once per seeder. A translated locale file is
|
||||
compared against it to tell a real translation apart from an untranslated fallback.
|
||||
"""
|
||||
return self.load_file(self.get_file(self.source_locale))
|
||||
|
||||
@abstractmethod
|
||||
def seed(self, locale: str | None = None) -> None: ...
|
||||
@@ -2,6 +2,7 @@ import pathlib
|
||||
from collections.abc import Generator
|
||||
from functools import cached_property
|
||||
|
||||
from mealie.lang.locale_config import resolve_food_plural, resolve_plural
|
||||
from mealie.schema.labels import MultiPurposeLabelOut, MultiPurposeLabelSave
|
||||
from mealie.schema.recipe.recipe_ingredient import (
|
||||
IngredientFood,
|
||||
@@ -28,18 +29,31 @@ class IngredientUnitsSeeder(AbstractSeeder):
|
||||
file = self.get_file(locale)
|
||||
|
||||
seen_unit_names = {unit.name for unit in self.get_all_units()}
|
||||
for unit in self.load_file(file).values():
|
||||
for key, unit in self.load_file(file).items():
|
||||
if unit["name"] in seen_unit_names:
|
||||
continue
|
||||
|
||||
seen_unit_names.add(unit["name"])
|
||||
|
||||
# an unrecognized key has nothing to compare against, so treat it as untranslated
|
||||
source = self.source_data.get(key, unit)
|
||||
yield SaveIngredientUnit(
|
||||
group_id=self.repos.group_id,
|
||||
name=unit["name"],
|
||||
plural_name=unit.get("plural_name"),
|
||||
plural_name=resolve_plural(
|
||||
source_singular=source["name"],
|
||||
source_plural=source.get("plural_name"),
|
||||
singular=unit["name"],
|
||||
plural=unit.get("plural_name"),
|
||||
),
|
||||
description=unit["description"],
|
||||
abbreviation=unit["abbreviation"],
|
||||
plural_abbreviation=unit.get("plural_abbreviation"),
|
||||
plural_abbreviation=resolve_plural(
|
||||
source_singular=source["abbreviation"],
|
||||
source_plural=source.get("plural_abbreviation"),
|
||||
singular=unit["abbreviation"],
|
||||
plural=unit.get("plural_abbreviation"),
|
||||
),
|
||||
)
|
||||
|
||||
def seed(self, locale: str | None = None) -> None:
|
||||
@@ -89,19 +103,29 @@ class IngredientFoodsSeeder(AbstractSeeder):
|
||||
# de-duplicate on the localized name rather than the English seed key, otherwise seeding
|
||||
# a second locale skips every food whose English key already exists in the group
|
||||
seen_foods_names = {food.name for food in self.get_all_foods()}
|
||||
for values in self.load_file(file).values():
|
||||
for label_key, values in self.load_file(file).items():
|
||||
label_out = self.get_label(values["name"])
|
||||
source_foods = self.source_data.get(label_key, {}).get("foods", {})
|
||||
|
||||
for attributes in values["foods"].values():
|
||||
for food_key, attributes in values["foods"].items():
|
||||
name = attributes["name"]
|
||||
if name in seen_foods_names:
|
||||
continue
|
||||
|
||||
seen_foods_names.add(name)
|
||||
|
||||
# an unrecognized key has nothing to compare against, so treat it as untranslated
|
||||
source = source_foods.get(food_key, attributes)
|
||||
yield SaveIngredientFood(
|
||||
group_id=self.repos.group_id,
|
||||
name=name,
|
||||
plural_name=attributes.get("plural_name") or None,
|
||||
plural_name=resolve_food_plural(
|
||||
source_singular=source["name"],
|
||||
source_plural=source.get("plural_name"),
|
||||
singular=name,
|
||||
plural=attributes.get("plural_name"),
|
||||
locale=locale,
|
||||
),
|
||||
description="", # description expected to be empty string by UnitFoodBase class
|
||||
label_id=label_out.id if label_out and label_out.id else None,
|
||||
)
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import pytest
|
||||
|
||||
from mealie.lang.locale_config import resolve_food_plural, resolve_plural
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"source_singular, source_plural, singular, plural, expected",
|
||||
[
|
||||
pytest.param("onion", "onions", "cebolla", "cebollas", "cebollas", id="translated-plural-is-kept"),
|
||||
pytest.param("onion", "onions", "onion", "onions", "onions", id="untranslated-term-keeps-its-plural"),
|
||||
pytest.param("garlic", "", "ajo", "", None, id="no-plural-in-either-locale"),
|
||||
pytest.param("garlic", None, "ajo", None, None, id="missing-plural-key"),
|
||||
# Crowdin can't store an empty translation, so translators repeat the singular instead
|
||||
pytest.param("teaspoon", "teaspoons", "茶匙", "茶匙", None, id="plural-repeating-its-own-singular"),
|
||||
# ...or they leave the source plural untouched, which leaves it in the wrong language
|
||||
pytest.param("cup", "cups", "컵", "cups", None, id="translated-singular-with-source-plural"),
|
||||
pytest.param("pound", "lbs", "libra", "lbs", None, id="translated-abbreviation-with-source-plural"),
|
||||
# a term the source doesn't pluralize may still pluralize once translated
|
||||
pytest.param("celery", "", "sellerie", "sellerien", "sellerien", id="plural-added-by-the-translation"),
|
||||
# a plural that legitimately matches the source is dropped; falling back to the singular
|
||||
# is safer than rendering the source language
|
||||
pytest.param("eggplant", "eggplants", "aubergine", "eggplants", None, id="plural-matching-source-is-dropped"),
|
||||
],
|
||||
)
|
||||
def test_resolve_plural(
|
||||
source_singular: str, source_plural: str | None, singular: str, plural: str | None, expected: str | None
|
||||
) -> None:
|
||||
assert (
|
||||
resolve_plural(source_singular=source_singular, source_plural=source_plural, singular=singular, plural=plural)
|
||||
== expected
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"locale, expected",
|
||||
[
|
||||
pytest.param("de-DE", "zwiebeln", id="always-pluralizes"),
|
||||
pytest.param("en-US", "zwiebeln", id="pluralizes-without-unit"),
|
||||
pytest.param("ja-JP", None, id="never-pluralizes"),
|
||||
pytest.param("zh-TW", None, id="never-pluralizes-traditional-chinese"),
|
||||
pytest.param(None, "zwiebeln", id="no-locale-falls-back-to-the-string-rules"),
|
||||
pytest.param("xx-XX", "zwiebeln", id="unknown-locale-falls-back-to-the-string-rules"),
|
||||
],
|
||||
)
|
||||
def test_resolve_food_plural_honors_locale(locale: str | None, expected: str | None) -> None:
|
||||
assert (
|
||||
resolve_food_plural(
|
||||
source_singular="onion", source_plural="onions", singular="zwiebel", plural="zwiebeln", locale=locale
|
||||
)
|
||||
== expected
|
||||
)
|
||||
|
||||
|
||||
def test_resolve_food_plural_never_locale_drops_untranslated_plurals() -> None:
|
||||
"""A locale that never pluralizes foods drops the plural even when the term is untranslated."""
|
||||
assert (
|
||||
resolve_food_plural(
|
||||
source_singular="onion", source_plural="onions", singular="onion", plural="onions", locale="ja-JP"
|
||||
)
|
||||
is None
|
||||
)
|
||||
Reference in new issue
Block a user