Flo0807
Hello everyone!
I am developing a library that contains translations. Users of this library can define a gettext backend in the config.exs script used by my library. Gettext provides functions that take a backend as a parameter. I want to use these functions to translate text in my library, but the translations are not extracted by the mix gettext.extract task.
Suppose I have a module DemoWeb.Gettext with use Gettext in it:
If I have this function in my code, everything works as expected and the translations are extracted when the mix task runs:
DemoWeb.Gettext.dgettext("domain", "translation")
However, when I pass the backend as a parameter, the translation is not extracted:
Gettext.dgettext(DemoWeb.Gettext, "domain", "translation")
Is this because in this function, which accepts a dynamic backend, gettext does not know which backend is used at compile time, because it could be dynamic, and therefore cannot extract the translations?
Is there a better way to integrate gettext into a library or automatically extract translations with a backend defined in the config.exs script?
Thanks in advance. I appreciate any help!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kip
The
MyApp.Gettext.*_noopset of macros are designed to help in this situation. They mark the messages for extraction but do not otherwise include any code in your module.I think the right approach is:
MyLib.Gettext.dgettext_noop("domain, "message")for each message you want to be extracted.Gettext.dgettext(DemoWeb.Gettext, "domain", "translation")function as you normally would.This ends up with two calls per message but one is a macro invoked at compile time (to extract the messages only) and the other is a function invoked at runtime to perform the translation.
PJUllrich
Hey @kip, may I ask you to elaborate where you’d put each function in your application, please?
I’m currently trying to define messages for multiple domains, but to fetch the translation with a dynamic domain.
I came up with something like this, but I don’t like that I have to repeat the
dgettext_noop_with_backend/3call for every domain plus thedefaultdomain:If I refactor the three calls into a loop, I sadly receive the following error:
LostKobrakai
That for loop needs to go outside the
quote.PJUllrich
@LostKobrakai thank you! I tried that but then it executes the
Gettext.dgettext/3three times and I receive a triple string back!LostKobrakai
I’d always suggest writing the code you want to generate first, then try to write the macro that is meant to generate that code.
PJUllrich
Heck yeah! That worked! Thank you so much!
Just for completeness sake, here’s my solution: