tolgee_liquid 0.1.0

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: 115f1b6b36f7ce8170a03661eafb9f994c8f4f8756323e5c384d5190d73bb58c
4
+ data.tar.gz: 360ce4f158ead855313cb7346ec7b25bf2ab15586c1de6e7c81595f40420b96e
5
+ SHA512:
6
+ metadata.gz: 435f14d941be45e2985fd91aa8d1a980f3a420d7201c60e2dbe63340f6e563935c9c89e0d87e8bfd20fc0b8bd7906e47db70f0161dc1a9e9dfb3207784d76567
7
+ data.tar.gz: f2e7948c8f45cc4942c1385a66036ca63ac20bee6a759d6a5f647d5c3b6e3ad2935440bb8bdb8e977bee313fa1838a163d67b09134b1d57b79d2528aaf337bea
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.10
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in tolgee_liquid.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,63 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tolgee_liquid (0.1.0)
5
+ message_format (~> 0.0.8)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.8.6)
11
+ public_suffix (>= 2.0.2, < 6.0)
12
+ bigdecimal (3.1.8)
13
+ camertron-eprun (1.1.1)
14
+ cldr-plurals-runtime-rb (1.1.0)
15
+ concurrent-ruby (1.3.1)
16
+ crack (1.0.0)
17
+ bigdecimal
18
+ rexml
19
+ diff-lcs (1.5.1)
20
+ hashdiff (1.1.0)
21
+ message_format (0.0.8)
22
+ twitter_cldr (~> 6.0)
23
+ public_suffix (5.0.5)
24
+ rake (10.5.0)
25
+ rexml (3.2.8)
26
+ strscan (>= 3.0.9)
27
+ rspec (3.13.0)
28
+ rspec-core (~> 3.13.0)
29
+ rspec-expectations (~> 3.13.0)
30
+ rspec-mocks (~> 3.13.0)
31
+ rspec-core (3.13.0)
32
+ rspec-support (~> 3.13.0)
33
+ rspec-expectations (3.13.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.13.0)
36
+ rspec-mocks (3.13.1)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.13.0)
39
+ rspec-support (3.13.1)
40
+ strscan (3.1.0)
41
+ twitter_cldr (6.12.1)
42
+ camertron-eprun
43
+ cldr-plurals-runtime-rb (~> 1.1)
44
+ tzinfo
45
+ tzinfo (2.0.6)
46
+ concurrent-ruby (~> 1.0)
47
+ webmock (3.23.1)
48
+ addressable (>= 2.8.0)
49
+ crack (>= 0.3.2)
50
+ hashdiff (>= 0.4.0, < 2.0.0)
51
+
52
+ PLATFORMS
53
+ ruby
54
+
55
+ DEPENDENCIES
56
+ bundler (~> 1.17)
57
+ rake (~> 10.0)
58
+ rspec (~> 3.0)
59
+ tolgee_liquid!
60
+ webmock (~> 3.23)
61
+
62
+ BUNDLED WITH
63
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Allen He
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # TolgeeLiquid
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tolgee_liquid`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'tolgee_liquid'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tolgee_liquid
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tolgee_liquid.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tolgee_liquid"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,54 @@
1
+ require 'net/http'
2
+ require 'message_format'
3
+
4
+ class Translate
5
+ def initialize
6
+ @tolgee_api_url = TolgeeLiquid.configuration.api_url
7
+ @tolgee_api_key = TolgeeLiquid.configuration.api_key
8
+ @tolgee_project_id = TolgeeLiquid.configuration.project_id
9
+ end
10
+
11
+ def execute(name, vars = {}, opts)
12
+ locale = opts[:locale]
13
+ dev_mode = opts[:mode] == 'development'
14
+ static_data = opts[:static_data]
15
+ dict = dev_mode ? get_remote_dict(locale.to_s) : static_data[locale.to_sym]
16
+ value = fetch_translation(dict, name)
17
+ return name if value.nil?
18
+
19
+ translation = MessageFormat.new(value, locale.to_s).format(vars.transform_keys(&:to_sym))
20
+
21
+ if dev_mode
22
+ message = { k: name }.to_json
23
+ hidden_message = ZeroWidthCharacterEncoder.new.execute(message)
24
+ "#{translation}#{hidden_message}"
25
+ else
26
+ translation
27
+ end
28
+ end
29
+
30
+ def fetch_translation(dict, name)
31
+ name.split('.'.freeze).reduce(dict) do |level, cur|
32
+ return nil if level[cur].nil?
33
+
34
+ level[cur]
35
+ end
36
+ end
37
+
38
+ def get_remote_dict(locale)
39
+ @remote_dict ||= begin
40
+ url = URI("#{@tolgee_api_url}/v2/projects/#{@tolgee_project_id}/translations/#{locale}")
41
+ http = Net::HTTP.new(url.host, url.port)
42
+ http.use_ssl = true if url.scheme == 'https'
43
+
44
+ request = Net::HTTP::Get.new(url)
45
+ request['Accept'] = 'application/json'
46
+ request['X-API-Key'] = @tolgee_api_key
47
+
48
+ response = http.request(request)
49
+ JSON.parse(response.body)[locale]
50
+ rescue
51
+ {}
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module TolgeeLiquid
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ class ZeroWidthCharacterEncoder
2
+ INVISIBLE_CHARACTERS = ["\u200C", "\u200D"].freeze
3
+
4
+ def execute(text)
5
+ bytes = text.bytes
6
+ binary = bytes.map { |byte| to_binary_with_extra_non_join_character(byte) }.join
7
+ to_invisible_string(binary)
8
+ end
9
+
10
+ private
11
+
12
+ def to_binary_with_extra_non_join_character(byte)
13
+ "#{byte.to_s(2).rjust(8, '0')}0"
14
+ end
15
+
16
+ def to_invisible_string(binary)
17
+ binary.chars.map { |bit| INVISIBLE_CHARACTERS[bit.to_i].encode('utf-8') }.join
18
+ end
19
+ end
@@ -0,0 +1,56 @@
1
+ require "tolgee_liquid/version"
2
+ require "tolgee_liquid/translate"
3
+ require "tolgee_liquid/zero_width_character_encoder"
4
+
5
+ module TolgeeLiquid
6
+ class << self
7
+ attr_accessor :configuration
8
+
9
+ class Configuration
10
+ attr_accessor :api_url, :api_key, :project_id
11
+ end
12
+
13
+ def configure
14
+ self.configuration ||= Configuration.new
15
+ yield(configuration)
16
+ end
17
+
18
+ def registers(options)
19
+ options.slice(:locale, :static_data, :mode)
20
+ end
21
+ end
22
+ end
23
+
24
+ def with_tolgee(method_name)
25
+ original_method = instance_method(method_name)
26
+ define_method(method_name) do |*args, &fn|
27
+ mode = @context.registers[:mode] || 'production'
28
+ locale = @context.registers[:locale] || I18n.default_locale
29
+
30
+ if mode == 'development'
31
+ message = { k: args.first }.to_json
32
+ hidden_message = ZeroWidthCharacterEncoder.new.execute(message)
33
+
34
+ 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?
38
+
39
+ translation = MessageFormat.new(value, locale.to_s).format(args.second&.transform_keys(&:to_sym))
40
+ "#{translation}#{hidden_message}"
41
+ else
42
+ original_method.bind(self).call(*args, &fn)
43
+ end
44
+ end
45
+ end
46
+
47
+ module TolgeeFilter
48
+ def t(name, vars = {})
49
+ opts = {
50
+ locale: @context.registers[:locale] || I18n.default_locale,
51
+ mode: @context.registers[:mode] || 'production',
52
+ static_data: @context.registers[:static_data] || {},
53
+ }
54
+ Translate.new.execute(name, vars, opts)
55
+ end
56
+ end
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "tolgee_liquid/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tolgee_liquid"
8
+ spec.version = TolgeeLiquid::VERSION
9
+ spec.authors = ["Allen He"]
10
+ spec.email = ["pooopooo543@gmail.com"]
11
+
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"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # 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
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency 'message_format', '~> 0.0.8'
26
+
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"
31
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tolgee_liquid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Allen He
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: message_format
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.23'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.23'
83
+ description: A gem that integrate Tolgee Platform to Shopify Liquid template engine.
84
+ email:
85
+ - pooopooo543@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/tolgee_liquid.rb
101
+ - lib/tolgee_liquid/translate.rb
102
+ - lib/tolgee_liquid/version.rb
103
+ - lib/tolgee_liquid/zero_width_character_encoder.rb
104
+ - tolgee_liquid.gemspec
105
+ homepage:
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubygems_version: 3.0.3.1
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Tolgee Integration for Liquid
128
+ test_files: []