Fix filterable_languages method of SettingsHelper (#4966)

This commit is contained in:
Akihiko Odaki 2017-09-16 21:59:41 +09:00 committed by Eugen Rochko
parent efec507230
commit 48d77ea1eb
7 changed files with 43 additions and 53 deletions

View file

@ -41,7 +41,7 @@ module SettingsHelper
end
def filterable_languages
I18n.available_locales.map { |locale| locale.to_s.split('-').first.to_sym }.uniq
LanguageDetector.instance.language_names.select(&HUMAN_LOCALES.method(:key?))
end
def hash_to_object(hash)

View file

@ -1,26 +1,31 @@
# frozen_string_literal: true
class LanguageDetector
attr_reader :text, :account
include Singleton
def initialize(text, account = nil)
@text = text
@account = account
def initialize
@identifier = CLD3::NNetLanguageIdentifier.new(1, 2048)
end
def to_iso_s
detected_language_code || default_locale
def detect(text, account)
detect_language_code(text) || default_locale(account)
end
def prepared_text
simplified_text.strip
def language_names
@language_names =
CLD3::TaskContextParams::LANGUAGE_NAMES.map { |name| iso6391(name.to_s).to_sym }
.uniq
end
private
def detected_language_code
iso6391(result.language).to_sym if detected_language_reliable?
def prepare_text(text)
simplify_text(text).strip
end
def detect_language_code(text)
result = @identifier.find_language(prepare_text(text))
iso6391(result.language.to_s).to_sym if result.reliable?
end
def iso6391(bcp47)
@ -32,15 +37,7 @@ class LanguageDetector
ISO_639.find(iso639).alpha2
end
def result
@result ||= @identifier.find_language(prepared_text)
end
def detected_language_reliable?
result.reliable?
end
def simplified_text
def simplify_text(text)
text.dup.tap do |new_text|
new_text.gsub!(FetchLinkCardService::URL_PATTERN, '')
new_text.gsub!(Account::MENTION_RE, '')
@ -49,7 +46,7 @@ class LanguageDetector
end
end
def default_locale
account&.user_locale&.to_sym || nil
def default_locale(account)
account.user_locale&.to_sym
end
end

View file

@ -28,7 +28,7 @@ class PostStatusService < BaseService
sensitive: options[:sensitive],
spoiler_text: options[:spoiler_text] || '',
visibility: options[:visibility] || account.user&.setting_default_privacy,
language: detect_language_for(text, account),
language: LanguageDetector.instance.detect(text, account),
application: options[:application])
attach_media(status, media)
@ -69,10 +69,6 @@ class PostStatusService < BaseService
media.update(status_id: status.id)
end
def detect_language_for(text, account)
LanguageDetector.new(text, account).to_iso_s
end
def process_mentions_service
@process_mentions_service ||= ProcessMentionsService.new
end