From 8fd7f7e4df63c908f63e4f9bb1e81fc862fd7d1d Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Wed, 13 May 2026 17:26:15 -0500 Subject: [PATCH] fix: store i18n key for Recipe Created event, translate at serve time The recipe-created timeline event was always stored as the translated English string "Recipe Created", regardless of the request locale. Now the i18n key "recipe.recipe-created" is stored in the database. The GET endpoints for timeline events translate system event subjects using the locale from the request's Accept-Language header, so the correct translation is returned for any supported locale. Old events that already contain English strings are returned as-is (backward- compatible). Closes #4497 --- mealie/routes/recipe/timeline_events.py | 10 ++++- mealie/services/recipe/recipe_service.py | 2 +- .../test_recipe_timeline_events.py | 37 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/mealie/routes/recipe/timeline_events.py b/mealie/routes/recipe/timeline_events.py index 7894e4626..a2c33cba2 100644 --- a/mealie/routes/recipe/timeline_events.py +++ b/mealie/routes/recipe/timeline_events.py @@ -15,6 +15,7 @@ from mealie.schema.recipe.recipe_timeline_events import ( RecipeTimelineEventPagination, RecipeTimelineEventUpdate, TimelineEventImage, + TimelineEventType, ) from mealie.schema.recipe.request_helpers import UpdateImageResponse from mealie.schema.response.pagination import PaginationQuery @@ -50,6 +51,10 @@ class RecipeTimelineEventsController(BaseCrudController): override=RecipeTimelineEventOut, ) + for event in response.items: + if event.event_type == TimelineEventType.system.value: + event.subject = self.t(event.subject) + response.set_pagination_guides(router.url_path_for("get_all"), q.model_dump()) return response @@ -83,7 +88,10 @@ class RecipeTimelineEventsController(BaseCrudController): @router.get("/{item_id}", response_model=RecipeTimelineEventOut) def get_one(self, item_id: UUID4): - return self.mixins.get_one(item_id) + event = self.mixins.get_one(item_id) + if event.event_type == TimelineEventType.system.value: + event.subject = self.t(event.subject) + return event @router.put("/{item_id}", response_model=RecipeTimelineEventOut) def update_one(self, item_id: UUID4, data: RecipeTimelineEventUpdate): diff --git a/mealie/services/recipe/recipe_service.py b/mealie/services/recipe/recipe_service.py index 90eaebeb7..ff3a2fced 100644 --- a/mealie/services/recipe/recipe_service.py +++ b/mealie/services/recipe/recipe_service.py @@ -224,7 +224,7 @@ class RecipeService(RecipeServiceBase): timeline_event_data = RecipeTimelineEventCreate( user_id=new_recipe.user_id, recipe_id=new_recipe.id, - subject=self.t("recipe.recipe-created"), + subject="recipe.recipe-created", event_type=TimelineEventType.system, timestamp=new_recipe.created_at or datetime.now(UTC), ) diff --git a/tests/integration_tests/user_recipe_tests/test_recipe_timeline_events.py b/tests/integration_tests/user_recipe_tests/test_recipe_timeline_events.py index 46eefce83..1af090a3e 100644 --- a/tests/integration_tests/user_recipe_tests/test_recipe_timeline_events.py +++ b/tests/integration_tests/user_recipe_tests/test_recipe_timeline_events.py @@ -8,6 +8,7 @@ from mealie.schema.recipe.recipe_timeline_events import ( RecipeTimelineEventOut, RecipeTimelineEventPagination, TimelineEventImage, + TimelineEventType, ) from mealie.schema.recipe.request_helpers import UpdateImageResponse from tests.utils import api_routes @@ -341,6 +342,42 @@ def test_create_recipe_with_timeline_event( assert events_pagination.items +def test_recipe_created_system_event_is_translated( + api_client: TestClient, + unique_user: TestUser, + recipes: list[Recipe], +): + recipe = recipes[0] + params = {"queryFilter": f"recipe_id={recipe.id}"} + + # fetch events in French — the system "recipe created" event should be translated + fr_headers = {**unique_user.token, "Accept-Language": "fr-FR"} + events_response = api_client.get(api_routes.recipes_timeline_events, params=params, headers=fr_headers) + assert events_response.status_code == 200 + events_pagination = RecipeTimelineEventPagination.model_validate(events_response.json()) + + system_events = [e for e in events_pagination.items if e.event_type == TimelineEventType.system.value] + assert system_events, "expected at least one system event for a newly created recipe" + + for event in system_events: + assert event.subject == "Recette créée", f"expected French translation, got: {event.subject!r}" + + # also verify the individual GET endpoint translates correctly + single_response = api_client.get(api_routes.recipes_timeline_events_item_id(event.id), headers=fr_headers) + assert single_response.status_code == 200 + single_event = RecipeTimelineEventOut.model_validate(single_response.json()) + assert single_event.subject == "Recette créée" + + # fetch the same events in English — subject should be the English string + en_headers = {**unique_user.token, "Accept-Language": "en-US"} + events_response = api_client.get(api_routes.recipes_timeline_events, params=params, headers=en_headers) + events_pagination = RecipeTimelineEventPagination.model_validate(events_response.json()) + + system_events = [e for e in events_pagination.items if e.event_type == TimelineEventType.system.value] + for event in system_events: + assert event.subject == "Recipe Created", f"expected English string, got: {event.subject!r}" + + @pytest.mark.parametrize("use_other_household_user", [True, False]) def test_invalid_recipe_id( api_client: TestClient, unique_user: TestUser, h2_user: TestUser, use_other_household_user: bool