diff --git a/Gemfile b/Gemfile index 5d7824394dcb83..d84597a787994b 100644 --- a/Gemfile +++ b/Gemfile @@ -57,7 +57,6 @@ gem 'statsd-instrument' gem 'twitter-text' gem 'tzinfo-data' gem 'webpacker', '~>1.2' -gem 'whatlanguage' # For some reason the view specs start failing without this gem 'react-rails' diff --git a/Gemfile.lock b/Gemfile.lock index e58d89c4af93f5..f4b307cec9874c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -466,7 +466,6 @@ GEM websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.2) - whatlanguage (1.0.6) xpath (2.0.0) nokogiri (~> 1.3) @@ -553,7 +552,6 @@ DEPENDENCIES uglifier (>= 1.3.0) webmock webpacker (~> 1.2) - whatlanguage RUBY VERSION ruby 2.4.1p111 diff --git a/app/lib/language_detector.rb b/app/lib/language_detector.rb index 9a32d6a6423940..8c1751beb8019f 100644 --- a/app/lib/language_detector.rb +++ b/app/lib/language_detector.rb @@ -9,11 +9,23 @@ def initialize(text, account = nil) end def to_iso_s - WhatLanguage.new(:all).language_iso(text_without_urls) || default_locale.to_sym + detected_language_code || default_locale.to_sym end private + def detected_language_code + detected_language[:code].to_sym if detected_language_reliable? + end + + def detected_language + @_detected_language ||= CLD.detect_language(text_without_urls) + end + + def detected_language_reliable? + detected_language[:reliable] + end + def text_without_urls text.dup.tap do |new_text| URI.extract(new_text).each do |url| diff --git a/spec/lib/language_detector_spec.rb b/spec/lib/language_detector_spec.rb index 1b42d0adbb3f91..bd4e65ef8e2e66 100644 --- a/spec/lib/language_detector_spec.rb +++ b/spec/lib/language_detector_spec.rb @@ -25,15 +25,15 @@ describe 'when language can\'t be detected' do it 'confirm language engine cant detect' do - result = WhatLanguage.new(:all).language_iso('') - expect(result).to be_nil + result = CLD.detect_language('') + expect(result[:reliable]).to be false end describe 'because of a URL' do it 'uses default locale when sent just a URL' do string = 'http://example.com/media/2kFTgOJLXhQf0g2nKB4' - wl_result = WhatLanguage.new(:all).language_iso(string) - expect(wl_result).not_to eq :en + cld_result = CLD.detect_language(string)[:code] + expect(cld_result).not_to eq :en result = described_class.new(string).to_iso_s