i18n-tasks 0.2.13 → 0.2.14

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 49fa662d252e8f812eedc6b79d0ecdf4e55b6979
4
- data.tar.gz: 97deaf64038b50772fa22ea2c997f8997550fe3f
3
+ metadata.gz: 760bba77247fae6727219b0e36b605c01f760dff
4
+ data.tar.gz: 12e5076cdf6279ae466e8137c84ea7f08f155eb7
5
5
  SHA512:
6
- metadata.gz: 0edb92a5f181c337826b617ca8d4d2d6d7d8a7299d6ba1fd7580eb2bf304e6789f69c842dcb8ddfbab57aa44b267e6bbf9d0852be18fb1fc33a683e8bb93dd9f
7
- data.tar.gz: 576b66877ab863f4c06f2dffb351d11968e7ea73593c87390108227ba319b3618317f5c63b84697f94d057c4276b76771d20bc92018c9d03eddfdba26a43f347
6
+ metadata.gz: 739772e09dde3095da37f5e0e47e53ce03564448479867de3b6b4bc7300d1aad9c331b7182ce28f942e29032b6ba30524e398b627ae04d6ce2a05373c09a321c
7
+ data.tar.gz: 39e7fad91f6269ab0aff4e60ef9d44a6e48baa34c134b177e21974a2aaf2828382af8b640817e6a53efd482531742bcb788a1ce0d8727cb6d0f661b526acccb1
data/CHANGES.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## v0.2.14.
2
+
3
+ * Google Translate fixes: preserve interpolations, set correct format based on the key (text or html).
4
+
5
+ ## v0.2.13
6
+
7
+ * New setting relative_roots for relative key resolution (default: %w(app/views))
8
+ * fix google translation attempts to translate non-string keys
9
+
1
10
  ## v0.2.11 .. v0.2.12
2
11
 
3
12
  * New task: `i18n:remove_unused`
data/Gemfile CHANGED
@@ -1,5 +1,5 @@
1
1
  source 'https://rubygems.org'
2
- #gem 'byebug'
2
+
3
3
  # Specify your gem's dependencies in i18n-tasks.gemspec
4
4
  gemspec
5
5
 
@@ -38,7 +38,6 @@ module I18n::Tasks::Configuration
38
38
  search_config = (config[:search] || {}).with_indifferent_access
39
39
  search_config.tap do |conf|
40
40
  conf[:paths] = %w(app/) if conf[:paths].blank?
41
- conf[:relative_roots] = %w( app/views ) if conf[:relative_roots].blank?
42
41
  conf[:include] = Array(conf[:include]) if conf[:include].present?
43
42
  conf[:exclude] = Array(conf[:exclude])
44
43
  conf[:pattern] = conf[:pattern].present? ? Regexp.new(conf[:pattern]) : DEFAULT_PATTERN
@@ -27,8 +27,9 @@ module I18n::Tasks::FillTasks
27
27
  tr.present? && tr.is_a?(String)
28
28
  }
29
29
  if blank_keys.present?
30
- translated = blank_keys.zip google_translate(blank_keys.map { |k| t(k) }, to: locale, from: base_locale)
31
- data[locale] = data[locale].deep_merge(list_to_tree translated)
30
+ data[locale] = data[locale].deep_merge(
31
+ list_to_tree google_translate(blank_keys.zip(blank_keys.map { |k| t(k) }), to: locale, from: base_locale)
32
+ )
32
33
  end
33
34
  end
34
35
  end
@@ -1,16 +1,33 @@
1
1
  require 'easy_translate'
2
2
 
3
3
  module I18n::Tasks::GoogleTranslation
4
- def google_translate(strings, opts)
5
- return [] if strings.empty?
4
+ # @param [Array] list of [key, value] pairs
5
+ def google_translate(list, opts)
6
+ return [] if list.empty?
6
7
  opts = opts.dup
7
- if (key = translation_config[:api_key]).present?
8
- opts[:key] ||= key
8
+ if !opts[:key] && (key = translation_config[:api_key]).present?
9
+ opts[:key] = key
9
10
  end
10
11
  if opts[:key].blank?
11
12
  $stderr.puts(Term::ANSIColor.red Term::ANSIColor.yellow 'You may need to provide Google API key as GOOGLE_TRANSLATE_API_KEY or in config/i18n-tasks.yml.
12
13
  You can obtain the key at https://code.google.com/apis/console.')
13
14
  end
14
- EasyTranslate.translate strings, opts
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(:+)
18
+ end
19
+
20
+ INTERPOLATION_KEY_RE = /%\{[^}]+\}/
21
+ UNTRANSLATABLE_STRING = 'zxzxzx'
22
+
23
+ def fetch_google_translations(list, opts)
24
+ translated = EasyTranslate.translate(list.map { |l| l[1].gsub(INTERPOLATION_KEY_RE, UNTRANSLATABLE_STRING) }, opts)
25
+ translated.each_with_index { |translation, i|
26
+ if (original = list[i][1]) =~ INTERPOLATION_KEY_RE
27
+ interpolation_keys = original.scan(INTERPOLATION_KEY_RE)
28
+ i = -1; translation.gsub!(UNTRANSLATABLE_STRING) { interpolation_keys[i += 1] }
29
+ end
30
+ }
31
+ list.map(&:first).zip(translated)
15
32
  end
16
33
  end
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module Tasks
3
- VERSION = '0.2.13'
3
+ VERSION = '0.2.14'
4
4
  end
5
5
  end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Google Translation' do
4
+ include I18n::Tasks::GoogleTranslation
5
+
6
+ if ENV['GOOGLE_TRANSLATE_API_KEY']
7
+ context 'real world test' do
8
+ it 'works' do
9
+ google_translate(
10
+ [['common.hello', "Hello, %{user}!"]], from: :en, to: :es, key: ENV['GOOGLE_TRANSLATE_API_KEY']
11
+ ).should == [['common.hello', 'Hola, %{user}!']]
12
+ end
13
+ end
14
+ end
15
+ end
@@ -12,8 +12,7 @@ describe 'Relative keys' do
12
12
 
13
13
  context 'custom roots' do
14
14
  it 'works' do
15
- task.relative_roots = %w(app/views-mobile)
16
- task.absolutize_key('.title', 'app/views-mobile/movies/show.html.slim').should == 'movies.show.title'
15
+ task.absolutize_key('.title', 'app/views-mobile/movies/show.html.slim', %w(app/views-mobile)).should == 'movies.show.title'
17
16
  end
18
17
  end
19
18
 
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.2.13
4
+ version: 0.2.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - glebm
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-12 00:00:00.000000000 Z
11
+ date: 2013-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -224,6 +224,7 @@ files:
224
224
  - spec/fixtures/app/views/index.html.slim
225
225
  - spec/fixtures/app/views/relative/index.html.slim
226
226
  - spec/fixtures/config/i18n-tasks.yml
227
+ - spec/google_translate_spec.rb
227
228
  - spec/i18n_tasks_spec.rb
228
229
  - spec/key_pattern_matching_spec.rb
229
230
  - spec/readme_spec.rb
@@ -268,6 +269,7 @@ test_files:
268
269
  - spec/fixtures/app/views/index.html.slim
269
270
  - spec/fixtures/app/views/relative/index.html.slim
270
271
  - spec/fixtures/config/i18n-tasks.yml
272
+ - spec/google_translate_spec.rb
271
273
  - spec/i18n_tasks_spec.rb
272
274
  - spec/key_pattern_matching_spec.rb
273
275
  - spec/readme_spec.rb