tolgee_liquid 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 135c8ea7a31e3de305940140011512b70acbcb3a2f1b1ce11ee576c8c5ae4837
4
- data.tar.gz: 9cf05ee3c4c5e8616fc83788300c6d14f50ab8b22401dcd9f05ab87e2881d16a
3
+ metadata.gz: dca99fd61c6ef48a8ed0e9859d8bf699018694413886d2fcca125715ae469487
4
+ data.tar.gz: 6a87046ae897d9c133ff9b8f5898f0d10d0008faf7f5fb6ce1e92bac957226c0
5
5
  SHA512:
6
- metadata.gz: ec5446a4b41750ef8b374ecb1bd8bbbb77d1afc2c29e134067e587b52206c48bf0e767b0f22fdbe1a70714322e4d23bbaca9f8575b5b5751a9370f61076300ab
7
- data.tar.gz: 486b3b5a4fd57539dd567d88f1488786b3253977a98eba3767b40d59a460e01f89b0e8a9a10cb99203609331b12ff358d7bd52d35b1016c24a3abd9d88358afb
6
+ metadata.gz: 73eea62ed05227aa481dc869095f1beaad7ee32aa7f9079e15aebcfa4549e69a8ee7644692b089afd27f978d94047fbb248cab3f0ab3de0fb0e495a241c2a9b0
7
+ data.tar.gz: 03b837fa231f52f88fd2eeae62f1b059fb27684a20dd5ce21f23a6f844b31643fb14d38ab7ab5d5e98cd7a5adffe2e981f2434a25ab0f89c1456eeb8d11e0110
data/.rubocop.yml ADDED
@@ -0,0 +1,7 @@
1
+ AllCops:
2
+ NewCops: disable
3
+ SuggestExtensions: false
4
+
5
+ Metrics/BlockLength:
6
+ Exclude:
7
+ - spec/*.rb
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.10
data/.travis.yml CHANGED
@@ -10,6 +10,7 @@ before_install:
10
10
 
11
11
  script:
12
12
  - bin/setup
13
+ - bundle exec rubocop
13
14
  - bundle exec rake spec
14
15
  - gem build tolgee_liquid.gemspec
15
16
 
data/Gemfile CHANGED
@@ -1,6 +1,8 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
6
 
5
7
  # Specify your gem's dependencies in tolgee_liquid.gemspec
6
8
  gemspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tolgee_liquid (0.1.8)
4
+ tolgee_liquid (0.1.9)
5
5
  message_format (~> 0.0.8)
6
6
 
7
7
  GEM
@@ -9,6 +9,7 @@ GEM
9
9
  specs:
10
10
  addressable (2.8.6)
11
11
  public_suffix (>= 2.0.2, < 6.0)
12
+ ast (2.4.2)
12
13
  bigdecimal (3.1.8)
13
14
  camertron-eprun (1.1.1)
14
15
  cldr-plurals-runtime-rb (1.1.0)
@@ -18,10 +19,20 @@ GEM
18
19
  rexml
19
20
  diff-lcs (1.5.1)
20
21
  hashdiff (1.1.0)
22
+ i18n (1.14.5)
23
+ concurrent-ruby (~> 1.0)
24
+ json (2.7.2)
21
25
  message_format (0.0.8)
22
26
  twitter_cldr (~> 6.0)
27
+ parallel (1.24.0)
28
+ parser (3.3.2.0)
29
+ ast (~> 2.4.1)
30
+ racc
23
31
  public_suffix (5.0.5)
32
+ racc (1.8.0)
33
+ rainbow (3.1.1)
24
34
  rake (10.5.0)
35
+ regexp_parser (2.9.2)
25
36
  rexml (3.2.8)
26
37
  strscan (>= 3.0.9)
27
38
  rspec (3.13.0)
@@ -37,6 +48,19 @@ GEM
37
48
  diff-lcs (>= 1.2.0, < 2.0)
38
49
  rspec-support (~> 3.13.0)
39
50
  rspec-support (3.13.1)
51
+ rubocop (1.50.2)
52
+ json (~> 2.3)
53
+ parallel (~> 1.10)
54
+ parser (>= 3.2.0.0)
55
+ rainbow (>= 2.2.2, < 4.0)
56
+ regexp_parser (>= 1.8, < 3.0)
57
+ rexml (>= 3.2.5, < 4.0)
58
+ rubocop-ast (>= 1.28.0, < 2.0)
59
+ ruby-progressbar (~> 1.7)
60
+ unicode-display_width (>= 2.4.0, < 3.0)
61
+ rubocop-ast (1.30.0)
62
+ parser (>= 3.2.1.0)
63
+ ruby-progressbar (1.13.0)
40
64
  strscan (3.1.0)
41
65
  twitter_cldr (6.12.1)
42
66
  camertron-eprun
@@ -44,6 +68,7 @@ GEM
44
68
  tzinfo
45
69
  tzinfo (2.0.6)
46
70
  concurrent-ruby (~> 1.0)
71
+ unicode-display_width (2.5.0)
47
72
  webmock (3.23.1)
48
73
  addressable (>= 2.8.0)
49
74
  crack (>= 0.3.2)
@@ -54,8 +79,10 @@ PLATFORMS
54
79
 
55
80
  DEPENDENCIES
56
81
  bundler (~> 1.17)
82
+ i18n (~> 1.14)
57
83
  rake (~> 10.0)
58
84
  rspec (~> 3.0)
85
+ rubocop (~> 1.50)
59
86
  tolgee_liquid!
60
87
  webmock (~> 3.23)
61
88
 
data/README.md CHANGED
@@ -107,9 +107,11 @@ key:
107
107
 
108
108
  ## Development
109
109
 
110
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
111
-
112
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
110
+ - Run `install dependencies` to install dependencies.
111
+ - Run `rake spec` to run the tests.
112
+ - Run `bin/console` for an interactive prompt that will allow you to experiment.
113
+ - Run `rubocop` to find code smells.
114
+ - Run `bin/deploy` to release a new version
113
115
 
114
116
  ## Contributing
115
117
 
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
data/bin/console CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "tolgee_liquid"
4
+ require 'bundler/setup'
5
+ require 'tolgee_liquid'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "tolgee_liquid"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- require "irb"
14
+ require 'irb'
14
15
  IRB.start(__FILE__)
data/bin/deploy CHANGED
@@ -49,7 +49,7 @@ case "$version_bump" in
49
49
  esac
50
50
 
51
51
  echo "Bumping version from $current_version to $new_version"
52
- sed -i "s/VERSION = \"$current_version\"/VERSION = \"$new_version\"/" lib/tolgee_liquid/version.rb
52
+ ruby -i -pe "gsub(/VERSION = '$current_version'/, \"VERSION = '$new_version'\")" lib/tolgee_liquid/version.rb
53
53
 
54
54
  # Create commit and tag for new version
55
55
  git add lib/tolgee_liquid/version.rb
@@ -1,6 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'net/http'
2
4
  require 'message_format'
3
5
 
6
+ # A translator will try to get the translations, handle interpolations and convert it to static string.
7
+ # If it's in development mode it will fetch translations from tolgee's platform.
8
+ # Otherwise, it will use static translations provided by outside.
4
9
  class Translate
5
10
  def initialize
6
11
  @tolgee_api_url = TolgeeLiquid.configuration.api_url
@@ -8,11 +13,12 @@ class Translate
8
13
  @tolgee_project_id = TolgeeLiquid.configuration.project_id
9
14
  end
10
15
 
11
- def execute(name, vars = {}, opts)
16
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
17
+ def execute(name, vars, opts)
12
18
  locale = opts[:locale]
13
19
  dev_mode = opts[:mode] == 'development'
14
20
  static_data = opts[:static_data]
15
- dict = dev_mode ? get_remote_dict(locale.to_s) : static_data[locale.to_sym]
21
+ dict = dev_mode ? remote_dict(locale.to_s) : static_data[locale.to_sym]
16
22
  value = fetch_translation(dict, name)
17
23
  return name if value.nil?
18
24
 
@@ -26,16 +32,18 @@ class Translate
26
32
  translation
27
33
  end
28
34
  end
35
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
29
36
 
30
37
  def fetch_translation(dict, name)
31
- name.split('.'.freeze).reduce(dict) do |level, cur|
38
+ name.split('.').reduce(dict) do |level, cur|
32
39
  return nil if level[cur].nil?
33
40
 
34
41
  level[cur]
35
42
  end
36
43
  end
37
44
 
38
- def get_remote_dict(locale)
45
+ # rubocop:disable Metrics/MethodLength
46
+ def remote_dict(locale)
39
47
  @remote_dict ||= begin
40
48
  url = URI("#{@tolgee_api_url}/v2/projects/#{@tolgee_project_id}/translations/#{locale}")
41
49
  http = Net::HTTP.new(url.host, url.port)
@@ -47,8 +55,9 @@ class Translate
47
55
 
48
56
  response = http.request(request)
49
57
  JSON.parse(response.body)[locale]
50
- rescue
58
+ rescue StandardError
51
59
  {}
52
60
  end
53
61
  end
62
+ # rubocop:enable Metrics/MethodLength
54
63
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TolgeeLiquid
2
- VERSION = "0.1.8"
4
+ VERSION = '0.1.9'
3
5
  end
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # An string encoder that will transform original message to invisible ASCII characters.
4
+ # See the detail in: https://tolgee.io/blog/2021/12/17/invisible-characters-for-better-localization
1
5
  class ZeroWidthCharacterEncoder
2
6
  INVISIBLE_CHARACTERS = ["\u200C", "\u200D"].freeze
3
7
 
data/lib/tolgee_liquid.rb CHANGED
@@ -1,14 +1,19 @@
1
- require "tolgee_liquid/version"
2
- require "tolgee_liquid/translate"
3
- require "tolgee_liquid/zero_width_character_encoder"
1
+ # frozen_string_literal: true
4
2
 
3
+ require 'tolgee_liquid/version'
4
+ require 'tolgee_liquid/translate'
5
+ require 'tolgee_liquid/zero_width_character_encoder'
6
+
7
+ # This allow user to configure Tolgee credentials
5
8
  module TolgeeLiquid
6
9
  class << self
7
10
  attr_accessor :configuration
8
11
 
12
+ # rubocop:disable Style/Documentation
9
13
  class Configuration
10
14
  attr_accessor :api_url, :api_key, :project_id
11
15
  end
16
+ # rubocop:enable Style/Documentation
12
17
 
13
18
  def configure
14
19
  self.configuration ||= Configuration.new
@@ -21,6 +26,7 @@ module TolgeeLiquid
21
26
  end
22
27
  end
23
28
 
29
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
24
30
  def with_tolgee(method_name)
25
31
  original_method = instance_method(method_name)
26
32
  define_method(method_name) do |*args, &fn|
@@ -28,28 +34,30 @@ def with_tolgee(method_name)
28
34
  locale = @context.registers[:locale] || I18n.default_locale
29
35
 
30
36
  if mode == 'development'
31
- message = { k: args.first }.to_json
37
+ message = { k: args[0] }.to_json
32
38
  hidden_message = ZeroWidthCharacterEncoder.new.execute(message)
33
39
 
34
40
  t = Translate.new
35
- dict = t.get_remote_dict(locale)
36
- value = t.fetch_translation(dict, args.first)
37
- return args.first if value.nil?
41
+ dict = t.remote_dict(locale)
42
+ value = t.fetch_translation(dict, args[0])
43
+ return args[0] if value.nil?
38
44
 
39
- translation = MessageFormat.new(value, locale.to_s).format(args.second&.transform_keys(&:to_sym))
45
+ translation = MessageFormat.new(value, locale.to_s).format(args[1]&.transform_keys(&:to_sym))
40
46
  "#{translation}#{hidden_message}"
41
47
  else
42
48
  original_method.bind(self).call(*args, &fn)
43
49
  end
44
50
  end
45
51
  end
52
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
46
53
 
54
+ # Expose translation method that used in Liquid filters
47
55
  module TolgeeFilter
48
56
  def t(name, vars = {})
49
57
  opts = {
50
58
  locale: @context.registers[:locale] || I18n.default_locale,
51
59
  mode: @context.registers[:mode] || 'production',
52
- static_data: @context.registers[:static_data] || {},
60
+ static_data: @context.registers[:static_data] || {}
53
61
  }
54
62
  Translate.new.execute(name, vars, opts)
55
63
  end
@@ -1,31 +1,36 @@
1
+ # frozen_string_literal: true
1
2
 
2
- lib = File.expand_path("../lib", __FILE__)
3
+ lib = File.expand_path('lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "tolgee_liquid/version"
5
+ require 'tolgee_liquid/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "tolgee_liquid"
8
+ spec.name = 'tolgee_liquid'
8
9
  spec.version = TolgeeLiquid::VERSION
9
- spec.authors = ["Allen He"]
10
- spec.email = ["pooopooo543@gmail.com"]
10
+ spec.authors = ['Allen He']
11
+ spec.email = ['pooopooo543@gmail.com']
11
12
 
12
- spec.summary = %q{Tolgee Integration for Liquid}
13
- spec.description = %q{A gem that integrate Tolgee Platform to Shopify Liquid template engine.}
14
- spec.license = "MIT"
13
+ spec.summary = 'Tolgee Integration for Liquid'
14
+ spec.description = 'A gem that integrate Tolgee Platform to Shopify Liquid template engine.'
15
+ spec.license = 'MIT'
15
16
 
16
17
  # Specify which files should be added to the gem when it is released.
17
18
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
20
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
21
  end
21
- spec.bindir = "exe"
22
+ spec.bindir = 'exe'
22
23
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
- spec.require_paths = ["lib"]
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.required_ruby_version = '>= 2.6.10'
24
27
 
25
28
  spec.add_dependency 'message_format', '~> 0.0.8'
26
29
 
27
- spec.add_development_dependency "bundler", "~> 1.17"
28
- spec.add_development_dependency "rake", "~> 10.0"
29
- spec.add_development_dependency "rspec", "~> 3.0"
30
- spec.add_development_dependency "webmock", "~> 3.23"
30
+ spec.add_development_dependency 'bundler', '~> 1.17'
31
+ spec.add_development_dependency 'i18n', '~> 1.14'
32
+ spec.add_development_dependency 'rake', '~> 10.0'
33
+ spec.add_development_dependency 'rspec', '~> 3.0'
34
+ spec.add_development_dependency 'rubocop', '~> 1.50'
35
+ spec.add_development_dependency 'webmock', '~> 3.23'
31
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tolgee_liquid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Allen He
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: i18n
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +80,20 @@ dependencies:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.50'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.50'
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: webmock
71
99
  requirement: !ruby/object:Gem::Requirement
@@ -89,6 +117,8 @@ extra_rdoc_files: []
89
117
  files:
90
118
  - ".gitignore"
91
119
  - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".ruby-version"
92
122
  - ".travis.yml"
93
123
  - Gemfile
94
124
  - Gemfile.lock
@@ -115,7 +145,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
145
  requirements:
116
146
  - - ">="
117
147
  - !ruby/object:Gem::Version
118
- version: '0'
148
+ version: 2.6.10
119
149
  required_rubygems_version: !ruby/object:Gem::Requirement
120
150
  requirements:
121
151
  - - ">="