Skip to content

Commit

Permalink
Replace WhatLanguage with CLD
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski committed May 3, 2017
1 parent d736cc0 commit fd301e5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
1 change: 0 additions & 1 deletion Gemfile
Expand Up @@ -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'
Expand Down
2 changes: 0 additions & 2 deletions Gemfile.lock
Expand Up @@ -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)

Expand Down Expand Up @@ -553,7 +552,6 @@ DEPENDENCIES
uglifier (>= 1.3.0)
webmock
webpacker (~> 1.2)
whatlanguage

RUBY VERSION
ruby 2.4.1p111
Expand Down
14 changes: 13 additions & 1 deletion app/lib/language_detector.rb
Expand Up @@ -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|
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/language_detector_spec.rb
Expand Up @@ -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

Expand Down

0 comments on commit fd301e5

Please sign in to comment.