i18n-migrations 2.0.0 → 2.0.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: 155eb3f6fe33497d8988ba301c6535df404f0d02c59cfaac034a77a169508d62
4
- data.tar.gz: 04a5b6dcc152e3ad7b7b88bc898acae8fc2fed122947a3ae1609edcdb6023e64
3
+ metadata.gz: 2f7ee2c5ac86c602a98d44d88d9beb2b56f5982521bfd0d4d786d40b0cf5a0ab
4
+ data.tar.gz: 0a0104497bb427863c9b882ed4012d4987df4505aa31e940dcb95825db3553ea
5
5
  SHA512:
6
- metadata.gz: 324c96c71807c3fb331a25742f84d82242a0c3ea0bd12f0df7cebe9a6bdd5e0261d7830cfd843e0debe50475a948347603a6a23d93efd2d610b054d251ecc782
7
- data.tar.gz: cb47a3ffd0fc1eae8a5b1b2e6996857c1b092f705112252818c9cb3d939246bc5711fa1357ef20e32c4243091fd59793544a7cd52c0cd295d9aca5a859585d58
6
+ metadata.gz: 9e424e65f15509548832954508ecf5f691be5d8375c5b22c6d026ee6e245aedf740913cfbc194efd05da112bb8fc540c973ceb1fe58ae91f21a0d69f826fc93e
7
+ data.tar.gz: 0dcea94acec54b24bf839a0173f43cf67c7ce440ac9ad696bb08a553bc22ef408f90189fc4491567576ae6fe9afc1c389dddb7296d58e4bcaa430ed377b2acdc
data/README.md CHANGED
@@ -47,20 +47,10 @@ Let's imagine that your config file look like this:
47
47
 
48
48
  In your project file, you should then have all your english terms in ```config/locales/en.yml```
49
49
 
50
- To create a new locale (like es.yml):
51
-
52
- 1. Translate all the terms w/ google translate
53
-
54
- > i18n-migrate new_locale es
55
-
56
- 2. Create a spreadsheet that is world editable (for now). You'll want to add the link to it to your config file. It should look like:
57
-
58
- | key | en | es | notes |
59
-
60
- 2. Push this to your google spreadsheet (the -f means it won't try to pull first)
61
-
62
- > i18n-migrate push -f es
50
+ You can:
63
51
 
52
+ * [Create a new locale](https://github.com/transparentclassroom/i18n-migrations/wiki/Create-a-new-locale-(language))
53
+ * [Add a migration](https://github.com/transparentclassroom/i18n-migrations/wiki/Add-a-migration)
64
54
 
65
55
  ## Development
66
56
 
@@ -71,8 +61,9 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
71
61
  ## Contributing
72
62
 
73
63
  1. Fork it ( https://github.com/[my-github-username]/i18n-migrations/fork )
74
- 2. Create your feature branch (`git checkout -b my-new-feature`)
75
- 3. Commit your changes (`git commit -am 'Add some feature'`)
76
- 4. Push to the branch (`git push origin my-new-feature`)
77
- 5. Create a new Pull Request
64
+ 1. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 1. Make sure tests are passing (`rake`)
66
+ 1. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 1. Push to the branch (`git push origin my-new-feature`)
68
+ 1. Create a new Pull Request
78
69
 
@@ -10,59 +10,60 @@ def extract_option(name)
10
10
  !!ARGV.delete(name)
11
11
  end
12
12
 
13
- case ARGV.shift
14
- when 'setup'
15
- puts 'Where should we create a default config file? [.]'
16
- dir = gets.chomp
17
- dir = dir == '' ? '.' : dir
18
- file = I18n::Migrations::Config.copy_default_config_file(dir)
19
-
20
- puts 'You will need to configure this file before you can get going.'
21
- puts File.expand_path(file)
22
-
23
- when 'new'
24
- name = ARGV.shift
25
- if name
26
- migrator.new_migration name
27
- else
28
- STDERR.puts 'Usage: im new [name]'
29
- exit 1
30
- end
31
-
32
- when 'migrate'
33
- migrator.migrate(ARGV[0] || 'all')
34
-
35
- when 'rollback'
36
- migrator.rollback(ARGV[0] || 'all')
37
-
38
- when 'redo'
39
- migrator.rollback(ARGV[0] || 'all')
40
- migrator.migrate(ARGV[0] || 'all')
41
-
42
- when 'pull'
43
- migrator.pull(ARGV[0] || 'all')
13
+ begin
14
+ case ARGV.shift
15
+ when 'setup'
16
+ puts 'Where should we create a default config file? [.]'
17
+ dir = gets.chomp
18
+ dir = dir == '' ? '.' : dir
19
+ file = I18n::Migrations::Config.copy_default_config_file(dir)
20
+
21
+ puts 'You will need to configure this file before you can get going.'
22
+ puts File.expand_path(file)
23
+
24
+ when 'new'
25
+ name = ARGV.shift
26
+ if name
27
+ migrator.new_migration name
28
+ else
29
+ STDERR.puts 'Usage: im new [name]'
30
+ exit 1
31
+ end
32
+
33
+ when 'migrate'
34
+ migrator.migrate(ARGV[0] || 'all')
35
+
36
+ when 'rollback'
37
+ migrator.rollback(ARGV[0] || 'all')
38
+
39
+ when 'redo'
40
+ migrator.rollback(ARGV[0] || 'all')
41
+ migrator.migrate(ARGV[0] || 'all')
42
+
43
+ when 'pull'
44
+ migrator.pull(ARGV[0] || 'all')
45
+
46
+ when 'push'
47
+ force = extract_option('-f')
48
+ migrator.push(ARGV[0] || 'all', force)
49
+
50
+ when 'validate'
51
+ migrator.validate(ARGV[0] || 'all')
52
+
53
+ when 'new_locale'
54
+ locale = ARGV.shift
55
+ if locale
56
+ migrator.new_locale(locale)
57
+ else
58
+ STDERR.puts 'Usage: im new_locale [name]'
59
+ exit 1
60
+ end
61
+
62
+ when 'version'
63
+ migrator.version
44
64
 
45
- when 'push'
46
- force = extract_option('-f')
47
- migrator.push(ARGV[0] || 'all', force)
48
-
49
- when 'validate'
50
- migrator.validate(ARGV[0] || 'all')
51
-
52
- when 'new_locale'
53
- locale = ARGV.shift
54
- if locale
55
- migrator.new_locale(locale)
56
65
  else
57
- STDERR.puts 'Usage: im new_locale [name]'
58
- exit 1
59
- end
60
-
61
- when 'version'
62
- migrator.version
63
-
64
- else
65
- puts <<-USAGE
66
+ puts <<-USAGE
66
67
  Usage: i18n-migrate [command]
67
68
 
68
69
  Commands:
@@ -78,5 +79,9 @@ Commands:
78
79
  version - Print version of locales.
79
80
 
80
81
  i18n-migrations version #{I18n::Migrations::VERSION}
81
- USAGE
82
+ USAGE
83
+ end
84
+ rescue Exception
85
+ puts "Error\n#{$!.message}".red
86
+ exit 1
82
87
  end
@@ -20,11 +20,6 @@ module I18n
20
20
  force_push(locale)
21
21
  end
22
22
 
23
- # do this just once
24
- unless @migrations_synced
25
- sync_migrations(locale.migrations)
26
- @migrations_synced = true
27
- end
28
23
  pull(locale)
29
24
  end
30
25
 
@@ -58,9 +53,7 @@ module I18n
58
53
  begin
59
54
  client.put_migration(version: version, ruby_file: migrations.get_migration(version: version))
60
55
  rescue
61
- puts "There was an error updating migration:".red
62
- puts " #{migrations.migration_file(version: version)}".bold
63
- raise
56
+ raise CrowdTranslateError, "... while updating migration: #{migrations.migration_file(version: version)}\n#{$!.message}"
64
57
  end
65
58
  end
66
59
  end
@@ -4,16 +4,16 @@ require 'active_support/core_ext/object'
4
4
  module I18n
5
5
  module Migrations
6
6
  module Backends
7
+ class CrowdTranslateError < StandardError
8
+ end
9
+
7
10
  class CrowdTranslateClient
8
11
  def initialize
9
12
  token = ENV['CROWD_TRANSLATE_API_TOKEN']
10
13
  raise("You must define CROWD_TRANSLATE_API_TOKEN in order to talk to Crowd Translate") unless token.present?
11
14
 
12
- server = ENV['CROWD_TRANSLATE_SERVER'] || 'https://crowd-translate.herokuapp.com'
13
- @faraday = Faraday.new(
14
- url: "#{server}/api/v1",
15
- headers: { 'X-CrowdTranslateApiToken' => token },
16
- )
15
+ @server = ENV['CROWD_TRANSLATE_SERVER'] || 'https://crowd-translate.herokuapp.com'
16
+ @faraday = Faraday.new(url: base_url, headers: { 'X-CrowdTranslateApiToken' => token })
17
17
  end
18
18
 
19
19
  def get_locale_file(locale_code)
@@ -37,26 +37,34 @@ module I18n
37
37
 
38
38
  def get(path)
39
39
  puts "GET #{path}".bold
40
- parse_response @faraday.get path
40
+ parse_response 'GET', path, @faraday.get(path)
41
41
  end
42
42
 
43
43
  def put(path, params = {})
44
44
  puts "PUT #{path} #{params.to_s[0..50]}#{'...' if params.to_s.length > 50}".bold
45
- parse_response @faraday.put path, params
45
+ parse_response 'PUT', path, @faraday.put(path, params)
46
46
  end
47
47
 
48
- def parse_response(response)
48
+ def parse_response(method, path, response)
49
49
  if response.success?
50
50
  response.body
51
51
  else
52
- error = begin
53
- JSON.parse(response.body)['error']
54
- rescue
55
- response.body
52
+ error = if response.status == 503
53
+ 'Server timed out - your request may have succeed, check the server for more details'
54
+ else
55
+ begin
56
+ JSON.parse(response.body)['error']
57
+ rescue
58
+ response.body
59
+ end
56
60
  end
57
- raise error
61
+ raise CrowdTranslateError, "... while calling #{method} #{File.join(base_url, path)}\n#{error}"
58
62
  end
59
63
  end
64
+
65
+ def base_url
66
+ "#{@server}/api/v1"
67
+ end
60
68
  end
61
69
  end
62
70
  end
@@ -34,6 +34,10 @@ module I18n
34
34
  @config.google_service_account_key_path).sheet
35
35
  end
36
36
 
37
+ def sync_migrations(migrations)
38
+ # nothing to do here in this backend
39
+ end
40
+
37
41
  def pull_from_sheet(sheet, locale)
38
42
  puts "Pulling #{locale.name}"
39
43
  data = {}
@@ -212,7 +212,7 @@ module I18n
212
212
  end
213
213
 
214
214
  def parse_yaml(string)
215
- YAML.load(string)
215
+ YAML.load(string) || {}
216
216
  end
217
217
 
218
218
  # flattens new_hash and adds it to hash
@@ -20,7 +20,9 @@ class Metadata
20
20
  end
21
21
 
22
22
  def delete(key)
23
+ metadatum = self[key]
23
24
  @hash.delete(key)
25
+ metadatum
24
26
  end
25
27
 
26
28
  def to_h
@@ -27,15 +27,15 @@ module I18n
27
27
  raise("Can't parse version: #{version}") unless version =~ /^(\d{12})_(.*)/
28
28
  migration_class_name = "#{$2.camelcase}#{$1}"
29
29
 
30
- translations = Translations.new(data: data, metadata: metadata)
30
+ translations = Translations.new(locale_code: locale, data: data, metadata: metadata)
31
31
  migration = begin
32
- migration_class_name.constantize.new(locale_code: locale,
33
- translations: translations,
34
- dictionary: dictionary,
35
- direction: direction)
36
- rescue
37
- raise "Couldn't load migration #{migration_class_name} in #{filename}"
38
- end
32
+ migration_class_name.constantize.new(locale_code: locale,
33
+ translations: translations,
34
+ dictionary: dictionary,
35
+ direction: direction)
36
+ rescue
37
+ raise "Couldn't load migration #{migration_class_name} in #{filename}"
38
+ end
39
39
 
40
40
  migration.change
41
41
  end
@@ -44,8 +44,8 @@ module I18n
44
44
  # data = all keys -> all translations in this locale
45
45
  # metadata = some keys -> metadata about the translation in this locale
46
46
  class Translations
47
- def initialize(data:, metadata:)
48
- @data, @metadata = data, metadata
47
+ def initialize(locale_code:, data:, metadata:)
48
+ @locale_code, @data, @metadata = locale_code, data, metadata
49
49
  end
50
50
 
51
51
  def get_term(key)
@@ -63,6 +63,17 @@ module I18n
63
63
  @metadata[key].errors = errors
64
64
  @metadata[key].notes = nil
65
65
  @metadata[key].autotranslated = autotranslated
66
+ if errors.present?
67
+ puts [
68
+ "=" * 100,
69
+ "#{@locale_code}: #{key}",
70
+ value,
71
+ errors.map { |e| "Error: #{e}".red }.join("\n"),
72
+ "=" * 100,
73
+ ].compact.join("\n")
74
+ # puts ["Error with #{@locale_code}: #{key}: \n #{errors.join("\n ")}".red,
75
+ # value].compact.join("\n")
76
+ end
66
77
  end
67
78
 
68
79
  def delete_term(key)
@@ -72,12 +72,13 @@ end
72
72
 
73
73
  def pull(locale_or_all)
74
74
  each_locale(locale_or_all) do |locale|
75
- next if locale.main_locale?
75
+ # next if locale.main_locale?
76
76
  backend.pull(locale)
77
77
  end
78
78
  end
79
79
 
80
80
  def push(locale_or_all, force = false)
81
+ backend.sync_migrations(new_migrations)
81
82
  each_locale(locale_or_all, concurrency: config.push_concurrency) do |locale|
82
83
  backend.push(locale, force: force)
83
84
  wait
@@ -103,9 +104,23 @@ end
103
104
  end
104
105
  end
105
106
 
107
+ private def report_locale_on_error(locale, &block)
108
+ begin
109
+ block.call locale
110
+ rescue Backends::CrowdTranslateError
111
+ puts "Error\n... while working with #{locale.name}\n#{$!.message}".red
112
+ # we want a readable error for our users, and the error happened in an external system,
113
+ # so don't give them a stack trace
114
+ rescue
115
+ puts "Error\n... while working with #{locale.name}\n#{$!.message}".red
116
+ raise
117
+ end
118
+ end
119
+
106
120
  private def each_locale(name = 'all',
107
- async: config.google_spreadsheet?,
108
- concurrency: config.concurrency)
121
+ async: true,
122
+ concurrency: config.concurrency,
123
+ &block)
109
124
  locale_names = name == 'all' ? all_locale_names : [name]
110
125
 
111
126
  if async
@@ -113,13 +128,13 @@ end
113
128
  locale_names.each_slice(concurrency) do |some_locale_names|
114
129
  threads = some_locale_names.map do |l|
115
130
  locale = locale_for(l)
116
- Thread.new { yield locale }
131
+ Thread.new { report_locale_on_error(locale, &block) }
117
132
  end
118
133
  threads.each(&:join)
119
134
  end
120
135
  else
121
136
  locale_names.each do |l|
122
- yield locale_for(l)
137
+ report_locale_on_error(locale_for(l), &block)
123
138
  end
124
139
  end
125
140
  end
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module Migrations
3
- VERSION = "2.0.0"
3
+ VERSION = "2.0.5"
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: 2.0.0
4
+ version: 2.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Lightsmith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-18 00:00:00.000000000 Z
11
+ date: 2020-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler