Sha256: c2eb415530473b5e09171c7c6440e30151dceed5ebbe48afbbe11490ca81265d

Contents?: true

Size: 804 Bytes

Versions: 4

Compression:

Stored size: 804 Bytes

Contents

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

4 entries across 4 versions & 1 rubygems

Version Path
ab_admin-0.8.2 lib/ab_admin/i18n_tools/google_translate.rb
ab_admin-0.8.1 lib/ab_admin/i18n_tools/google_translate.rb
ab_admin-0.8.0 lib/ab_admin/i18n_tools/google_translate.rb
ab_admin-0.7.0 lib/ab_admin/i18n_tools/google_translate.rb