]> cat aescling's git repositories - mastodon.git/commitdiff
[WIP] Html lang on statuses (#2297)
authorMatt Jankowski <mjankowski@thoughtbot.com>
Sat, 22 Apr 2017 02:26:25 +0000 (22:26 -0400)
committerEugen <eugen@zeonfederated.com>
Sat, 22 Apr 2017 02:26:25 +0000 (04:26 +0200)
* Add html lang attributes around statuses

* Remove urls from language detection

app/lib/language_detector.rb
app/views/stream_entries/_detailed_status.html.haml
app/views/stream_entries/_simple_status.html.haml
spec/lib/language_detector_spec.rb

index b6f81923b5f98fdf884ff1fe26fa9e35909ba592..9a32d6a642394068f6c897aace2708a490997f56 100644 (file)
@@ -9,11 +9,19 @@ class LanguageDetector
   end
 
   def to_iso_s
-    WhatLanguage.new(:all).language_iso(text) || default_locale.to_sym
+    WhatLanguage.new(:all).language_iso(text_without_urls) || default_locale.to_sym
   end
 
   private
 
+  def text_without_urls
+    text.dup.tap do |new_text|
+      URI.extract(new_text).each do |url|
+        new_text.gsub!(url, '')
+      end
+    end
+  end
+
   def default_locale
     account&.user&.locale || I18n.default_locale
   end
index 626b97f41ad2637d983812eb991762aaebd552aa..0b4eeba547ab0dad2e61dd5e25fd0dbff5665f32 100644 (file)
@@ -12,7 +12,7 @@
       %p{ style: 'margin-bottom: 0' }<
         %span.p-summary>= "#{status.spoiler_text} "
         %a.status__content__spoiler-link{ href: '#' }= t('statuses.show_more')
-    %div.e-content{ style: "display: #{status.spoiler_text? ? 'none' : 'block'}; direction: #{rtl?(status.content) ? 'rtl' : 'ltr'}" }= Formatter.instance.format(status)
+    %div.e-content{ lang: status.language, style: "display: #{status.spoiler_text? ? 'none' : 'block'}; direction: #{rtl?(status.content) ? 'rtl' : 'ltr'}" }= Formatter.instance.format(status)
 
 
   - unless status.media_attachments.empty?
index 52518bc3340c36abec12276badd4e12c123e51c2..99acae23dc15c888e0bd4ce61805f3728b331c0e 100644 (file)
@@ -17,7 +17,7 @@
       %p{ style: 'margin-bottom: 0' }<
         %span.p-summary>= "#{status.spoiler_text} "
         %a.status__content__spoiler-link{ href: '#' }= t('statuses.show_more')
-    %div.e-content{ style: "display: #{status.spoiler_text? ? 'none' : 'block'}; direction: #{rtl?(status.content) ? 'rtl' : 'ltr'}" }= Formatter.instance.format(status)
+    %div.e-content{ lang: status.language, style: "display: #{status.spoiler_text? ? 'none' : 'block'}; direction: #{rtl?(status.content) ? 'rtl' : 'ltr'}" }= Formatter.instance.format(status)
 
   - unless status.media_attachments.empty?
     .status__attachments
index 2e68ec63bc04eddfe2f5b6aa270f66f7a24736e2..5fb19a1e7d4a3a960c26e6433ff98c9144eff561 100644 (file)
@@ -23,6 +23,18 @@ describe LanguageDetector do
         expect(result).to be_nil
       end
 
+      describe 'because of a URL' do
+        it 'uses default locale when sent just a URL' do
+          string = 'http://example.com/media/2kFTgOJLXhQf0g2nKB4'
+          wl_result = WhatLanguage.new(:all).language_iso(string)
+          expect(wl_result).not_to eq :en
+
+          result = described_class.new(string).to_iso_s
+
+          expect(result).to eq :en
+        end
+      end
+
       describe 'with an account' do
         it 'uses the account locale when present' do
           user    = double(:user, locale: 'fr')