gettext icon indicating copy to clipboard operation
gettext copied to clipboard

Optionally warn about missing translations

Open PJUllrich opened this issue 11 months ago • 5 comments

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 :)

PJUllrich avatar Jan 09 '25 17:01 PJUllrich

Hey Peter 👋 , so this would be a local development kind of configuration?

whatyouhide avatar Jan 13 '25 12:01 whatyouhide

Hello 👋 yes that’s right. It would help to find missing translations. 

PJUllrich avatar Jan 13 '25 13:01 PJUllrich

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?

whatyouhide avatar Jan 22 '25 08:01 whatyouhide

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:

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)

maennchen avatar Jan 22 '25 09:01 maennchen

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: ...

whatyouhide avatar Jan 27 '25 08:01 whatyouhide