mc_translator 0.1.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8df4b86675486570431856d5a8a58c608afcccb4603010670dba38a5319c401f
4
+ data.tar.gz: 90ac2cd1d8e9f67ac41044a54eaa40f195c2ff63d32c291a9c1f5b4a83b3bcc2
5
+ SHA512:
6
+ metadata.gz: 5b533b9881a2065743d0f20d4294ea4395237cde0cc24fab8c36b5d09906572240a582e3482ab01e2fd4d67c6e07be6330f366ee4c8f5c758371478d98fd9194
7
+ data.tar.gz: d390634b64d849bf06d492dbb0a1c21a9d87015da5466a04b18e1a68dd8c27cee253a64d29b2283e8f5ce14cb610edeff068ffdb5ae1025212d7bfb8d80dd58c
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in mc_translator.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mc_translator (0.1.3)
5
+ git (~> 1.8.1)
6
+ smartling
7
+ yaml (~> 0.1.1)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ domain_name (0.5.20190701)
13
+ unf (>= 0.0.5, < 1.0.0)
14
+ git (1.8.1)
15
+ rchardet (~> 1.8)
16
+ http-accept (1.7.0)
17
+ http-cookie (1.0.3)
18
+ domain_name (~> 0.5)
19
+ mime-types (3.3.1)
20
+ mime-types-data (~> 3.2015)
21
+ mime-types-data (3.2021.0225)
22
+ multi_json (1.15.0)
23
+ netrc (0.11.0)
24
+ oj (3.11.5)
25
+ rake (13.0.3)
26
+ rchardet (1.8.0)
27
+ rest-client (2.1.0)
28
+ http-accept (>= 1.7.0, < 2.0)
29
+ http-cookie (>= 1.0.2, < 2.0)
30
+ mime-types (>= 1.16, < 4.0)
31
+ netrc (~> 0.8)
32
+ smartling (2.0.3)
33
+ multi_json (~> 1.0)
34
+ oj (~> 3.0)
35
+ rest-client (~> 2.0)
36
+ unf (0.1.4)
37
+ unf_ext
38
+ unf_ext (0.0.7.7)
39
+ yaml (0.1.1)
40
+
41
+ PLATFORMS
42
+ x86_64-darwin-20
43
+
44
+ DEPENDENCIES
45
+ mc_translator!
46
+ rake (~> 13.0)
47
+
48
+ BUNDLED WITH
49
+ 2.2.15
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Translator Gem
2
+
3
+ This gem is for pushing and pulling translations to and from Smartling.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's [Gemfile](https://github.com/yankaindustries/masterclass/blob/i18n/mc_translator/Gemfile#L188):
8
+
9
+ ```ruby
10
+ gem 'mc_translator', github: 'yankaindustries/mc_translator', branch: 'main', require: 'smartling'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```zsh
16
+ $ bundle install
17
+ ```
18
+
19
+ Then, in your [Rakefile](https://github.com/yankaindustries/masterclass/blob/i18n/mc_translator/Rakefile#L8):
20
+
21
+ ```rb
22
+ require 'mc_translator
23
+ ```
24
+
25
+ Once you've got it installed, you'll need some basic configuration by adding a [.translations.yml](https://github.com/yankaindustries/masterclass/blob/i18n/mc_translator/.translator.yml):
26
+
27
+ ```yaml
28
+ userId: xxxxxxxxxxxxxxxxxx
29
+ userSecret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
30
+ projectId: xxxxxxx
31
+ locales:
32
+ - en-GB
33
+ matches:
34
+ - pattern: '**/*en-US.yml'
35
+ type: YAML
36
+ - pattern: '**/*en-US.json'
37
+ type: JSON
38
+ parentBranch: master
39
+ ```
40
+
41
+ ## Usage
42
+
43
+ To keep things as simple as possible, we've added some Rake commands so that you can do this as simply as running:
44
+
45
+ ```zsh
46
+ $ rake translator:push
47
+ ```
48
+
49
+ and
50
+
51
+ ```zsh
52
+ $ rake translator:pull
53
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env rake
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/gem_tasks"
5
+ # require "mc_translator"
6
+ require_relative "lib/mc_translator"
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "mc_translator"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ 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,160 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'mc_translator/version'
4
+ require_relative 'mc_translator/tasks'
5
+ require_relative 'smartling/job'
6
+ require 'yaml'
7
+ require 'git'
8
+ require 'smartling'
9
+
10
+
11
+ module McTranslator
12
+ class Error < StandardError; end
13
+
14
+ class Translator
15
+ def print_msg(msg)
16
+ puts
17
+ puts msg
18
+ end
19
+
20
+ def initialize
21
+ args = {
22
+ userId: config['userId'],
23
+ userSecret: config['userSecret'],
24
+ projectId: config['projectId'],
25
+ }
26
+
27
+ @files = Smartling::File.new(args)
28
+ @jobs = Smartling::Job.new(args)
29
+ end
30
+
31
+ def push_changed
32
+ push changed_files
33
+ end
34
+
35
+ def push_all
36
+ push all_files
37
+ end
38
+
39
+ def pull_job
40
+ pull job_files
41
+ end
42
+
43
+ def pull_all
44
+ pull all_files
45
+ end
46
+
47
+ private
48
+
49
+ def config
50
+ YAML.safe_load(File.read('.translator.yml'))
51
+ end
52
+
53
+ def git
54
+ Git.open(Dir.getwd)
55
+ end
56
+
57
+ def origin
58
+ current_branch = git.current_branch
59
+ parent_branch = config['parentBranch']
60
+
61
+ if current_branch == config['parentBranch']
62
+ first_commit = git.log.last
63
+ else
64
+ first_commit = git.log.between(parent_branch, current_branch).last
65
+ end
66
+
67
+ first_commit.parent
68
+ end
69
+
70
+ def changed_files
71
+ config['matches'].flat_map do |matcher|
72
+ git.diff(origin.sha)
73
+ .select { |file| File.fnmatch(matcher['pattern'], file.path) }
74
+ .select { |file| %w(new modified).include? file.type }
75
+ .map { |file| { path: file.path, name: file.path, type: matcher['type'] } }
76
+ end
77
+ end
78
+
79
+ def all_files
80
+ config['matches'].flat_map do |matcher|
81
+ Dir.glob(matcher['pattern']).map do |file|
82
+ { path: file, name: file, type: matcher['type'] }
83
+ end
84
+ end
85
+ end
86
+
87
+ def job_files
88
+ current_branch = git.current_branch
89
+
90
+ print_msg 'Getting job and files...'
91
+ job = @jobs.list['items'].detect { |j| j['jobName'] == current_branch }
92
+ @jobs.files(job['translationJobUid'])['items'].map do |file|
93
+ { path: file['uri'], name: file['uri'] }
94
+ end
95
+ end
96
+
97
+ def push(files)
98
+ current_branch = git.current_branch
99
+
100
+ print_msg 'Setting up job...'
101
+ job = @jobs.find_or_create current_branch
102
+ p job['translationJobUid']
103
+
104
+ print_msg 'Uploading files...'
105
+ files.each do |file|
106
+ p file[:path]
107
+ begin
108
+ @files.upload file[:path], file[:path], file[:type]
109
+ @jobs.add_file job['translationJobUid'], file[:path], config['locales']
110
+ rescue => error
111
+ p error
112
+ end
113
+ end
114
+
115
+ # There's some sort of lag between the creating a job and the API
116
+ # returning the correct status, so we add a little delay to compensate
117
+ sleep 2
118
+
119
+ print_msg 'Checking job...'
120
+ job = @jobs.detail job['translationJobUid']
121
+ p job['jobStatus']
122
+
123
+ if job['jobStatus'] == 'AWAITING_AUTHORIZATION'
124
+ print_msg 'Authorizing job...'
125
+ @jobs.authorize job["translationJobUid"]
126
+ end
127
+
128
+ job
129
+ end
130
+
131
+ def pull(files)
132
+ files
133
+ .map do |file| file[:path] end
134
+ .each do |file|
135
+ config['locales'].each do |locale|
136
+ expansion = {
137
+ locale: locale,
138
+ dir: File.dirname(file),
139
+ name: File.basename(file, '.*').gsub(/en-US/, ''),
140
+ ext: File.extname(file),
141
+ }
142
+
143
+ name = File.expand_path(config['rewrite'] % expansion)
144
+ content = @files.download_translated(
145
+ file,
146
+ locale,
147
+ { retrievalType: 'published' }
148
+ )
149
+
150
+ p file
151
+ p name
152
+
153
+ dir = File.dirname(name)
154
+ Dir.mkdir(dir) unless Dir.exist?(dir)
155
+ File.write(name, content)
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ path = File.expand_path(__dir__)
4
+ # Dir.glob("#{path}/tasks/test.rake").each { |f| import f }
5
+ Dir.glob("#{path}/**/*.rake").each { |f| import f }
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :translator do
4
+ desc 'Download translated files of current job from Smartling'
5
+ task 'pull:job' do
6
+ translator = McTranslator::Translator.new
7
+ translator.pull_job
8
+ end
9
+ task 'pull' => 'pull:job'
10
+
11
+ desc 'Download all translated files from Smartling'
12
+ task 'pull:all' do
13
+ translator = McTranslator::Translator.new
14
+ translator.pull_all
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :translator do
4
+ desc 'Upload changed base locale files to Smartling'
5
+ task 'push:changed' do
6
+ translator = McTranslator::Translator.new
7
+ translator.push_changed
8
+ end
9
+ task 'push' => 'push:changed'
10
+
11
+ desc 'Upload all base locale files to Smartling'
12
+ task 'push:all' do
13
+ translator = McTranslator::Translator.new
14
+ translator.push_all
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module McTranslator
4
+ VERSION = "0.1.3"
5
+ end
@@ -0,0 +1,48 @@
1
+ require 'smartling'
2
+
3
+ module Smartling
4
+ class Job < Api
5
+ def initialize(args = {})
6
+ super(args)
7
+ @project_id = args[:projectId]
8
+ end
9
+
10
+ def list
11
+ uri = uri("jobs-api/v3/projects/#{@project_id}/jobs")
12
+ return get(uri)
13
+ end
14
+
15
+ def detail(job_id)
16
+ uri = uri("/jobs-api/v3/projects/#{@project_id}/jobs/#{job_id}")
17
+ return get(uri)
18
+ end
19
+
20
+ def create(name)
21
+ keys = { jobName: name }
22
+ uri = uri("jobs-api/v3/projects/#{@project_id}/jobs", keys)
23
+ return post(uri, uri.params)
24
+ end
25
+
26
+ def find_or_create(name)
27
+ list['items'].find do |job| job['jobName'] == name end ||
28
+ create(name)
29
+ end
30
+
31
+ def files(job_id)
32
+ uri = uri("jobs-api/v3/projects/#{@project_id}/jobs/#{job_id}/files")
33
+ return get(uri)
34
+ end
35
+
36
+ def add_file(job_id, file_uri, locales)
37
+ keys = { fileUri: file_uri, targetLocaleIds: locales }
38
+ uri = uri("jobs-api/v3/projects/#{@project_id}/jobs/#{job_id}/file/add", keys)
39
+ return post(uri, uri.params)
40
+ end
41
+
42
+ def authorize(job_id)
43
+ uri = uri("jobs-api/v3/projects/#{@project_id}/jobs/#{job_id}/authorize")
44
+ return post(uri, uri.params)
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/mc_translator/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "mc_translator"
7
+ spec.version = McTranslator::VERSION
8
+ spec.authors = ["justinjones53@gmail.com"]
9
+ spec.email = ["justinjones53@gmail.com"]
10
+
11
+ spec.summary = "Translates locale files"
12
+ spec.description = "Translates locale files"
13
+ spec.homepage = "http://github.com/yankaindustries/mc_translator"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
15
+
16
+ # spec.metadata["allowed_push_host"] = "http://mygemserver.com"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "http://github.com/yankaindustries/mc_translator"
20
+ spec.metadata["changelog_uri"] = "http://github.com/yankaindustries/mc_translator"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ spec.add_dependency "git", "~> 1.8.1"
33
+ spec.add_dependency "yaml", "~> 0.1.1"
34
+ spec.add_dependency "smartling", "~> 2.0.3"
35
+
36
+ # For more information and examples about making a new gem, checkout our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mc_translator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - justinjones53@gmail.com
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: git
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.8.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.8.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: yaml
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: smartling
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.0.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.0.3
55
+ description: Translates locale files
56
+ email:
57
+ - justinjones53@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - lib/mc_translator.rb
70
+ - lib/mc_translator/tasks.rb
71
+ - lib/mc_translator/tasks/translator/pull.rake
72
+ - lib/mc_translator/tasks/translator/push.rake
73
+ - lib/mc_translator/version.rb
74
+ - lib/smartling/job.rb
75
+ - mc_translator.gemspec
76
+ homepage: http://github.com/yankaindustries/mc_translator
77
+ licenses: []
78
+ metadata:
79
+ homepage_uri: http://github.com/yankaindustries/mc_translator
80
+ source_code_uri: http://github.com/yankaindustries/mc_translator
81
+ changelog_uri: http://github.com/yankaindustries/mc_translator
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 2.4.0
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.2.15
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Translates locale files
101
+ test_files: []