Optionally warn about missing translations
Functions like dgettext/4 and dngettext/4 silently fall back to the transalation in default.po if a translation doesn't exist in the PO file of the given domain.
Would it be possible to throw a warning or error if Gettext silently falls back to a default translation? Just optionally, if e.g. a configuration flag is set.
My current problem is that I have to create a domain PO file by hand because I'm dynamically fetching the translation based on some user configuration. I don't want to translate all entries from the default.po though, but only a few key words that are different between e.g. organizations. This means that I can't use (I think) mix gettext to extract the domain-specific translations and I have to manually find and add all domain-specific words. However, I can run the phoenix project and would like Gettext to throw a warning whenever it doesn't find a domain-specific keyword in the domain.po file. This way, I could quickly identify the missing translations.
Thank you for your help :)
Hey Peter 👋 , so this would be a local development kind of configuration?
Hello 👋 yes that’s right. It would help to find missing translations.
Mh we could do this through a config option that is enabled by default in :dev, sort of like how Phoenix code reloading works. @maennchen thoughts here?
Having that option sounds like a great addition to the library.
I've seen multiple behaviors so far with translation tooling in other languages on missing translations:
- Fallback to msgid
- and optionally warn
- Fallback to default locale
- and optionally warn
- Pseudolocalization
- eg. something like
⟦Ȟêĺĺø %{name}~~~~~~⟧ - and optionally warn
- Prior art:
gettext_pseudolocalizeandpseudoloc)
- eg. something like
- Error
I would however not decide based on the env. Possibly I'd like a warning in prod as well for example.
We could do it in the config:
config :gettext,
on_missing_translation: option
Or when creating the backend:
defmodule MyApp.Gettext do
use Gettext.Backend,
otp_app: :my_app,
on_missing_translation: option
end
Where option is :fallback_to_msgid | :fallback_to_msgid_and_warn | :fallback_to_default_locale | :fallback_to_default_locale_and_warn | :mark | :mark_and_warn | :error.
(Not all options would have to be supported initially or at all based on what we find useful.)
Also, there's two ways of how we can deal with those options:
- Do them at backend compile time
- Do them when the translation is used
If we do it when the translation is used, we should probably also investigate having a mix task for the CI to check if it is complete. (Prior art: gettext_check)
Yes I agree doing it per env is not necessarily the right approach as it's easily implementable on top of configuration instead.
Do them at backend compile time
I would go with this, and I would do it in the config per backend:
config :my_app, MyApp.Gettext, on_missing_translations: ...