Skip to content

fix(notifications): translate notification emails and messages into recipient user locale (#11408) - #12944

Merged
SchrodingersGat merged 2 commits into
inventree:masterfrom
xyz-is-best:fix/notification-locale-translation
Sep 29, 2026
Merged

SchrodingersGat merged 2 commits into
inventree:masterfrom
xyz-is-best:fix/notification-locale-translation

Conversation

@xyz-is-best

Copy link
Copy Markdown
Contributor

Summary of Changes

Fixes #11408 by ensuring notification emails and UI messages are translated into each recipient user's configured language preference rather than being "baked in" with the server process default locale.

Root Cause

Previously, in InvenTreeEmailNotifications.send_notification():

  1. html_message = render_to_string(...) and subject were rendered once globally outside the user loop using whatever locale happened to be active on the worker process (typically English).
  2. All recipients received this identical rendered message regardless of their personal language preference (user.profile.language or InvenTreeUserSetting).
  3. Similarly, UI notification messages stored unlocalized strings upon creation.

Key Fixes

  1. User Locale Resolution (common/notifications.py):
    • Added get_user_language(user) helper to inspect user language preferences in priority order:
      • user.profile.language / user.user_profile.language
      • InvenTreeUserSetting.get_setting('LANGUAGE', user=user)
      • Fallback to settings.LANGUAGE_CODE.
  2. Per-Locale Email Batching (core_notifications.py):
    • Group active recipients by their preferred language code (defaultdict(list)).
    • For each language group, activate the locale context via django.utils.translation.override(user_lang).
    • Re-render the subject and HTML body with localized strings and templates.
    • Batch-send to all recipients sharing that locale for optimal SMTP efficiency.
  3. UI Notifications Localization (core_notifications.py):
    • Ensure notification messages and names resolve through active locale overrides when created.

Verification & Testing

Added hermetic test suite in src/backend/InvenTree/common/test_notifications.py:

  • test_email_notifications_locale_translation: Mocks InvenTree.helpers_email.send_email and asserts:
    • English recipient receives English email subject and body ("Items Received").
    • Italian recipient receives translated Italian subject and body ("Elemento ricevuto").
  • test_email_notifications_single_locale_batching: Verifies recipients sharing a locale are dispatched in a single batch.
  • test_ui_notifications_locale_translation: Verifies NotificationMessage records are created with user-specific translations.
  • test_email_notifications_no_recipients: Verifies edge-case handling for missing templates and empty recipient lists.

Test Results:

  • pytest src/backend/InvenTree/common/test_notifications.py -> 5 passed, 0 failed
  • uv run ruff check -> 0 errors
  • uv run ruff format --check -> Clean

@netlify

netlify Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for inventree-web-pui-preview canceled.

Name Link
🔨 Latest commit 9edde3e
🔍 Latest deploy log https://app.netlify.com/projects/inventree-web-pui-preview/deploys/6abadaa388642b000812bc60

@matmair matmair added enhancement This is an suggested enhancement or new feature User Interface Related to the frontend / User Interface labels Sep 28, 2026
@matmair matmair added this to the 1.6.0 milestone Sep 28, 2026
@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.32353% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.32%. Comparing base (c2b7f71) to head (9edde3e).

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12944      +/-   ##
==========================================
+ Coverage   87.31%   87.32%   +0.01%     
==========================================
  Files        1508     1509       +1     
  Lines      103755   103880     +125     
  Branches    11790    11790              
==========================================
+ Hits        90591    90711     +120     
- Misses      13098    13103       +5     
  Partials       66       66              
Flag Coverage Δ
backend 92.09% <96.32%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Backend Apps 92.71% <96.32%> (+<0.01%) ⬆️
Backend General 94.95% <95.19%> (+<0.01%) ⬆️
Frontend 79.56% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@SchrodingersGat

Copy link
Copy Markdown
Member

@xyz-is-best thanks for the contribution

@SchrodingersGat
SchrodingersGat merged commit 65349ee into inventree:master Sep 29, 2026
60 of 61 checks passed
SchrodingersGat added a commit that referenced this pull request Nov 4, 2026
- The 'LANGUAGE" user setting does not exist
- Introduced in #12944
SchrodingersGat added a commit that referenced this pull request Nov 4, 2026
- The 'LANGUAGE" user setting does not exist
- Introduced in #12944

(cherry picked from commit ec5d632)

Co-authored-by: Oliver <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement This is an suggested enhancement or new feature User Interface Related to the frontend / User Interface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Notification locale translation

3 participants