From 6ab96ba647ae51ab3f09636ee10b1cd8e4e20488 Mon Sep 17 00:00:00 2001 From: Claire Date: Wed, 21 May 2025 18:22:54 +0200 Subject: [PATCH] Use more generic locale as fallback for rules (#34756) --- app/javascript/mastodon/features/about/index.jsx | 4 ++-- app/javascript/mastodon/features/report/rules.jsx | 2 +- app/models/rule.rb | 2 +- spec/models/rule_spec.rb | 11 +++++++++++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/javascript/mastodon/features/about/index.jsx b/app/javascript/mastodon/features/about/index.jsx index f2ea16a952..d2e1ea8d77 100644 --- a/app/javascript/mastodon/features/about/index.jsx +++ b/app/javascript/mastodon/features/about/index.jsx @@ -171,8 +171,8 @@ class About extends PureComponent { ) : (
    {server.get('rules').map(rule => { - const text = rule.getIn(['translations', locale, 'text']) || rule.get('text'); - const hint = rule.getIn(['translations', locale, 'hint']) || rule.get('hint'); + const text = rule.getIn(['translations', locale, 'text']) || rule.getIn(['translations', locale.split('-')[0], 'text']) || rule.get('text'); + const hint = rule.getIn(['translations', locale, 'hint']) || rule.getIn(['translations', locale.split('-')[0], 'hint']) || rule.get('hint'); return (
  1. {text}
    diff --git a/app/javascript/mastodon/features/report/rules.jsx b/app/javascript/mastodon/features/report/rules.jsx index dff3769379..33087e3d62 100644 --- a/app/javascript/mastodon/features/report/rules.jsx +++ b/app/javascript/mastodon/features/report/rules.jsx @@ -51,7 +51,7 @@ class Rules extends PureComponent { value={item.get('id')} checked={selectedRuleIds.includes(item.get('id'))} onToggle={this.handleRulesToggle} - label={item.getIn(['translations', locale, 'text']) || item.get('text')} + label={item.getIn(['translations', locale, 'text']) || item.getIn(['translations', locale.split('-')[0], 'text']) || item.get('text')} multiple /> ))} diff --git a/app/models/rule.rb b/app/models/rule.rb index 8f36f11abb..c7b532fe5d 100644 --- a/app/models/rule.rb +++ b/app/models/rule.rb @@ -42,6 +42,6 @@ class Rule < ApplicationRecord def translation_for(locale) @cached_translations ||= {} - @cached_translations[locale] ||= translations.find_by(language: locale) || RuleTranslation.new(language: locale, text: text, hint: hint) + @cached_translations[locale] ||= translations.where(language: [locale, locale.to_s.split('-').first]).order('length(language) desc').first || RuleTranslation.new(language: locale, text: text, hint: hint) end end diff --git a/spec/models/rule_spec.rb b/spec/models/rule_spec.rb index a7fc5ef693..e6a2d807cb 100644 --- a/spec/models/rule_spec.rb +++ b/spec/models/rule_spec.rb @@ -36,4 +36,15 @@ RSpec.describe Rule do .to change { described_class.ordered.pluck(:text) }.from(%w(foo baz bar)).to(%w(foo bar baz)) end end + + describe '#translation_for' do + let!(:rule) { Fabricate(:rule, text: 'This is a rule', hint: 'This is an explanation of the rule') } + let!(:translation) { Fabricate(:rule_translation, rule: rule, text: 'Ceci est une règle', hint: 'Ceci est une explication de la règle', language: 'fr') } + + it 'returns the expected translation, including fallbacks' do + expect(rule.translation_for(:en)).to have_attributes(text: rule.text, hint: rule.hint) + expect(rule.translation_for(:fr)).to have_attributes(text: translation.text, hint: translation.hint) + expect(rule.translation_for(:'fr-CA')).to have_attributes(text: translation.text, hint: translation.hint) + end + end end