Leave unknown language as nil if account is remote (#8861)
* Force use language detector if account is remote * Set unknown remote toot's language as nil
This commit is contained in:
		
							parent
							
								
									9a42b75f00
								
							
						
					
					
						commit
						144d73730d
					
				
					 2 changed files with 13 additions and 1 deletions
				
			
		| 
						 | 
					@ -12,6 +12,7 @@ class LanguageDetector
 | 
				
			||||||
  def detect(text, account)
 | 
					  def detect(text, account)
 | 
				
			||||||
    input_text = prepare_text(text)
 | 
					    input_text = prepare_text(text)
 | 
				
			||||||
    return if input_text.blank?
 | 
					    return if input_text.blank?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    detect_language_code(input_text) || default_locale(account)
 | 
					    detect_language_code(input_text) || default_locale(account)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -33,6 +34,7 @@ class LanguageDetector
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def detect_language_code(text)
 | 
					  def detect_language_code(text)
 | 
				
			||||||
    return if unreliable_input?(text)
 | 
					    return if unreliable_input?(text)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    result = @identifier.find_language(text)
 | 
					    result = @identifier.find_language(text)
 | 
				
			||||||
    iso6391(result.language.to_s).to_sym if result.reliable?
 | 
					    iso6391(result.language.to_s).to_sym if result.reliable?
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
| 
						 | 
					@ -75,6 +77,6 @@ class LanguageDetector
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def default_locale(account)
 | 
					  def default_locale(account)
 | 
				
			||||||
    account.user_locale&.to_sym || I18n.default_locale
 | 
					    return account.user_locale&.to_sym || I18n.default_locale if account.local?
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -42,6 +42,7 @@ describe LanguageDetector do
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  describe 'detect' do
 | 
					  describe 'detect' do
 | 
				
			||||||
    let(:account_without_user_locale) { Fabricate(:user, locale: nil).account }
 | 
					    let(:account_without_user_locale) { Fabricate(:user, locale: nil).account }
 | 
				
			||||||
 | 
					    let(:account_remote) { Fabricate(:account, domain: 'joinmastodon.org') }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    it 'detects english language for basic strings' do
 | 
					    it 'detects english language for basic strings' do
 | 
				
			||||||
      strings = [
 | 
					      strings = [
 | 
				
			||||||
| 
						 | 
					@ -104,6 +105,15 @@ describe LanguageDetector do
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      describe 'remote user' do
 | 
				
			||||||
 | 
					        it 'nil for foreign user when language is not present' do
 | 
				
			||||||
 | 
					          string = '안녕하세요'
 | 
				
			||||||
 | 
					          result = described_class.instance.detect(string, account_remote)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          expect(result).to eq nil
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      describe 'with a non-`en` default locale' do
 | 
					      describe 'with a non-`en` default locale' do
 | 
				
			||||||
        around(:each) do |example|
 | 
					        around(:each) do |example|
 | 
				
			||||||
          before = I18n.default_locale
 | 
					          before = I18n.default_locale
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue