graphql-rails-i18n 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f0b90d5534c7c2d5d09a73ea5bf13f963a4bd6835a867c6c3e81e944f11ace85
4
+ data.tar.gz: f51d456806a54485379df05b966a6e7ffac618a4ac80cda90c90590dfa193baf
5
+ SHA512:
6
+ metadata.gz: d60650cf3b4adc149e7db68113ac1e803bf010764e644e0ebaa0480d62112622a07ef2bf51fc39b35ae174b1d8845c438dfdaf8c01d313fb40140db2e0185dfc
7
+ data.tar.gz: 2e4d77836f1d3984f64838122ad7b0339920ab3643a957f93af623ba09391c805881c35524efe00e339aaba30c1b86d3ac8f0ef8eb0bc785c0885c5118f6bb4e
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './graphql/schema/directive/locale'
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class GraphQL::Schema::Directive::Locale < GraphQL::Schema::Directive
4
+ class LocaleEnum < GraphQL::Schema::Enum
5
+ def self.locale(codename:, description: '')
6
+ value(codename.underscore, description: description, value: codename)
7
+ end
8
+ end
9
+
10
+ locations GraphQL::Schema::Directive::FIELD
11
+
12
+ attr_accessor :locale_enum
13
+
14
+ argument :lang, type: LocaleEnum, required: false
15
+
16
+ def self.resolve(object, argument, context)
17
+ I18n.with_locale(argument[:lang]) do
18
+ yield
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails'
4
+ require 'graphql'
5
+ require 'graphql-rails-i18n'
6
+
7
+ describe GraphQL::Schema::Directive::Locale do
8
+ class LocaleSchema < GraphQL::Schema
9
+ class Query < GraphQL::Schema::Object
10
+ field :localized_hello, String, null: false
11
+
12
+ def localized_hello
13
+ case I18n.locale
14
+ when :en then 'Hello'
15
+ when :'zh-Hant-HK' then '您好'
16
+ else 'Hello in default language'
17
+ end
18
+ end
19
+ end
20
+
21
+ directive(GraphQL::Schema::Directive::Locale)
22
+
23
+ query(Query)
24
+ # only supported by the interpreter
25
+ use GraphQL::Execution::Interpreter
26
+ end
27
+
28
+ before do
29
+ @original_available_locales = I18n.available_locales
30
+ @original_default_locale = I18n.default_locale
31
+
32
+ I18n.available_locales = ['default', 'en', 'zh-Hant-HK']
33
+ I18n.default_locale = 'default'
34
+
35
+ GraphQL::Schema::Directive::Locale::LocaleEnum.class_eval do
36
+ locale(codename: 'en', description: 'English')
37
+ locale(codename: 'zh-Hant-HK', description: 'Hong Kong Traditional Chinese')
38
+ end
39
+ end
40
+
41
+ after do
42
+ I18n.available_locales = @original_available_locales
43
+ I18n.default_locale = @original_default_locale
44
+
45
+ GraphQL::Schema::Directive::Locale::LocaleEnum.instance_variable_set('@own_values', {})
46
+ end
47
+
48
+ context 'when @locale directive is not used' do
49
+ it 'does not call I18n.with_locale' do
50
+ str = '{
51
+ result: localizedHello
52
+ }'
53
+
54
+ res = LocaleSchema.execute(str)
55
+
56
+ expect(I18n).not_to receive(:with_locale)
57
+ expect(res['data']['result']).to eq('Hello in default language')
58
+ end
59
+ end
60
+
61
+ context 'when @locale directive is used with en argument' do
62
+ it 'calls I18n.with_locale' do
63
+ expect(I18n).to receive(:with_locale).with('en').once.and_call_original
64
+
65
+ str = '{
66
+ result: localizedHello @locale(lang: en)
67
+ }'
68
+
69
+ res = LocaleSchema.execute(str)
70
+
71
+ expect(res['data']['result']).to eq('Hello')
72
+ end
73
+ end
74
+
75
+ context 'when @locale directive is used with zh_hant_hk argument' do
76
+ it 'calls I18n.with_locale' do
77
+ expect(I18n).to receive(:with_locale).with('zh-Hant-HK').once.and_call_original
78
+
79
+ str = '{
80
+ result: localizedHello @locale(lang: zh_hant_hk)
81
+ }'
82
+
83
+ res = LocaleSchema.execute(str)
84
+
85
+ expect(res['data']['result']).to eq('您好')
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ Bundler.require
6
+
7
+ require "graphql"
8
+ require "graphql-rails-i18n"
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphql-rails-i18n
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Kenneth Law
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: graphql
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.9.pre2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.9.pre2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.2.0
41
+ description: Add @locale directive in your GraphQL API that integrates Rails I18n
42
+ API
43
+ email:
44
+ - cyt05108@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - lib/graphql-rails-i18n.rb
50
+ - lib/graphql/schema/directive/locale.rb
51
+ - spec/graphql/schema/directive/locale_spec.rb
52
+ - spec/spec_helper.rb
53
+ homepage: http://github.com/Kenneth-KT/graphql-rails-i18n
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.7.6
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Rails I18n plugin for GraphQL Ruby
77
+ test_files:
78
+ - spec/spec_helper.rb
79
+ - spec/graphql/schema/directive/locale_spec.rb