Skip to content

Localization Support #216

Closed
Closed

Description

@JoshNorthrup

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

Activity

  1. sandstrom commented on Apr 1, 2020

    @sandstrom

    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.

  2. timkrins commented on Apr 14, 2020

    @timkrins

    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)

  3. JoshNorthrup commented on Apr 14, 2020

    @JoshNorthrup
    Author

    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

  4. timkrins commented on Apr 15, 2020

    @timkrins

    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)

  5. JoshNorthrup commented on Apr 15, 2020

    @JoshNorthrup
    Author

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

  6. richmolj commented on Apr 15, 2020

    @richmolj
    Contributor

    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.

  7. JoshNorthrup commented on Apr 15, 2020

    @JoshNorthrup
    Author

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

  8. apauly commented on May 5, 2020

    @apauly

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

  9. jkeen commented on Jul 29, 2026

    @jkeen
    Collaborator

    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.

  10. added a commit that references this issue on Aug 30, 2026
    d01fd06
  11. jkeen commented on Aug 30, 2026

    @jkeen
    Collaborator

    Plans changed and I got this into the 2.0 beta 12!

    d01fd06

  12. added a commit that references this issue on Sep 1, 2026
  13. github-actions commented on Sep 1, 2026

    @github-actions
    Contributor

    🎉 This issue has been resolved in version 2.0.0 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions