Translating Ash Error messages
What are my options for translating error messages returned by actions?
The first thing that comes to mind is translating the errors in the controller like this:
However, this feels repetitive and pollutes the controller.
Is there a better way to handle this in Ash?
13 Replies
Currently that is the best way TBH
I personally use dggettext in my web layer
not sure if it works for all errors, but it works for field errors pretty well
they are already being passed through gettext anyway for the {min} {max} {count} stuff anyway
we have definitely talked about adding gettext to Ash's error system but since neither of us speaks another language or has really built anything with translations in it we want to defer to experts
I have a few apps in japanese, I've seen murmurings about how gettext isn't really that good anyway (from the cldr people), so maybe one day I'll change
I don't know if it exists yet, but it would be useful to have a mix task that just extracts the strings from ash and dumps it into a pot file
I've had to add the errors I see manually, which isn't the most comprehensive
Would you be so kind to share a code snippet showing how you are doing that? I planned to use gettext as well but I refuse to translate errors in my controller, at least for now
@Zach Daniel How about modifying the error messages directly? Is that possible? I don't need to support multiple languages, just Spanish
gettext is best option as the errors are hardcoded inside the respective ash error modules
in errors.pot
and then the standard phoenix liveview core_components translate_error thing should work
Basically

then you generate po files for whatever language you want
Oh yeah, problem is I'm not using LiveView components
I'm using React with Inertia
would be similar, get an i18n thing for react and generate translations there instead
have to create a list once manually of the ash stuff and swap it out in react
if you want to go even more budget then you don't even need gettext i18n, just have a function in react land that swaps keys
assuming it will be similar to this
https://medium.com/@ayatir04/laravel-inertia-js-localization-without-packages-6e7f49fb07c
Yeah that would be another option. Just wanted to see if there was a more "ergonomic way" to do this directly from Ash. So everything is sent to the frontend ready for rendering
well it's the view layer, so just use gettext on the server to send translations. I've never used inertia so no idea how it all plumbs together
maybe register a before_send thing on Plug.Conn and do stuff there
so you only define it once
Will look into all that
Thanks a lot for your suggestions. I appreciate it.