Sha256: 9625f1b8567cf5f6bce297871be0b9305639d1cf1c14064a3bf6144832e007a2

Contents?: true

Size: 846 Bytes

Versions: 7

Compression:

Stored size: 846 Bytes

Contents

# -*- encoding : utf-8 -*-
require 'multi_json'
require 'rest-client'

module AbAdmin
  module I18nTools
    module GoogleTranslate
      def self.t(text, from, to)
        return '' if text.blank?
        return text if from == to
        base = 'https://www.googleapis.com/language/translate/v2'
        params = {
            :key => configatron.else.retrieve(:google_api_key, ENV['GOOGLE_API_KEY']),
            :format => 'html',
            :source => from,
            :target => to,
            :q => text
        }
        response = RestClient.post(base, params, 'X-HTTP-Method-Override' => 'GET')

        if response.code == 200
          json = MultiJson.decode(response)
          json['data']['translations'][0]['translatedText']
        else
          raise StandardError, response.inspect
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ab_admin-0.2.3 lib/ab_admin/i18n_tools/google_translate.rb
ab_admin-0.2.2 lib/ab_admin/i18n_tools/google_translate.rb
ab_admin-0.2.1 lib/ab_admin/i18n_tools/google_translate.rb
ab_admin-0.2.0 lib/ab_admin/i18n_tools/google_translate.rb
ab_admin-0.1.2 lib/ab_admin/i18n_tools/google_translate.rb
ab_admin-0.1.1 lib/ab_admin/i18n_tools/google_translate.rb
ab_admin-0.1.0 lib/ab_admin/i18n_tools/google_translate.rb