about summary refs log tree commit diff
path: root/app/lib
diff options
context:
space:
mode:
authorMatt Jankowski <mjankowski@thoughtbot.com>2017-04-18 16:20:12 -0400
committerEugen <eugen@zeonfederated.com>2017-04-18 22:20:12 +0200
commit297c11dba2864b20992cd3f98282f5ce35d5d144 (patch)
treefd4c3e3ec0c981b417931708c7f88cd25e6282c9 /app/lib
parent0a7588282a00513af9631d06eea76878a974c659 (diff)
Language detection refactor (#2099)
* Extract detect_language to separate class

* Use default locale, not just en

* Add spec to confirm that whatlanguage cant identify empty string

* Allow account locale to override default in language detector

* PostStatusService supplies an account to detect language
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/language_detector.rb20
1 files changed, 20 insertions, 0 deletions
diff --git a/app/lib/language_detector.rb b/app/lib/language_detector.rb
new file mode 100644
index 000000000..b6f81923b
--- /dev/null
+++ b/app/lib/language_detector.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+class LanguageDetector
+  attr_reader :text, :account
+
+  def initialize(text, account = nil)
+    @text = text
+    @account = account
+  end
+
+  def to_iso_s
+    WhatLanguage.new(:all).language_iso(text) || default_locale.to_sym
+  end
+
+  private
+
+  def default_locale
+    account&.user&.locale || I18n.default_locale
+  end
+end