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.

28 lines
521 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. end
  8. def to_iso_s
  9. WhatLanguage.new(:all).language_iso(text_without_urls) || default_locale.to_sym
  10. end
  11. private
  12. def text_without_urls
  13. text.dup.tap do |new_text|
  14. URI.extract(new_text).each do |url|
  15. new_text.gsub!(url, '')
  16. end
  17. end
  18. end
  19. def default_locale
  20. account&.user&.locale || I18n.default_locale
  21. end
  22. end