i18n-migrations 1.1.0 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a61642308e6f7429d13a4c6f1b0f6789b75af51091b3966474c72fe1834e79f9
4
- data.tar.gz: 046affdbc02080b90a1d18e0b89f668a025e404e25016cab3036fa7151c1680f
3
+ metadata.gz: f6b1e9a88178e490e8baba639086d36979b29d071c21fa370fb735ba50bb59b5
4
+ data.tar.gz: 8e7d1a53fa84cb226edec7bd5fdf60149b4935cf3d0030ebef6cc8d75846f815
5
5
  SHA512:
6
- metadata.gz: b2383e487b6aff057aa3e78d831b692344bab7058ce9b94df3ee95b9e559fd9bbeb8112c5b75315097d5473679380f1f1ee9a0e46beb83b045ad2d1a501da5ed
7
- data.tar.gz: ee172e7cda5416a57e31370dc694c5df868217b780a6de51b3eb99e6389d298e6f82d8c9997faaf19e9b9b6c1c821be1066714b82c9461e2d4bb659cda1dec04
6
+ metadata.gz: 43f7eddfd78658cc5f25ba43d7169928e9f53505ab01df23fb2e83310c05954d9b997827cfad186fc7abf76a7183c5addc8b13d156f80b5efad523743975d14a
7
+ data.tar.gz: dab17959080db63b69f5a8ed32733b2769bd35592c52334c3e6624db16c96719ce38d82a3a91ff7f915d623652817dffe4447806d2b56c81bc837637453c05cc
data/bin/i18n-migrate CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative '../lib/i18n/migrations/migrator'
4
4
  require_relative '../lib/i18n/migrations/config'
5
+ require_relative '../lib/i18n/migrations/version'
5
6
 
6
7
  migrator = I18n::Migrations::Migrator.new
7
8
 
@@ -45,6 +46,13 @@ case ARGV.shift
45
46
  force = extract_option('-f')
46
47
  migrator.push(ARGV[0] || 'all', force)
47
48
 
49
+ when 'exp-pull'
50
+ migrator.exp_pull(ARGV[0] || 'all')
51
+
52
+ when 'exp-push'
53
+ force = extract_option('-f')
54
+ migrator.exp_push(ARGV[0] || 'all', force)
55
+
48
56
  when 'validate'
49
57
  migrator.validate(ARGV[0] || 'all')
50
58
 
@@ -62,7 +70,7 @@ case ARGV.shift
62
70
 
63
71
  else
64
72
  puts <<-USAGE
65
- Usage: im [command]
73
+ Usage: i18n-migrate [command]
66
74
 
67
75
  Commands:
68
76
  setup - Setup a new project w/ i18n-migrations.
@@ -76,8 +84,6 @@ Commands:
76
84
  new_locale - Copy your current main locale file to a new language, translating all keys.
77
85
  version - Print version of locales.
78
86
 
87
+ i18n-migrations version #{I18n::Migrations::VERSION}
79
88
  USAGE
80
- puts
81
- puts "Commands:"
82
- puts
83
89
  end
@@ -0,0 +1,45 @@
1
+ require 'rest_client'
2
+
3
+ module I18n
4
+ module Migrations
5
+ class CrowdTranslateClient
6
+ def sync_migrations(migrations)
7
+ local_versions = migrations.all_versions
8
+ remote_versions = JSON.parse(get('/migrations.json'))
9
+
10
+ if (extra_versions = remote_versions - local_versions).present?
11
+ raise("You may not upload migrations to the server because it has migrations not found locally: " +
12
+ "#{extra_versions.join(', ')}")
13
+ end
14
+
15
+ if (versions_to_add = local_versions - remote_versions).present?
16
+ versions_to_add.each do |version|
17
+ put("/migrations/#{version}.json",
18
+ ruby: migrations.get_migration(version: version))
19
+ end
20
+ end
21
+ end
22
+
23
+ def play_all_migrations
24
+
25
+ end
26
+
27
+ def get_locale_file(locale_code)
28
+ get("/locales/#{locale_code}.yml")
29
+ end
30
+
31
+ private
32
+
33
+ def get(url)
34
+ response = RestClient.get "https://crowd-translate.herokuapp.com#{url}"
35
+ response.body
36
+ end
37
+
38
+ def put(url, params = {})
39
+ response = RestClient.put "https://crowd-translate.herokuapp.com#{url}",
40
+ params: params
41
+ response.body
42
+ end
43
+ end
44
+ end
45
+ end
@@ -12,14 +12,11 @@ module I18n
12
12
  def lookup(term, key: nil)
13
13
  return [term, ''] if @from_locale == @to_locale
14
14
 
15
- response = RestClient.get 'https://www.googleapis.com/language/translate/v2', {
16
- accept: :json,
17
- params: { key: @key,
18
- source: @from_locale,
19
- target: @to_locale,
20
- format: format(key),
21
- q: term }
22
- }
15
+ response = google_translate(key: @key,
16
+ source: @from_locale,
17
+ target: @to_locale,
18
+ format: format(key),
19
+ q: term)
23
20
  translated_term = JSON.parse(response.body)['data']['translations'].first['translatedText']
24
21
  translated_term, errors = fix(term, translated_term, key: key)
25
22
  unless errors.empty?
@@ -135,6 +132,22 @@ module I18n
135
132
  end
136
133
  nil
137
134
  end
135
+
136
+ def google_translate(key:, source:, target:, format:, q:)
137
+ params = { key: key,
138
+ source: source,
139
+ target: target,
140
+ format: format,
141
+ q: q }
142
+ url = 'https://www.googleapis.com/language/translate/v2'
143
+ begin
144
+ RestClient.get url, { accept: :json, params: params }
145
+ rescue Exception
146
+ puts "Google Translate Error: #{$!.message}"
147
+ puts " #{url}?#{params.map { |k, v| [k, URI.escape(v.to_s)].join('=') }.join('&')}"
148
+ raise
149
+ end
150
+ end
138
151
  end
139
152
  end
140
153
  end
@@ -2,6 +2,7 @@ require 'fileutils'
2
2
  require 'yaml'
3
3
  require 'active_support'
4
4
  require 'colorize'
5
+ require 'rest_client'
5
6
 
6
7
  # this class does all the work, but doesn't hold config or do more than one locale
7
8
  module I18n
@@ -16,6 +17,7 @@ module I18n
16
17
  end
17
18
 
18
19
  def validate(data, notes)
20
+ fix_count, error_count = 0, 0
19
21
  main_data = main_locale.read_data
20
22
  main_data.each do |key, main_term|
21
23
  old_term = data[key]
@@ -27,6 +29,7 @@ module I18n
27
29
  puts "#{@name} (old): #{old_term}"
28
30
  puts "#{@name} (new): #{new_term}"
29
31
  puts
32
+ fix_count += 1
30
33
  end
31
34
  replace_errors_in_notes(notes, key, errors)
32
35
  if errors.length > 0
@@ -34,8 +37,12 @@ module I18n
34
37
  puts "#{@main_locale_name.bold}: #{main_term}"
35
38
  puts "#{@name.bold}: #{old_term}"
36
39
  puts
40
+ error_count += 1
37
41
  end
38
42
  end
43
+
44
+ puts "#{name}: #{fix_count} Fixes" if fix_count > 0
45
+ puts "#{name}: #{error_count} Errors" if error_count > 0
39
46
  end
40
47
 
41
48
  def update_info
@@ -92,6 +99,14 @@ module I18n
92
99
  puts "\n#{count} keys"
93
100
  end
94
101
 
102
+ def pull_from_crowd_translate(client)
103
+ data = client.get_locale_file(name)
104
+ File.open(File.join(@locales_dir, "#{name}.yml"), 'w') do |file|
105
+ file << data
106
+ end
107
+ write_remote_version(YAML::load(data)[name])
108
+ end
109
+
95
110
  def push(sheet)
96
111
  main_data = main_locale.read_data
97
112
  data, notes = read_data_and_notes
@@ -119,11 +134,14 @@ module I18n
119
134
  count = 0
120
135
  main_data = main_locale.read_data
121
136
  main_data.each do |key, term|
122
- new_data[key], new_notes[key] = @dictionary.lookup(term, key: key)
137
+ if key == 'VERSION'
138
+ new_data['VERSION'] = main_data['VERSION']
139
+ else
140
+ new_data[key], new_notes[key] = @dictionary.lookup(term, key: key)
141
+ end
123
142
  print '.'.green
124
143
  break if limit && limit < (count += 1)
125
144
  end
126
- new_data['VERSION'] = main_data['VERSION']
127
145
  puts
128
146
  write_data_and_notes(new_data, new_notes)
129
147
  end
@@ -166,17 +184,19 @@ module I18n
166
184
  end
167
185
 
168
186
  def write_data_and_notes(data, notes)
169
- write_to_file("#{@name}.yml", data)
187
+ write_data(data)
170
188
  write_to_file("../#{@name}_notes.yml", notes) unless main_locale?
171
189
  end
172
190
 
191
+ def write_data(data)
192
+ write_to_file("#{@name}.yml", data)
193
+ end
173
194
 
174
195
  def write_remote_version(data)
175
196
  write_to_file("../#{@name}_remote_version.yml",
176
197
  { 'VERSION' => read_versions(data) })
177
198
  end
178
199
 
179
-
180
200
  def migrate_to_version(data, notes, version, direction)
181
201
  migrations.play_migration(version: version,
182
202
  locale: @name,
@@ -11,6 +11,10 @@ module I18n
11
11
  Dir[@migration_dir + '/*.rb'].map { |name| File.basename(name).gsub('.rb', '') }
12
12
  end
13
13
 
14
+ def get_migration(version:)
15
+ File.read(File.join(@migration_dir, "#{version}.rb"))
16
+ end
17
+
14
18
  def play_migration(version:, locale:, data:, notes:, dictionary:, direction:)
15
19
  filename = File.join(@migration_dir, "#{version}.rb")
16
20
  require filename
@@ -4,12 +4,12 @@ require 'active_support/inflector'
4
4
  require 'active_support/core_ext/object'
5
5
  require 'colorize'
6
6
 
7
- $LOAD_PATH.unshift(File.dirname(__FILE__))
8
- require 'google_translate_dictionary'
9
- require 'google_spreadsheet'
10
- require 'config'
11
- require 'locale'
12
- require 'migration_factory'
7
+ require 'i18n/migrations/google_translate_dictionary'
8
+ require 'i18n/migrations/google_spreadsheet'
9
+ require 'i18n/migrations/config'
10
+ require 'i18n/migrations/locale'
11
+ require 'i18n/migrations/migration_factory'
12
+ require 'i18n/migrations/crowd_translate_client'
13
13
 
14
14
  CONCURRENT_THREADS = 4
15
15
 
@@ -22,7 +22,7 @@ module I18n
22
22
  Locale.new(name,
23
23
  locales_dir: config.locales_dir,
24
24
  main_locale_name: config.main_locale,
25
- migrations: MigrationFactory.new(config.migration_dir),
25
+ migrations: new_migrations,
26
26
  dictionary: new_dictionary(name))
27
27
  end
28
28
 
@@ -96,6 +96,21 @@ end
96
96
  end
97
97
  end
98
98
 
99
+ def exp_pull(locale_or_all)
100
+ client = new_crowd_translate_client
101
+ each_locale(locale_or_all) do |locale|
102
+ locale.pull_from_crowd_translate(client)
103
+ migrate(locale.name)
104
+ end
105
+ end
106
+
107
+ def exp_push(locale_or_all, force = false)
108
+ client = new_crowd_translate_client
109
+ client.sync_migrations(new_migrations)
110
+ client.play_all_migrations
111
+ exp_pull(locale_or_all)
112
+ end
113
+
99
114
  def new_locale(new_locale, limit = nil)
100
115
  locale_for(new_locale).create
101
116
  end
@@ -124,7 +139,7 @@ end
124
139
  locale_names.each_slice(CONCURRENT_THREADS) do |some_locale_names|
125
140
  threads = some_locale_names.map do |l|
126
141
  locale = locale_for(l)
127
- Thread.new {yield locale}
142
+ Thread.new { yield locale }
128
143
  end
129
144
  threads.each(&:join)
130
145
  end
@@ -151,6 +166,15 @@ end
151
166
  key: config.google_translate_api_key,
152
167
  do_not_translate: config.main_locale == locale ? {} : config.do_not_translate(locale))
153
168
  end
169
+
170
+ def new_migrations
171
+ MigrationFactory.new(config.migration_dir)
172
+ end
173
+
174
+ def new_crowd_translate_client
175
+ CrowdTranslateClient.new
176
+ end
177
+
154
178
  end
155
179
  end
156
180
  end
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module Migrations
3
- VERSION = "1.1.0"
3
+ VERSION = "1.1.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Lightsmith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-09 00:00:00.000000000 Z
11
+ date: 2019-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -125,6 +125,7 @@ files:
125
125
  - i18n-migrations.gemspec
126
126
  - lib/i18n-migrations.rb
127
127
  - lib/i18n/migrations/config.rb
128
+ - lib/i18n/migrations/crowd_translate_client.rb
128
129
  - lib/i18n/migrations/google_spreadsheet.rb
129
130
  - lib/i18n/migrations/google_translate_dictionary.rb
130
131
  - lib/i18n/migrations/locale.rb
@@ -151,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
152
  - !ruby/object:Gem::Version
152
153
  version: '0'
153
154
  requirements: []
154
- rubygems_version: 3.0.6
155
+ rubygems_version: 3.0.3
155
156
  signing_key:
156
157
  specification_version: 4
157
158
  summary: Migrations for doing i18n.