The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

42 lines
785 B

  1. # frozen_string_literal: true
  2. class LanguageDetector
  3. attr_reader :text, :account
  4. def initialize(text, account = nil)
  5. @text = text
  6. @account = account
  7. @identifier = CLD3::NNetLanguageIdentifier.new(1, 2048)
  8. end
  9. def to_iso_s
  10. detected_language_code || default_locale.to_sym
  11. end
  12. private
  13. def detected_language_code
  14. result.language.to_sym if detected_language_reliable?
  15. end
  16. def result
  17. @result ||= @identifier.find_language(text_without_urls)
  18. end
  19. def detected_language_reliable?
  20. result.reliable?
  21. end
  22. def text_without_urls
  23. text.dup.tap do |new_text|
  24. URI.extract(new_text).each do |url|
  25. new_text.gsub!(url, '')
  26. end
  27. end
  28. end
  29. def default_locale
  30. account&.user_locale || I18n.default_locale
  31. end
  32. end