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

Backend Localization #218

Merged
merged 19 commits into from
Jan 9, 2024
Merged

Backend Localization #218

merged 19 commits into from
Jan 9, 2024

Conversation

MelissaAutumn
Copy link
Member

@MelissaAutumn MelissaAutumn commented Dec 18, 2023

Localization! I hear it's neat. Here's the initial pass at adding fluent-based localization to backend. Feel free to be opinionated on this stuff. If you think strings need to be pulled into separate files based on category let me know! Also let me know if I missed any strings.

Breaking Changes:

  • You now have to install the backend in edit mode for tests to run. Simply run cd backend && pip install -e .
  • Tests are now ran from /backend instead of /backend/test

Additions:

  • Added starlette_context to allow per-request global data
  • Added the l10n helper that accepts a msg-id and optionally a dictionary of data for that message.
  • Added support for English and German in the backend (Note: We'll switch to en-US in the future.)
  • Added a bunch of fluent strings to the file under main.ftl and email.ftl
  • Added a variety of API exceptions to exceptions/validation.py

Notes:

  • Read up on fluent's file format: https://projectfluent.org/
  • There's a lot of duplicate strings for things like model not found. This is based on the advice of https://github.com/projectfluent/fluent/wiki/Good-Practices-for-Developers#prefer-wet-over-dry I'm sure I'm taking that a little too literally so we can work on that in the future.
  • I think any API exception that appears more than once can have a exception class. We can fine-tune this and split things out into other files as needed though.
  • I went with TB's commenting style for variables.
  • Currently the health check with English and German strings are used for a test to ensure l10n helper function is working. We might want to create a specific test route and test strings for this though.
  • I intentionally didn't use _ as the localization helper to avoid association with gettext (as the call is a bit different.)
  • In the future we might want to store user's preferred locale in the db. But when we roll fluent out on the frontend we can just override accept-languages for now. (At least until we get a worker/queue system for emails.)

 - Adds starlette_context so we can safely have per-request data
 - Adds a middleware to read accept-language and load fluent
 - Adds a `l10n` helper function to localize a given key
 - Adjusts the health check to return a distinct string between locales (we use this for tests...)
 - A couple of string -> fluent conversions
This requires a local `-e` build for tests to work.
@MelissaAutumn MelissaAutumn self-assigned this Dec 18, 2023
@MelissaAutumn MelissaAutumn changed the title Features/84 fluent [Draft] Backend Localization Dec 19, 2023

# Should indicate application wellness.
# (This was google translated, sorry.)
health-ok = Das System ist betriebsbereit
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha it's alright 👍🏻 I'll look over German translation when this PR is out of draft state

@MelissaAutumn MelissaAutumn changed the title [Draft] Backend Localization Backend Localization Dec 21, 2023
@MelissaAutumn
Copy link
Member Author

And it should be good! Feel free to wait until the new year 😅.

Copy link
Collaborator

@devmount devmount left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. I added small bug fixes and the German translation. Although _() would have been shorter an cleaner, I quickly got used to l10n(). Thank you, Mel!

@MelissaAutumn MelissaAutumn merged commit e3abe71 into main Jan 9, 2024
1 check passed
@MelissaAutumn MelissaAutumn deleted the features/84-fluent branch January 9, 2024 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants