i18n-tasks 0.6.0 → 0.6.1
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 +4 -4
- data/CHANGES.md +4 -0
- data/README.md +1 -1
- data/lib/i18n/tasks/fill_tasks.rb +1 -1
- data/lib/i18n/tasks/google_translation.rb +42 -39
- data/lib/i18n/tasks/missing_keys.rb +4 -4
- data/lib/i18n/tasks/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 334c00605d3e55a82e36c72359bdf31a17bd54f2
|
4
|
+
data.tar.gz: f0240ff3e7452b1509c18cc0b5c33f6eecd8b7ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53a6c2668b4a31bc8049b2c0b08ea4c76aa0e30e6740c83a70878820965a77efa8d47f2639407b21e8b306ea6a9e581c344933b484f737a650dd4f3df0991392
|
7
|
+
data.tar.gz: 9a50045d591c8aa773e31e2223739eb814ab9010e8a7d6f9b603c76fe0bbd791c643ebbbd8acc1954eec3b241febefaf652a4f67a7514b59552dacaf68d2f9fb
|
data/CHANGES.md
CHANGED
data/README.md
CHANGED
@@ -127,7 +127,7 @@ Inspect configuration with `i18n-tasks config`.
|
|
127
127
|
Install the default config file with:
|
128
128
|
|
129
129
|
```bash
|
130
|
-
cp $(i18n-tasks gem-path)/templates/config/i18n-tasks.yml
|
130
|
+
cp $(i18n-tasks gem-path)/templates/config/i18n-tasks.yml config/
|
131
131
|
```
|
132
132
|
|
133
133
|
### Locales
|
@@ -20,7 +20,7 @@ module I18n::Tasks
|
|
20
20
|
from = opts[:from] || base_locale
|
21
21
|
locales = (Array(opts[:locales]).presence || self.locales) - [from]
|
22
22
|
locales.each do |locale|
|
23
|
-
keys = missing_tree(locale, from).key_names.map(&:to_s)
|
23
|
+
keys = missing_tree(locale, from, false).key_names.map(&:to_s)
|
24
24
|
values = google_translate(keys.zip(keys.map(&t_proc(from))), to: locale, from: from).map(&:last)
|
25
25
|
|
26
26
|
data[locale] = data[locale].merge! Data::Tree::Node.new(
|
@@ -1,51 +1,54 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
require 'easy_translate'
|
3
3
|
|
4
|
-
module I18n::Tasks
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
opts[:key] =
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
4
|
+
module I18n::Tasks
|
5
|
+
module GoogleTranslation
|
6
|
+
# @param [Array] list of [key, value] pairs
|
7
|
+
def google_translate(list, opts)
|
8
|
+
return [] if list.empty?
|
9
|
+
opts = opts.dup
|
10
|
+
if !opts[:key] && (key = translation_config[:api_key]).present?
|
11
|
+
opts[:key] = key
|
12
|
+
end
|
13
|
+
if opts[:key].blank?
|
14
|
+
warn_missing_api_key
|
15
|
+
return []
|
16
|
+
end
|
17
|
+
key_idx = {}
|
18
|
+
list.each_with_index { |k_v, i| key_idx[k_v[0]] = i }
|
19
|
+
list.group_by { |k_v|
|
20
|
+
k_v[0].end_with?('_html'.freeze)
|
21
|
+
}.map do |html, slice|
|
22
|
+
t_opts = opts.merge(html ? {html: true} : {format: 'text'})
|
23
|
+
fetch_google_translations slice, t_opts
|
24
|
+
end.reduce(:+).tap { |l|
|
25
|
+
l.sort! { |a, b| key_idx[a[0]] <=> key_idx[b[0]] }
|
26
|
+
}
|
15
27
|
end
|
16
|
-
key_idx = {}
|
17
|
-
list.each_with_index { |k_v, i| key_idx[k_v[0]] = i}
|
18
|
-
list.group_by { |k_v|
|
19
|
-
k_v[0].end_with?('_html'.freeze)
|
20
|
-
}.map do |html, slice|
|
21
|
-
t_opts = opts.merge(html ? {html: true} : {format: 'text'})
|
22
|
-
fetch_google_translations slice, t_opts
|
23
|
-
end.reduce(:+).tap { |l|
|
24
|
-
l.sort! { |a, b| key_idx[a[0]] <=> key_idx[b[0]] }
|
25
|
-
}
|
26
|
-
end
|
27
28
|
|
28
|
-
|
29
|
-
|
29
|
+
INTERPOLATION_KEY_RE = /%\{[^}]+\}/
|
30
|
+
UNTRANSLATABLE_STRING = 'zxzxzx'
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
translated.each_with_index { |translation, i|
|
37
|
-
if (original = list[i][1]) =~ INTERPOLATION_KEY_RE
|
38
|
-
interpolation_keys = original.scan(INTERPOLATION_KEY_RE)
|
39
|
-
i = -1; translation.gsub!(Regexp.new(UNTRANSLATABLE_STRING, Regexp::IGNORECASE)) { interpolation_keys[i += 1] }
|
32
|
+
def fetch_google_translations(list, opts)
|
33
|
+
translated = EasyTranslate.translate(list.map { |l| l[1].gsub(INTERPOLATION_KEY_RE, UNTRANSLATABLE_STRING) }, opts)
|
34
|
+
if translated.blank?
|
35
|
+
raise CommandError.new('Google Translate returned no results. Make sure billing information is set at https://code.google.com/apis/console.')
|
40
36
|
end
|
41
|
-
|
42
|
-
|
43
|
-
|
37
|
+
translated.each_with_index { |translation, i|
|
38
|
+
if (original = list[i][1]) =~ INTERPOLATION_KEY_RE
|
39
|
+
interpolation_keys = original.scan(INTERPOLATION_KEY_RE)
|
40
|
+
i = -1
|
41
|
+
translation.gsub!(Regexp.new(UNTRANSLATABLE_STRING, Regexp::IGNORECASE)) { interpolation_keys[i += 1] }
|
42
|
+
end
|
43
|
+
}
|
44
|
+
list.map(&:first).zip(translated)
|
45
|
+
end
|
44
46
|
|
45
|
-
|
47
|
+
private
|
46
48
|
|
47
|
-
|
48
|
-
|
49
|
+
def warn_missing_api_key
|
50
|
+
$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.
|
49
51
|
Get the key at https://code.google.com/apis/console.'
|
52
|
+
end
|
50
53
|
end
|
51
54
|
end
|
@@ -50,19 +50,19 @@ module I18n::Tasks
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
def missing_tree(locale, compared_to)
|
53
|
+
def missing_tree(locale, compared_to, collapse_plural = true)
|
54
54
|
if locale == compared_to
|
55
55
|
missing_used_tree locale
|
56
56
|
else
|
57
|
-
missing_diff_tree locale, compared_to
|
57
|
+
missing_diff_tree locale, compared_to, collapse_plural
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
61
|
# keys present in compared_to, but not in locale
|
62
|
-
def missing_diff_tree(locale, compared_to = base_locale)
|
62
|
+
def missing_diff_tree(locale, compared_to = base_locale, collapse_plural = true)
|
63
63
|
data[compared_to].select_keys { |key, _node|
|
64
64
|
locale_key_missing?(locale, key)
|
65
|
-
}.set_root_key(locale, type: :missing_diff).tap { |t| collapse_plural_nodes!(t) }
|
65
|
+
}.set_root_key(locale, type: :missing_diff).tap { |t| collapse_plural_nodes!(t) if collapse_plural }
|
66
66
|
end
|
67
67
|
|
68
68
|
# keys used in the code missing translations in locale
|
data/lib/i18n/tasks/version.rb
CHANGED
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.6.
|
4
|
+
version: 0.6.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-07-
|
11
|
+
date: 2014-07-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: erubis
|