i18n-migrations 1.1.4 → 1.1.5

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: e0f8d5372c9541fdf212f771cbf6fa9f8159fb4f6fb5dab37beca63862cf2deb
4
- data.tar.gz: 6f6d8dc7c053da0500415dbe8999bd015eec3023d92898c177b54af5962b4fc6
3
+ metadata.gz: b28ece541b9bbbaeb46aa30e606571ee8a8d4126ee76b8950692c23677fe8eb2
4
+ data.tar.gz: 5692f437d5e61bfc8dd758335c5f3b3d27c910b2a1336eb95b1e100ab65fe7bf
5
5
  SHA512:
6
- metadata.gz: 905e9f53dda1e46c7a169ffe869260854964fa323baf030d71f8c9d76d16ba40db658b0235da346256123c8a3e21d0805beef3c055c6efc733e8070a02f0403f
7
- data.tar.gz: f3f1cec60dfb164bc0d61e9a81004eb9ebd527568613af170f9d3429e15bf9ddc1803f8258476c5c621e299e3ee87354067e361e34ee29b37937dc8ee74827f3
6
+ metadata.gz: 4daa370126c01254c4d436120247f018eb32218e6ba9ee655d09eeaf0e00dc252949d66e160806c8304b878dbb0dd9bba3d1a8d1daa585b00c895066ca89d086
7
+ data.tar.gz: 7086d6183999b34bed8507a7b5596dfcb6b247cb813352927f0721fd03ba1bb440cb4c97633ae63d8f1bfec85b579af5271ab4e005ba1edbb0bb4390c20782ae
@@ -4,6 +4,15 @@ migration_dir: i18n/migrate
4
4
  # this is where your locale files will live (en.yml, es.yml, etc). it will be relative to your config file
5
5
  locales_dir: config/locales
6
6
 
7
+ # number of threads to concurrently perform migration operations for locales (optional)
8
+ # concurrency: 4
9
+
10
+ # number of threads to concurrently perform push operation for locales (optional)
11
+ # push_concurrency: 4
12
+
13
+ # seconds to wait time between push operations per thread, can be used to avoid API throttling (optional)
14
+ # wait_seconds: 0
15
+
7
16
  # this is the locale you will be translating from
8
17
  main_locale: en
9
18
 
@@ -1,3 +1,2 @@
1
1
  language: ruby
2
- rvm:
3
- - 2.0.0
2
+ cache: bundler
data/Rakefile CHANGED
@@ -1 +1,10 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ # ignore - like in prod
8
+ end
9
+
10
+ task :default => :spec
@@ -4,6 +4,8 @@ module I18n
4
4
  module Migrations
5
5
  class Config
6
6
  CONFIG_FILE_NAME = '.i18n-migrations.yml'
7
+ DEFAULT_CONCURRENCY = 4
8
+ DEFAULT_WAIT_SECONDS = 0
7
9
 
8
10
  def initialize(config_file_name = CONFIG_FILE_NAME)
9
11
  @config_file_name = config_file_name
@@ -43,6 +45,18 @@ module I18n
43
45
  get_value(:google_translate_api_key)
44
46
  end
45
47
 
48
+ def concurrency
49
+ get_value(:concurrency, DEFAULT_CONCURRENCY)
50
+ end
51
+
52
+ def push_concurrency
53
+ get_value(:push_concurrency, concurrency)
54
+ end
55
+
56
+ def wait_seconds
57
+ get_value(:wait_seconds, DEFAULT_WAIT_SECONDS)
58
+ end
59
+
46
60
  def read!
47
61
  yaml_file = find_config_file(@config_file_name)
48
62
  unless yaml_file
@@ -72,7 +86,7 @@ module I18n
72
86
 
73
87
  private
74
88
 
75
- def get_value(key)
89
+ def get_value(key, default = nil)
76
90
  if key.is_a?(Array)
77
91
  value = @config
78
92
  key.each do |key_part|
@@ -85,6 +99,8 @@ module I18n
85
99
  value
86
100
  elsif @config.has_key?(key.to_s)
87
101
  @config[key.to_s]
102
+ elsif default.present?
103
+ default
88
104
  else
89
105
  raise ArgumentError, "You must have defined #{key} in #{@root_dir}/#{@config_file_name}"
90
106
  end
@@ -1,4 +1,5 @@
1
1
  require 'faraday'
2
+ require 'active_support/core_ext/object'
2
3
 
3
4
  module I18n
4
5
  module Migrations
@@ -1,4 +1,5 @@
1
- require 'rest-client'
1
+ require 'faraday'
2
+ require 'active_support/core_ext/object'
2
3
 
3
4
  module I18n
4
5
  module Migrations
@@ -141,7 +142,10 @@ module I18n
141
142
  q: q }
142
143
  url = 'https://www.googleapis.com/language/translate/v2'
143
144
  begin
144
- RestClient.get url, { accept: :json, params: params }
145
+ Faraday.get(url) do |req|
146
+ req.headers['Content-Type'] = 'application/json'
147
+ req.params = params
148
+ end
145
149
  rescue Exception
146
150
  puts "Google Translate Error: #{$!.message}"
147
151
  puts " #{url}?#{params.map { |k, v| [k, URI.escape(v.to_s)].join('=') }.join('&')}"
@@ -2,6 +2,7 @@ require 'fileutils'
2
2
  require 'yaml'
3
3
  require 'active_support'
4
4
  require 'colorize'
5
+ require 'active_support/core_ext/object'
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
@@ -1,4 +1,5 @@
1
1
  require 'active_support/inflector'
2
+ require 'active_support/core_ext/object'
2
3
 
3
4
  module I18n
4
5
  module Migrations
@@ -11,8 +11,6 @@ require 'i18n/migrations/locale'
11
11
  require 'i18n/migrations/migration_factory'
12
12
  require 'i18n/migrations/crowd_translate_client'
13
13
 
14
- CONCURRENT_THREADS = 3
15
-
16
14
  # this class knows how to do all the things the cli needs done.
17
15
  # it mostly delegates to locale to do it, often asking multiple locales to do the same thing
18
16
  module I18n
@@ -84,7 +82,7 @@ end
84
82
  end
85
83
 
86
84
  def push(locale_or_all, force = false)
87
- each_locale(locale_or_all) do |locale|
85
+ each_locale(locale_or_all, concurrency: config.push_concurrency) do |locale|
88
86
  next if locale.main_locale?
89
87
  sheet = get_google_spreadsheet(locale.name)
90
88
  unless force
@@ -92,7 +90,7 @@ end
92
90
  migrate(locale.name)
93
91
  end
94
92
  locale.push(sheet)
95
- sleep 4
93
+ wait
96
94
  end
97
95
  end
98
96
 
@@ -130,13 +128,12 @@ end
130
128
  end
131
129
  end
132
130
 
133
- private
134
-
135
- def each_locale(name = 'all', async: true)
131
+ private def each_locale(name = 'all', async: true, concurrency: config.concurrency)
136
132
  locale_names = name == 'all' ? all_locale_names : [name]
137
133
 
134
+ puts "Using #{concurrency} concurrency"
138
135
  if async
139
- locale_names.each_slice(CONCURRENT_THREADS) do |some_locale_names|
136
+ locale_names.each_slice(concurrency) do |some_locale_names|
140
137
  threads = some_locale_names.map do |l|
141
138
  locale = locale_for(l)
142
139
  Thread.new { yield locale }
@@ -150,31 +147,37 @@ end
150
147
  end
151
148
  end
152
149
 
153
- def all_locale_names
150
+ private def all_locale_names
154
151
  [config.main_locale] + config.other_locales
155
152
  end
156
153
 
157
- def get_google_spreadsheet(locale)
154
+ private def get_google_spreadsheet(locale)
158
155
  GoogleSpreadsheet.new(locale,
159
156
  config.google_spreadsheet(locale),
160
157
  config.google_service_account_key_path).sheet
161
158
  end
162
159
 
163
- def new_dictionary(locale)
160
+ private def new_dictionary(locale)
164
161
  GoogleTranslateDictionary.new(from_locale: config.main_locale,
165
162
  to_locale: locale,
166
163
  key: config.google_translate_api_key,
167
164
  do_not_translate: config.main_locale == locale ? {} : config.do_not_translate(locale))
168
165
  end
169
166
 
170
- def new_migrations
167
+ private def new_migrations
171
168
  MigrationFactory.new(config.migration_dir)
172
169
  end
173
170
 
174
- def new_crowd_translate_client
171
+ private def new_crowd_translate_client
175
172
  CrowdTranslateClient.new
176
173
  end
177
174
 
175
+ private def wait
176
+ if config.wait_seconds > 0
177
+ puts "Pausing #{config.wait_seconds}s to not run into Google Translate API throttling..."
178
+ sleep config.wait_seconds
179
+ end
180
+ end
178
181
  end
179
182
  end
180
183
  end
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module Migrations
3
- VERSION = "1.1.4"
3
+ VERSION = "1.1.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Lightsmith