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

Gettext i18n #561

Merged
merged 4 commits into from
Jan 23, 2025
Merged

Gettext i18n #561

merged 4 commits into from
Jan 23, 2025

Conversation

serpent213
Copy link
Contributor

A simple wrapper to invoke gettext if available.

Would solve #110, #559.

What you think of this implementation?

Missing: Tests, default .pot, maybe .po files, docs

@zachdaniel
Copy link
Collaborator

Looks great to me, I think this is pretty much the only way (and definitely the most standard way) to do it. @jimsynz what do you think?

@serpent213
Copy link
Contributor Author

One variation might be to let the user inject a generic translate(msg) function instead of a Gettext backend. Would allow for other translation libraries to be used.

@serpent213
Copy link
Contributor Author

Would suggest to go the latter route and not have Gettext as dependency. Instead extend the Igniter installer to check for Gettext availability and in case offer to setup the translate function and copy the language files.

@zachdaniel
Copy link
Collaborator

🤔 I hadn't considered it, but yeah it's a good point. The implementation would look roughly the same except you'd be fetching a translation function from config for the app in question (probably best at the app config level, i.e config :my_app, ...), instead of using gettext directly. We can then revisit for next steps, i.e to adjust the installer/provide pot/po files.

@clifinger
Copy link

Hello guys,

Do you need some help?

@serpent213
Copy link
Contributor Author

Hello guys,

Do you need some help?

Sure! 🙂

Manual testing: There might be some pieces of text left that need to be wrapped in _gettext() invocations to get translated (just added some more). Also, for manual tests I only have password strategy set up, so visual inspection of the other strategies would be great.

Language files: A number of untranslated strings are missing from the .pot file, would be great to complete those. Next step would be to collect translations to various languages.

@serpent213
Copy link
Contributor Author

🤔 I hadn't considered it, but yeah it's a good point. The implementation would look roughly the same except you'd be fetching a translation function from config for the app in question (probably best at the app config level, i.e config :my_app, ...), instead of using gettext directly. We can then revisit for next steps, i.e to adjust the installer/provide pot/po files.

I left the config under :ash_authentication_phoenix for now. How would you name the key under :my_app?

This variant requires a config like

config :ash_authentication_phoenix, gettext_fn: {MyAppWeb.Gettext, :translate}

with a translate fn in MyAppWeb.Gettext like

  def translate(msgid, bindings \\ []), do: Gettext.dgettext(__MODULE__, "auth", msgid, bindings)

@serpent213
Copy link
Contributor Author

Hello guys,

Do you need some help?

In general: just try it out and see if it works for you! 🙂

Apply the above config, copy the language files, point the dep to this branch:

{:ash_authentication_phoenix, git: "https://github.com/serpent213/ash_authentication_phoenix.git", branch: "gettext", override: true},

@serpent213 serpent213 force-pushed the gettext branch 2 times, most recently from 46cdd21 to 94756ed Compare January 19, 2025 13:40
@zachdaniel
Copy link
Collaborator

Small note: this should include #562 before being released.

@zachdaniel
Copy link
Collaborator

IIRC we pass the :otp_app in as an assign in the on mount hook

@jimsynz
Copy link
Collaborator

jimsynz commented Jan 20, 2025

Hey folks. Thank you for this!

🙌

The reason I didn't address it when building AAP was because I've never actually worked on an app with translations in place, so wanted to leave it until someone with more knowledge shows up rather than just getting it wrong based on my english-based assumptions.

I'm afraid I couldn't say whether this is the right approach or not - I'm just going to leave it up to you folks to decide amongst yourselves and let me know when to merge it 😄

@serpent213
Copy link
Contributor Author

Refactored it again: We carry around the gettext_fn (is there a better name?) in parallel to overrides now to every single component, so it can be set on component invocation or as well using the router macro. Feels like a more clean and flexible approach.

sign_in_route register_path: "/register",
  reset_path: "/reset",
  auth_routes_prefix: "/auth",
  gettext_fn: {AshAuthentication.Phoenix.Test.Helper, :gettext}

Will polish it a little, add some docs and we should be good to go.

@serpent213 serpent213 changed the title DRAFT: Gettext i18n Gettext i18n Jan 21, 2025
Provides `_gettext` macro to views, which leverages a user supplied
function for output text translation.

* `_gettext` macro
* Pass `gettext_fn` parallel to `overrides`
* Wrap all output texts in `_gettext`
* Gettext .pot/.po file templates in `/i18n` (included in package)
* Docs
* Doc clarification regarding password reset routes
* Tests for translation and basic test for `/password-reset`
@serpent213
Copy link
Contributor Author

Ready from my side.

Copy link
Collaborator

@zachdaniel zachdaniel left a comment

Choose a reason for hiding this comment

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

🔥

@zachdaniel
Copy link
Collaborator

Looks great to me!

@zachdaniel
Copy link
Collaborator

@jimsynz mind giving it a once over?

@zachdaniel zachdaniel merged commit 2e1acda into team-alembic:main Jan 23, 2025
@zachdaniel
Copy link
Collaborator

🚀 Thank you for your contribution! 🚀

@clifinger
Copy link

Thank you bro, it' s was really needed.
You did it too fast for me ...

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.

4 participants