-
Notifications
You must be signed in to change notification settings - Fork 9
I18n add translation support for invenio-checks #49
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
base: master
Are you sure you want to change the base?
I18n add translation support for invenio-checks #49
Conversation
* Added missing translation entry for invenio_checks. * Updated mapping and output file keys to use underscores. * add keywords to catch aliases on extract msgs
* Wrap class-level attributes strings with lazy_gettext as stored values will be used later
* Introduces the English translation files structure for the project * Run extract_msgs command
* Change command prefix from 'python' to 'uv run' for consistency. * Update Transifex host URL to the correct endpoint. * Adjust section header for clarity in configuration.
1a5bb90 to
9bced7d
Compare
* its backward compatible with existing config * implement _translate_field function for i18n * update RuleResult to handle multilingual dict * add translation macro in metadata template * include translation tests
9f2e453 to
c8276df
Compare
* Implemented _getLocalizedMessage to handle string, object, and array inputs for localization. * Updated error handling to utilize the new method for better localization of error messages. * require inveniosoftware/invenio-checks#49
523102b to
a6e2b9f
Compare
* Fix display-time translation for dynamic language switching * Move translate_field from rules.py to utils.py for better separation * Update all test imports to use utils.translate_field * Maintain backward compatibility with string and dict fields * Store raw multilingual dictionaries instead of translated strings
a6e2b9f to
8a8d759
Compare
| try: | ||
| # Lazy imports to keep utils usable even when deps/context aren't ready | ||
| # pylint: disable=import-outside-toplevel | ||
| from flask import current_app |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in which situation is it necessary to place the imports here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lazy imports are necessary to prevent application context errors. This utils module might be imported during Docker builds, CLI scripts, or testing, all before Flask's application context exists. Without lazy imports, you'd get RuntimeError: Working outside of application context or import failures when the module is loaded outside a running Flask app. So I am trying to avoid that.
* Changed Dict to dict for consistency in type hints as suggested as dict works too since python3.9 * update copyrights
7dd6a15 to
d56fd79
Compare
❤️ Thank you for your contribution!
Description
This PR introduces backward-compatible multilingual support for rule configurations in invenio-checks. Rule titles, messages, and descriptions can now be specified in multiple languages using dictionary format while maintaining full backward compatibility with existing string-based configurations.
Changes
Core Features
title,message, anddescriptionnow support multilingual dictionariestranslate_field()function with intelligent fallback chaintranslate_fieldfilter for dynamic language change "macro didn't work"Translation Fallback Chain
current_i18n.locale)"en") as universal fallbackAPI Changes
translate_field()function ininvenio_checks/utils.py: Handles translation of multilingual fields with fallback{{ field | translate_field }}in templatesValueErrorConfiguration Examples
New Multilingual Format
{ "id": "access:open/publication", "title": { "en": "Open Access Publication", "sv": "Öppen tillgång publikation", "de": "Open-Access-Publikation" }, "message": { "en": "Publication articles must be Open Access", "sv": "Publikationsartiklar måste vara öppen tillgång", "de": "Publikationsartikel müssen Open Access sein" }, "description": { "en": "This rule ensures that all publication articles are Open Access.", "sv": "Denna regel säkerställer att alla publikationer är öppet tillgängliga.", "de": "Diese Regel stellt sicher, dass alle Publikationen Open Access sind." }, "level": "error" }Backward Compatible (still works)
{ "id": "simple-rule", "title": "Simple Rule Title", "message": "Simple string message", "level": "warning" }Mixed Format (also supported)
{ "id": "mixed-rule", "title": "String Title", "message": { "en": "English message", "sv": "Swedish message" }, "description": "String description" }Breaking Changes
None - This is backward compatible. Existing configurations work unchanged.
Replace string fields with language dictionaries:
Use the filter for custom templates:
{{ rule.title | translate_field }}Checklist
Ticks in all boxes and 🟢 on all GitHub actions status checks are required to merge:
Frontend
Reminder
By using GitHub, you have already agreed to the GitHub’s Terms of Service including that: