graphiti icon indicating copy to clipboard operation
graphiti copied to clipboard

Localization Support

Open JoshNorthrup opened this issue 5 years ago • 8 comments

Are there plans to add localization support to Graphiti? I'd like to be able to offer multilingual error messaging.

JoshNorthrup avatar Mar 06 '20 01:03 JoshNorthrup

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.

sandstrom avatar Apr 01 '20 08:04 sandstrom

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)

timkrins avatar Apr 14 '20 14:04 timkrins

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

JoshNorthrup avatar Apr 14 '20 15:04 JoshNorthrup

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)

timkrins avatar Apr 15 '20 09:04 timkrins

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

JoshNorthrup avatar Apr 15 '20 14:04 JoshNorthrup

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.

richmolj avatar Apr 15 '20 14:04 richmolj

Thanks for the reply @richmolj. I may take a stab at this at some point in the future

JoshNorthrup avatar Apr 15 '20 14:04 JoshNorthrup

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

apauly avatar May 05 '20 11:05 apauly