Extremely easy way to detect language of text using google translate:

require "rubygems" require "net/http" require "open-uri" require "cgi" require "json" def detect_language(content) base_url = 'http://www.google.com/uds/GlangDetect?v=1.0&q=' url = base_url + CGI.escape(content) response = Net::HTTP.get_response(URI.parse(url)) result = JSON.parse(response.body) return result['responseData']['language'] end if detect_language("To powinnien być polski!") == "pl" puts "Witam" elsif detect_language("This should be english!") == "en" puts "Hello!" end

Comments