i18n-tasks 0.4.0 → 0.4.1

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
  SHA1:
3
- metadata.gz: efe7070e53b66680ce9137f81c21755823c27cb2
4
- data.tar.gz: 925f44e4027a20cff615a97b2ad2dcf3c8e10ff1
3
+ metadata.gz: 71be19b5936a14fe104e5637369097c2a835b57a
4
+ data.tar.gz: bf6e3c088c8f202032c81f71782acff219465c0c
5
5
  SHA512:
6
- metadata.gz: e079e4bb84bde0365ac3628c63670317b7827db35fc1eef6789e1ce3eb82144a42de4e7ace9240e737c0b08db5e711b13a7be38465881f49b5989f856beb0cf9
7
- data.tar.gz: 1878f3455bb41be8824e5dd59a235f4afafd1ca0830e1b881fc9e0f26688c50b7e7e95de97d8ee0a38f10b416f8bab7f3de4fb011237e3378586efc014e9ca81
6
+ metadata.gz: 22661c315b535b89fd04b9d8efc52781da3b5409f3474ae9ada566d98e96795ca802988c917fd300e90d889b6c6057ef4fab144d98e212d5f592e87303c57967
7
+ data.tar.gz: 1d9778f7d2ae1a3a56e76fe33ed00d7fcfb453337e224deac743b21213aee2b8f2beaadd005133abe991ea49004fe10a6c95743302f5152fa7aef378bc4ca017
data/CHANGES.md CHANGED
@@ -1,10 +1,16 @@
1
- ## 0.4.0 (not yet released)
1
+ ## 0.4.1
2
+
3
+ * Improved error messages across the board
4
+ * Fixed google translate issue with _html keys [#67](https://github.com/glebm/i18n-tasks/issues/67).
5
+
6
+ ## 0.4.0
2
7
 
3
8
  * In addition to pattern router, a new conservative router that keeps the keys in place. (See [#57](https://github.com/glebm/i18n-tasks/issues/57))
4
9
  * `i18n-tasks irb` for debugging
5
10
  * This release is a major refactoring to use real trees internally (as opposed to nested hashes).
6
11
  Real trees allow for much easier [traversal](/lib/i18n/tasks/data/tree/traversal.rb).
7
12
  With these trees, information can be associated with each node, which allows for things like the conservative router.
13
+ * Accept keys with dashes (`-`) [#64](https://github.com/glebm/i18n-tasks/issues/64).
8
14
 
9
15
  ## 0.3.11
10
16
 
@@ -1,4 +1,5 @@
1
1
  # coding: utf-8
2
+ require 'i18n/tasks/command_error'
2
3
  require 'i18n/tasks/key_pattern_matching'
3
4
  require 'i18n/tasks/logging'
4
5
  require 'i18n/tasks/plural_keys'
@@ -0,0 +1,7 @@
1
+ module I18n
2
+ module Tasks
3
+ class CommandError < StandardError
4
+ end
5
+ end
6
+ end
7
+
@@ -32,7 +32,14 @@ module I18n::Tasks
32
32
  def cmd(name, &block)
33
33
  cmds[name] = OpenStruct.new(@next_def)
34
34
  @next_def = {}
35
- define_method(name, &block)
35
+ define_method name do |*args|
36
+ begin
37
+ instance_exec *args, &block
38
+ rescue CommandError => e
39
+ log_error e.message
40
+ exit 78
41
+ end
42
+ end
36
43
  end
37
44
 
38
45
  def desc(text)
@@ -39,7 +39,7 @@ module I18n
39
39
  def adapter_for(path)
40
40
  @fn_patterns.detect { |(name, pattern, adapter)|
41
41
  ::File.fnmatch(pattern, path)
42
- } or raise "Adapter not found for #{path}. Registered adapters: #{@fn_patterns.inspect}"
42
+ } or raise CommandError.new("Adapter not found for #{path}. Registered adapters: #{@fn_patterns.inspect}")
43
43
  end
44
44
  end
45
45
  end
@@ -35,7 +35,7 @@ module I18n::Tasks
35
35
  path.gsub!(/\\\d+/) { |m| key_match[m[1..-1].to_i] }
36
36
  (out[path] ||= Set.new) << "#{locale}.#{key}"
37
37
  else
38
- raise "no route matches key. routes = #{@routes_config}, key = #{key}"
38
+ raise CommandError.new("Cannot route key #{key}. Routes are #{@routes_config.inspect}")
39
39
  end
40
40
  end
41
41
  out.each do |dest, keys|
@@ -9,12 +9,19 @@ module I18n::Tasks::GoogleTranslation
9
9
  opts[:key] = key
10
10
  end
11
11
  if opts[:key].blank?
12
- $stderr.puts(Term::ANSIColor.red Term::ANSIColor.yellow 'You may need to provide Google API key as GOOGLE_TRANSLATE_API_KEY env var or translation.api_key in config/i18n-tasks.yml.
13
- You can obtain the key at https://code.google.com/apis/console.')
12
+ warn_missing_api_key
13
+ return []
14
14
  end
15
- list.group_by { |k_v| k_v[0].end_with?('_html'.freeze) ? opts.merge(html: true) : opts.merge(format: 'text') }.map do |opts, strings|
16
- fetch_google_translations(strings, opts)
17
- end.reduce(:+)
15
+ key_idx = {}
16
+ list.each_with_index { |k_v, i| key_idx[k_v[0]] = i}
17
+ list.group_by { |k_v|
18
+ k_v[0].end_with?('_html'.freeze)
19
+ }.map do |html, slice|
20
+ t_opts = opts.merge(html ? {html: true} : {format: 'text'})
21
+ fetch_google_translations slice, t_opts
22
+ end.reduce(:+).tap { |l|
23
+ l.sort! { |a, b| key_idx[a[0]] <=> key_idx[b[0]] }
24
+ }
18
25
  end
19
26
 
20
27
  INTERPOLATION_KEY_RE = /%\{[^}]+\}/
@@ -22,6 +29,9 @@ You can obtain the key at https://code.google.com/apis/console.')
22
29
 
23
30
  def fetch_google_translations(list, opts)
24
31
  translated = EasyTranslate.translate(list.map { |l| l[1].gsub(INTERPOLATION_KEY_RE, UNTRANSLATABLE_STRING) }, opts)
32
+ if translated.blank?
33
+ raise CommandError.new('Google Translate returned no results. Make sure billing information is set at https://code.google.com/apis/console.')
34
+ end
25
35
  translated.each_with_index { |translation, i|
26
36
  if (original = list[i][1]) =~ INTERPOLATION_KEY_RE
27
37
  interpolation_keys = original.scan(INTERPOLATION_KEY_RE)
@@ -30,4 +40,11 @@ You can obtain the key at https://code.google.com/apis/console.')
30
40
  }
31
41
  list.map(&:first).zip(translated)
32
42
  end
43
+
44
+ private
45
+
46
+ def warn_missing_api_key
47
+ $stderr.puts Term::ANSIColor.red Term::ANSIColor.yellow 'Set Google API key via GOOGLE_TRANSLATE_API_KEY environmnet variable or translation.api_key in config/i18n-tasks.yml.
48
+ Get the key at https://code.google.com/apis/console.'
49
+ end
33
50
  end
@@ -13,6 +13,10 @@ module I18n::Tasks::Logging
13
13
  log_stderr Term::ANSIColor.yellow "i18n-tasks: [WARN] #{message}"
14
14
  end
15
15
 
16
+ def log_error(message)
17
+ log_stderr Term::ANSIColor.red Term::ANSIColor.bold "i18n-tasks: #{message}"
18
+ end
19
+
16
20
  def log_stderr(*args)
17
21
  STDERR.puts *args
18
22
  end
@@ -8,7 +8,7 @@ module I18n
8
8
  # normalized path
9
9
  path = File.expand_path path
10
10
  (path_root = roots.map { |path| File.expand_path path }.sort.reverse.detect { |root| path.start_with?(root + '/') }) or
11
- raise "No relative key root detected for \"#{key}\" at #{path}. Please set relative_roots in config/i18n-tasks.yml (currently set to #{relative_roots})"
11
+ raise CommandError.new("Error scanning #{path}: cannot resolve relative key \"#{key}\".\nSet relative_roots in config/i18n-tasks.yml (currently #{relative_roots.inspect})")
12
12
  # key prefix based on path
13
13
  prefix = path.gsub(%r(#{path_root}/|(\.[^/]+)*$), '').tr('/', '.').gsub(%r(\._), '.')
14
14
  "#{prefix}#{key}"
@@ -16,7 +16,7 @@ module I18n::Tasks::Scanners
16
16
  conf[:exclude] = Array(conf[:exclude])
17
17
  else
18
18
  # exclude common binary extensions by default (images and fonts)
19
- conf[:exclude] = %w(*.jpg *.png *.gif *.svg *.ico *.eot *.ttf *.woff)
19
+ conf[:exclude] = %w(*.jpg *.png *.gif *.svg *.ico *.eot *.ttf *.woff *.pdf)
20
20
  end
21
21
  end
22
22
  @record_src_loc = false
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module Tasks
3
- VERSION = '0.4.0'
3
+ VERSION = '0.4.1'
4
4
  end
5
5
  end
@@ -4,8 +4,11 @@ require 'i18n/tasks/commands'
4
4
  describe 'Google Translation' do
5
5
  include I18n::Tasks::GoogleTranslation
6
6
 
7
- TEST_STRING = 'Hello, %{user}!'
8
- TEST_RESULT = 'Hola, %{user}!'
7
+ tests = [
8
+ text_test = ['a', "Hello - %{user} O'neill!", "Hola - %{user} O'neill!"],
9
+ html_test = ['a_html', "Hello - <b>%{user} O'neill</b>", "Hola - <b>%{user} O'neill</b>"]
10
+ ]
11
+
9
12
 
10
13
  if ENV['GOOGLE_TRANSLATE_API_KEY']
11
14
  describe 'real world test' do
@@ -13,9 +16,10 @@ describe 'Google Translation' do
13
16
 
14
17
  context 'API' do
15
18
  it 'works' do
16
- expect(google_translate(
17
- [['common.hello', TEST_STRING]], from: :en, to: :es, key: ENV['GOOGLE_TRANSLATE_API_KEY']
18
- )).to eq([['common.hello', TEST_RESULT]])
19
+ # Just one test with all the cases to lower the Google bill
20
+ translations = google_translate(
21
+ tests.map { |t| t[0..1] }, from: :en, to: :es, key: ENV['GOOGLE_TRANSLATE_API_KEY'])
22
+ expect(translations).to eq(tests.map { |t| [t[0], t[2]] })
19
23
  end
20
24
  end
21
25
 
@@ -33,9 +37,15 @@ describe 'Google Translation' do
33
37
 
34
38
  it 'works' do
35
39
  in_test_app_dir do
36
- task.data[:en] = build_tree('en' => {'common' => {'hello' => TEST_STRING}})
40
+ task.data[:en] = build_tree('en' => {
41
+ 'common' => {
42
+ 'hello' => text_test[1],
43
+ 'hello_html' => html_test[1]
44
+ }
45
+ })
37
46
  cmd.translate_missing
38
- expect(task.t('common.hello', 'es')).to eq(TEST_RESULT)
47
+ expect(task.t('common.hello', 'es')).to eq(text_test[2])
48
+ expect(task.t('common.hello_html', 'es')).to eq(html_test[2])
39
49
  end
40
50
  end
41
51
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-tasks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - glebm
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-06 00:00:00.000000000 Z
11
+ date: 2014-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erubis
@@ -206,6 +206,7 @@ files:
206
206
  - i18n-tasks.gemspec
207
207
  - lib/i18n/tasks.rb
208
208
  - lib/i18n/tasks/base_task.rb
209
+ - lib/i18n/tasks/command_error.rb
209
210
  - lib/i18n/tasks/commands.rb
210
211
  - lib/i18n/tasks/commands_base.rb
211
212
  - lib/i18n/tasks/configuration.rb