Module: I18n::Processes::GoogleTranslation
- Included in:
- BaseProcess
- Defined in:
- lib/i18n/processes/google_translation.rb
Instance Method Summary collapse
-
#fetch_google_translations(list, opts) ⇒ Array<[String, Object]>
Translated list.
-
#google_translate_forest(forest, from) ⇒ I18n::Processes::Tree::Siblings
Translated forest.
-
#google_translate_list(list, opts) ⇒ Array<[String, Object]>
Translated list.
Instance Method Details
#fetch_google_translations(list, opts) ⇒ Array<[String, Object]>
Returns translated list.
39 40 41 42 43 |
# File 'lib/i18n/processes/google_translation.rb', line 39 def fetch_google_translations(list, opts) from_values(list, EasyTranslate.translate(to_values(list), opts)).tap do |result| fail CommandError, 'Google Translate returned no results. Make sure billing information is set at https://code.google.com/apis/console.' if result.blank? end end |
#google_translate_forest(forest, from) ⇒ I18n::Processes::Tree::Siblings
Returns translated forest.
11 12 13 14 15 16 |
# File 'lib/i18n/processes/google_translation.rb', line 11 def google_translate_forest(forest, from) forest.inject empty_forest do |result, root| translated = google_translate_list(root.key_values(root: true), to: root.key, from: from) result.merge! Data::Tree::Siblings.from_flat_pairs(translated) end end |
#google_translate_list(list, opts) ⇒ Array<[String, Object]>
Returns translated list.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/i18n/processes/google_translation.rb', line 20 def google_translate_list(list, opts) # rubocop:disable Metrics/AbcSize return [] if list.empty? opts = opts.dup opts[:key] ||= translation_config[:api_key] validate_google_translate_api_key! opts[:key] key_pos = list.each_with_index.inject({}) { |idx, ((k, _v), i)| idx.update(k => i) } # copy reference keys as is, instead of translating reference_key_vals = list.select { |_k, v| v.is_a? Symbol } || [] list -= reference_key_vals result = list.group_by { |k_v| html_key? k_v[0], opts[:from] }.map do |is_html, list_slice| fetch_google_translations list_slice, opts.merge(is_html ? { html: true } : { format: 'text' }) end.reduce(:+) || [] result.concat(reference_key_vals) result.sort! { |a, b| key_pos[a[0]] <=> key_pos[b[0]] } result end |