No commit activity in last 3 years
No release in over 3 years
Add @locale directive in your GraphQL API that integrates Rails I18n API
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 3.0
~> 0.49

Runtime

~> 1.9
>= 2.2.0
 Project Readme

GraphQL Rails i18n

Ruby on Rails i18n integration plugin for graphql-ruby, inspired by graphql-i18n npm library.

Installation

Install from RubyGems by adding it to your Gemfile, then bundling.

# Gemfile
gem 'graphql-rails-i18n'
$ bundle install

Getting Started

follow the ### Step comments below to setup your schema with locale directive

class ExampleSchema < GraphQL::Schema

  ### Step 1: Use new interpreter runtime from graphql-ruby
  use GraphQL::Execution::Interpreter

  ### Step 2: Declare locale directive in your schema
  directive GraphQL::Schema::Directive::Locale

  GraphQL::Schema::Directive::Locale::LocaleEnum.class_eval do
    ### Step 3: Tell @locale directive which languages are supported.
    ###         Language code names are automagically added into LocaleEnum and
    ###         underscored using `underscore` method.
    ###         For instance: `zh-Hant-HK` will be transformed into `zh_hant_hk`.
    locale(codename: 'en', description: 'English')
    locale(codename: 'zh-Hant-HK', description: 'Hong Kong Traditional Chinese')
  end

  # In your Query root object
  class Query < GraphQL::Schema::Object
    field :localized_hello, String, null: false

    def localized_hello
      ### Step 4: Deliver actual content data based on selected language
      ###         ** Code below is for demostration only, you may hook up some
      ###         i18n gems like `globalize` to output localized content.
      case I18n.locale
      when :en then 'Hello'
      when :'zh-Hant-HK' then '您好'
      else 'Hello in default language'
      end
    end
  end

  query Query
end

You can start making queries with locale directive.

query {
  ### Step 5: Write your query with @locale directive.
  ###         Remember language code names are transformed automatically to underscore format.
  defaultString: localizedString
  englishString: localizedString @locale(lang: en)
  chineseString: localizedString @locale(lang: zh_hant_hk)
}

will output

{
  "defaultString": "Hello in default language",
  "englishString": "Hello",
  "chineseString": "您好"
}