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:
parent
57f592fed5
commit
97df239b41
3 changed files with 24 additions and 19 deletions
|
@ -98,7 +98,7 @@ class TextFormatter
|
|||
url = tag_url(hashtag)
|
||||
|
||||
<<~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
|
||||
end
|
||||
|
||||
|
|
|
@ -45,11 +45,11 @@ class TranslateStatusService < BaseService
|
|||
|
||||
def source_texts
|
||||
texts = {}
|
||||
texts[:content] = wrap_emoji_shortcodes(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[:content] = wrap_emoji_and_tags(status_content_format(@status)) if @status.content.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|
|
||||
texts[option] = wrap_emoji_shortcodes(html_escape(option.title))
|
||||
texts[option] = wrap_emoji_and_tags(html_escape(option.title))
|
||||
end
|
||||
|
||||
@status.media_attachments.each do |media_attachment|
|
||||
|
@ -75,14 +75,14 @@ class TranslateStatusService < BaseService
|
|||
|
||||
case source
|
||||
when :content
|
||||
node = unwrap_emoji_shortcodes(translation.text)
|
||||
node = unwrap_emoji_and_tags(translation.text)
|
||||
Sanitize.node!(node, Sanitize::Config::MASTODON_STRICT)
|
||||
status_translation.content = node.to_html
|
||||
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
|
||||
status_translation.poll_options << Translation::Option.new(
|
||||
title: unwrap_emoji_shortcodes(translation.text).content
|
||||
title: unwrap_emoji_and_tags(translation.text).content
|
||||
)
|
||||
when MediaAttachment
|
||||
status_translation.media_attachments << Translation::MediaAttachment.new(
|
||||
|
@ -95,13 +95,18 @@ class TranslateStatusService < BaseService
|
|||
status_translation
|
||||
end
|
||||
|
||||
def wrap_emoji_shortcodes(text)
|
||||
EmojiFormatter.new(text, @status.emojis, { raw_shortcode: true }).to_s
|
||||
def wrap_emoji_and_tags(text)
|
||||
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
|
||||
|
||||
def unwrap_emoji_shortcodes(html)
|
||||
def unwrap_emoji_and_tags(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.replace(element.children) if element.attributes.empty?
|
||||
end
|
||||
|
|
|
@ -203,7 +203,7 @@ RSpec.describe TranslateStatusService, type: :service do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#wrap_emoji_shortcodes' do
|
||||
describe '#wrap_emoji_and_tags' do
|
||||
before do
|
||||
service.instance_variable_set(:@status, status)
|
||||
end
|
||||
|
@ -212,22 +212,22 @@ RSpec.describe TranslateStatusService, type: :service do
|
|||
let(:text) { ':highfive:' }
|
||||
|
||||
it 'renders the emoji' do
|
||||
html = service.send(:wrap_emoji_shortcodes, 'Hello :highfive:'.html_safe)
|
||||
expect(html).to eq 'Hello <span translate="no">:highfive:</span>'
|
||||
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> <a class="hashtag" translate="no">#<span>hashtag</span></a>'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#unwrap_emoji_shortcodes' do
|
||||
describe '#unwrap_emoji_and_tags' do
|
||||
describe 'string contains custom emoji' do
|
||||
it 'inserts the shortcode' do
|
||||
fragment = service.send(:unwrap_emoji_shortcodes, '<p>Hello <span translate="no">:highfive:</span>!</p>')
|
||||
expect(fragment.to_html).to eq '<p>Hello :highfive:!</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:! <a class="hashtag">#<span>hashtag</span></a></p>'
|
||||
end
|
||||
|
||||
it 'preserves other attributes than translate=no' do
|
||||
fragment = service.send(:unwrap_emoji_shortcodes, '<p>Hello <span translate="no" class="foo">:highfive:</span>!</p>')
|
||||
expect(fragment.to_html).to eq '<p>Hello <span 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>! <a class="mention hashtag">#<span>hashtag</span></a></p>'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue