You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For most single-page apps you'd probably want to keep translations in the client, and only pass strings (error codes) in the API. You'd then translate these on the client.
If you really want to pass them, you should be able to use the Accept-Language header (which the client will send) and use that to set the locale (per request) on the server-side. That will give you translated error messages.
I don't think translations are a concern that Graphiti is likely to bolt-on, it doesn't really make sense in a serialization layer.
hey @JoshNorthrup - I am in the process of localizing a Rails app that uses Graphiti and Spraypaint.
Which errors are you wanting to localize? Rails / ActiveRecord? (because in that case Graphiti uses the underlying ActiveRecord errors, which are translated automatically by setting I18n.locale)
Everything that can be returned in an error response (outside of meta), I believe this is most, if not all, in Graphiti::Errors. I'm not consuming my API so I'd like more control over its messaging. Obviously this is doable within my app but it's not the ideal solution
Ah I see - if you want to localize the messages in Graphiti::Errors you could aways override the message methods on each class, and combine it with some other gem in your application to provide localized strings.
I am curious as to how this would be implemented - do you have an example of another gem (non-Rails or Rails-optional) that includes localization support out-of-the-box?
I know that a gem like r18n/i18n could probably be used but I also have a feeling that your use case might be uncommon (as I expect that most people using Graphiti will be consuming their own API, and the functionality in Graphiti::Errors would only be used for debugging)
I'd like to avoid overriding code if I can, this is really an inquiry to see if this is on the roadmap or something they'd be interested in having implemented. Otherwise I'll do the work within my own app.
Appreciate folks chiming in on this one and I think @timkrins is probably correct. I'd be happy to merge a PR but otherwise not on the roadmap - not a use case I personally have.
One thing to keep in mind when using Graphiti with Rails:
Sideloads will be executed within an own thread if Graphiti.config.concurrencyis set to true (= no test env and Rails.application.config.cache_classes = true). In order to also apply the locale within sideloads I came up with sth like this in an initializer:
module GraphitiContextWithLocale
def context=(context)
if context[:object].respond_to?(:request)
# set locale from context[:object].request
end
super
end
end
Graphiti.singleton_class.send :prepend, GraphitiContextWithLocale
I know it's not related to error messages directly, but maybe it's helpful for somebody else ;)
I've got no plans for built-in localization at this time. If someone wants to propose a hook for it, open a PR or an issue with a concrete design and I'd be happy to take a look.
Activity
sandstrom commentedon Apr 1, 2020
For most single-page apps you'd probably want to keep translations in the client, and only pass strings (error codes) in the API. You'd then translate these on the client.
If you really want to pass them, you should be able to use the
Accept-Languageheader (which the client will send) and use that to set the locale (per request) on the server-side. That will give you translated error messages.I don't think translations are a concern that Graphiti is likely to bolt-on, it doesn't really make sense in a serialization layer.
timkrins commentedon Apr 14, 2020
hey @JoshNorthrup - I am in the process of localizing a Rails app that uses Graphiti and Spraypaint.
Which errors are you wanting to localize? Rails / ActiveRecord? (because in that case Graphiti uses the underlying ActiveRecord errors, which are translated automatically by setting
I18n.locale)JoshNorthrup commentedon Apr 14, 2020
Everything that can be returned in an error response (outside of
meta), I believe this is most, if not all, inGraphiti::Errors. I'm not consuming my API so I'd like more control over its messaging. Obviously this is doable within my app but it's not the ideal solutiontimkrins commentedon Apr 15, 2020
Ah I see - if you want to localize the messages in
Graphiti::Errorsyou could aways override themessagemethods on each class, and combine it with some other gem in your application to provide localized strings.I am curious as to how this would be implemented - do you have an example of another gem (non-Rails or Rails-optional) that includes localization support out-of-the-box?
I know that a gem like
r18n/i18ncould probably be used but I also have a feeling that your use case might be uncommon (as I expect that most people using Graphiti will be consuming their own API, and the functionality inGraphiti::Errorswould only be used for debugging)JoshNorthrup commentedon Apr 15, 2020
I'd like to avoid overriding code if I can, this is really an inquiry to see if this is on the roadmap or something they'd be interested in having implemented. Otherwise I'll do the work within my own app.
Devise does this: https://github.com/heartcombo/devise. I don't know any example off the top that is outside of the rails universe (Rails-verse?)
richmolj commentedon Apr 15, 2020
Appreciate folks chiming in on this one and I think @timkrins is probably correct. I'd be happy to merge a PR but otherwise not on the roadmap - not a use case I personally have.
JoshNorthrup commentedon Apr 15, 2020
Thanks for the reply @richmolj. I may take a stab at this at some point in the future
apauly commentedon May 5, 2020
One thing to keep in mind when using Graphiti with Rails:
Sideloads will be executed within an own thread if
Graphiti.config.concurrencyis set to true (= no test env andRails.application.config.cache_classes = true). In order to also apply the locale within sideloads I came up with sth like this in an initializer:I know it's not related to error messages directly, but maybe it's helpful for somebody else ;)
jkeen commentedon Jul 29, 2026
I've got no plans for built-in localization at this time. If someone wants to propose a hook for it, open a PR or an issue with a concrete design and I'd be happy to take a look.
feat: take error text from locale keys
2.0.0-beta.12 CHANGELOG [skip ci]
jkeen commentedon Aug 30, 2026
Plans changed and I got this into the 2.0 beta 12!
d01fd06
2.0.0 CHANGELOG [skip ci]
github-actions commentedon Sep 1, 2026
🎉 This issue has been resolved in version 2.0.0 🎉
The release is available on:
v2.0.0Your semantic-release bot 📦🚀