1
0
Fork 0
mirror of https://github.com/mastodon/mastodon.git synced 2024-08-20 21:08:15 -07:00

Do not translate hashtags

This commit is contained in:
Robert George 2023-09-28 14:09:50 -07:00
parent 57f592fed5
commit 97df239b41
No known key found for this signature in database
GPG key ID: 9B0D835A293663C4
3 changed files with 24 additions and 19 deletions

View file

@ -98,7 +98,7 @@ class TextFormatter
url = tag_url(hashtag) url = tag_url(hashtag)
<<~HTML.squish <<~HTML.squish
<a href="#{h(url)}" class="mention hashtag" rel="tag">#<span>#{h(hashtag)}</span></a> <a href="#{h(url)}" class="mention hashtag" rel="tag" translate="no">#<span>#{h(hashtag)}</span></a>
HTML HTML
end end

View file

@ -45,11 +45,11 @@ class TranslateStatusService < BaseService
def source_texts def source_texts
texts = {} texts = {}
texts[:content] = wrap_emoji_shortcodes(status_content_format(@status)) if @status.content.present? texts[:content] = wrap_emoji_and_tags(status_content_format(@status)) if @status.content.present?
texts[:spoiler_text] = wrap_emoji_shortcodes(html_escape(@status.spoiler_text)) if @status.spoiler_text.present? texts[:spoiler_text] = wrap_emoji_and_tags(html_escape(@status.spoiler_text)) if @status.spoiler_text.present?
@status.preloadable_poll&.loaded_options&.each do |option| @status.preloadable_poll&.loaded_options&.each do |option|
texts[option] = wrap_emoji_shortcodes(html_escape(option.title)) texts[option] = wrap_emoji_and_tags(html_escape(option.title))
end end
@status.media_attachments.each do |media_attachment| @status.media_attachments.each do |media_attachment|
@ -75,14 +75,14 @@ class TranslateStatusService < BaseService
case source case source
when :content when :content
node = unwrap_emoji_shortcodes(translation.text) node = unwrap_emoji_and_tags(translation.text)
Sanitize.node!(node, Sanitize::Config::MASTODON_STRICT) Sanitize.node!(node, Sanitize::Config::MASTODON_STRICT)
status_translation.content = node.to_html status_translation.content = node.to_html
when :spoiler_text when :spoiler_text
status_translation.spoiler_text = unwrap_emoji_shortcodes(translation.text).content status_translation.spoiler_text = unwrap_emoji_and_tags(translation.text).content
when Poll::Option when Poll::Option
status_translation.poll_options << Translation::Option.new( status_translation.poll_options << Translation::Option.new(
title: unwrap_emoji_shortcodes(translation.text).content title: unwrap_emoji_and_tags(translation.text).content
) )
when MediaAttachment when MediaAttachment
status_translation.media_attachments << Translation::MediaAttachment.new( status_translation.media_attachments << Translation::MediaAttachment.new(
@ -95,13 +95,18 @@ class TranslateStatusService < BaseService
status_translation status_translation
end end
def wrap_emoji_shortcodes(text) def wrap_emoji_and_tags(text)
EmojiFormatter.new(text, @status.emojis, { raw_shortcode: true }).to_s html = EmojiFormatter.new(text, @status.emojis, { raw_shortcode: true }).to_s
fragment = Nokogiri::HTML.fragment(html)
fragment.css('.hashtag').each do |element|
element['translate'] = 'no'
end
fragment.to_s
end end
def unwrap_emoji_shortcodes(html) def unwrap_emoji_and_tags(html)
fragment = Nokogiri::HTML.fragment(html) fragment = Nokogiri::HTML.fragment(html)
fragment.css('span[translate="no"]').each do |element| fragment.css('span[translate="no"],.hashtag[translate="no"]').each do |element|
element.remove_attribute('translate') element.remove_attribute('translate')
element.replace(element.children) if element.attributes.empty? element.replace(element.children) if element.attributes.empty?
end end

View file

@ -203,7 +203,7 @@ RSpec.describe TranslateStatusService, type: :service do
end end
end end
describe '#wrap_emoji_shortcodes' do describe '#wrap_emoji_and_tags' do
before do before do
service.instance_variable_set(:@status, status) service.instance_variable_set(:@status, status)
end end
@ -212,22 +212,22 @@ RSpec.describe TranslateStatusService, type: :service do
let(:text) { ':highfive:' } let(:text) { ':highfive:' }
it 'renders the emoji' do it 'renders the emoji' do
html = service.send(:wrap_emoji_shortcodes, 'Hello :highfive:'.html_safe) html = service.send(:wrap_emoji_and_tags, 'Hello :highfive: <a class="hashtag">#<span>hashtag</span></a>'.html_safe)
expect(html).to eq 'Hello <span translate="no">:highfive:</span>' expect(html).to eq 'Hello <span translate="no">:highfive:</span> <a class="hashtag" translate="no">#<span>hashtag</span></a>'
end end
end end
end end
describe '#unwrap_emoji_shortcodes' do describe '#unwrap_emoji_and_tags' do
describe 'string contains custom emoji' do describe 'string contains custom emoji' do
it 'inserts the shortcode' do it 'inserts the shortcode' do
fragment = service.send(:unwrap_emoji_shortcodes, '<p>Hello <span translate="no">:highfive:</span>!</p>') fragment = service.send(:unwrap_emoji_and_tags, '<p>Hello <span translate="no">:highfive:</span>! <a class="hashtag" translate="no">#<span>hashtag</span></a></p>')
expect(fragment.to_html).to eq '<p>Hello :highfive:!</p>' expect(fragment.to_html).to eq '<p>Hello :highfive:! <a class="hashtag">#<span>hashtag</span></a></p>'
end end
it 'preserves other attributes than translate=no' do it 'preserves other attributes than translate=no' do
fragment = service.send(:unwrap_emoji_shortcodes, '<p>Hello <span translate="no" class="foo">:highfive:</span>!</p>') fragment = service.send(:unwrap_emoji_and_tags, '<p>Hello <span translate="no" class="foo">:highfive:</span>! <a class="mention hashtag" translate="no">#<span>hashtag</span></a></p>')
expect(fragment.to_html).to eq '<p>Hello <span class="foo">:highfive:</span>!</p>' expect(fragment.to_html).to eq '<p>Hello <span class="foo">:highfive:</span>! <a class="mention hashtag">#<span>hashtag</span></a></p>'
end end
end end
end end