Sha256: 1f529ed9b2c0fd596e413e78f66b450cdfc943b02c0d598a022a285976898331

Contents?: true

Size: 804 Bytes

Versions: 1

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: ENV['GOOGLE_API_KEY'] || Settings.data.else.try!(: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

1 entries across 1 versions & 1 rubygems

Version Path
ab_admin-0.8.3 lib/ab_admin/i18n_tools/google_translate.rb