Use translated rules for report post modal

pull/1214/head
Lim Chee Aun 2025-07-09 18:34:28 +08:00
rodzic 9e16633eb8
commit 2ce0423991
2 zmienionych plików z 80 dodań i 37 usunięć

Wyświetl plik

@ -6,6 +6,7 @@ import { Fragment } from 'preact';
import { useMemo, useRef, useState } from 'preact/hooks'; import { useMemo, useRef, useState } from 'preact/hooks';
import { api } from '../utils/api'; import { api } from '../utils/api';
import localeMatch from '../utils/locale-match';
import showToast from '../utils/show-toast'; import showToast from '../utils/show-toast';
import { getCurrentInstance } from '../utils/store-utils'; import { getCurrentInstance } from '../utils/store-utils';
@ -17,7 +18,7 @@ import Status from './status';
// NOTE: `dislike` hidden for now, it's actually not used for reporting // NOTE: `dislike` hidden for now, it's actually not used for reporting
// Mastodon shows another screen for unfollowing, muting or blocking instead of reporting // Mastodon shows another screen for unfollowing, muting or blocking instead of reporting
const CATEGORIES = [/*'dislike'*/ , 'spam', 'legal', 'violation', 'other']; const CATEGORIES = [/*'dislike' ,*/ 'spam', 'legal', 'violation', 'other'];
// `violation` will be set if there are `rule_ids[]` // `violation` will be set if there are `rule_ids[]`
const CATEGORIES_INFO = { const CATEGORIES_INFO = {
@ -45,16 +46,58 @@ const CATEGORIES_INFO = {
}, },
}; };
function findMatchingLanguage(rule, currentLang) {
if (!rule.translations || !currentLang) return null;
const availableLanguages = Object.keys(rule.translations);
if (!availableLanguages?.length) return null;
let matchedLang = localeMatch([currentLang], availableLanguages, null);
if (!matchedLang) {
// localeMatch fails if there are keys like zhCn, zhTw
// Convert them something like zh-CN first, try again
// Detect uppercase, then split by dash
const normalizedLanguages = availableLanguages.map((lang) => {
const parts = lang.split(/(?=[A-Z])/);
return parts
.map((part, i) => (i === 0 ? part : part.toLowerCase()))
.join('-');
});
matchedLang = localeMatch([currentLang], normalizedLanguages, null);
}
// If matchedLang has dash, convert back to original format
// E.g. zh-cn to zhCn
if (matchedLang && matchedLang.includes('-')) {
const [lang, region] = matchedLang.split('-');
matchedLang = lang + region.charAt(0).toUpperCase() + region.slice(1);
}
return matchedLang;
}
function translateRules(rules, currentLang) {
if (!rules?.length) return [];
if (!currentLang) return rules;
return rules.map((rule) => {
const matchedLang = findMatchingLanguage(rule, currentLang);
return {
...rule,
_translatedText: rule.translations?.[matchedLang]?.text || null,
};
});
}
function ReportModal({ account, post, onClose }) { function ReportModal({ account, post, onClose }) {
const { _, t } = useLingui(); const { _, t, i18n } = useLingui();
const { masto } = api(); const { masto } = api();
const [uiState, setUIState] = useState('default'); const [uiState, setUIState] = useState('default');
const [username, domain] = account.acct.split('@'); const [username, domain] = account.acct.split('@');
const [rules, currentDomain] = useMemo(() => { const [translatedRules, currentDomain] = useMemo(() => {
const { rules, domain } = getCurrentInstance(); const { rules, domain } = getCurrentInstance();
return [rules || [], domain]; const rawRules = rules || [];
}); return [translateRules(rawRules, i18n.locale), domain];
}, [i18n.locale]);
const [selectedCategory, setSelectedCategory] = useState(null); const [selectedCategory, setSelectedCategory] = useState(null);
const [showRules, setShowRules] = useState(false); const [showRules, setShowRules] = useState(false);
@ -165,7 +208,7 @@ function ReportModal({ account, post, onClose }) {
</p> </p>
<section class="report-categories"> <section class="report-categories">
{CATEGORIES.map((category) => {CATEGORIES.map((category) =>
category === 'violation' && !rules?.length ? null : ( category === 'violation' && !translatedRules?.length ? null : (
<Fragment key={category}> <Fragment key={category}>
<label class="report-category"> <label class="report-category">
<input <input
@ -186,14 +229,14 @@ function ReportModal({ account, post, onClose }) {
</small> </small>
</span> </span>
</label> </label>
{category === 'violation' && !!rules?.length && ( {category === 'violation' && !!translatedRules?.length && (
<div <div
class="shazam-container no-animation" class="shazam-container no-animation"
hidden={!showRules} hidden={!showRules}
> >
<div class="shazam-container-inner"> <div class="shazam-container-inner">
<div class="report-rules" ref={rulesRef}> <div class="report-rules" ref={rulesRef}>
{rules.map((rule, i) => ( {translatedRules.map((rule, i) => (
<label class="report-rule" key={rule.id}> <label class="report-rule" key={rule.id}>
<input <input
type="checkbox" type="checkbox"
@ -216,7 +259,7 @@ function ReportModal({ account, post, onClose }) {
} }
}} }}
/> />
<span>{rule.text}</span> <span>{rule._translatedText || rule.text}</span>
</label> </label>
))} ))}
</div> </div>

56
src/locales/en.po wygenerowano
Wyświetl plik

@ -462,7 +462,7 @@ msgstr ""
#: src/components/media-alt-modal.jsx:43 #: src/components/media-alt-modal.jsx:43
#: src/components/media-modal.jsx:327 #: src/components/media-modal.jsx:327
#: src/components/notification-service.jsx:157 #: src/components/notification-service.jsx:157
#: src/components/report-modal.jsx:75 #: src/components/report-modal.jsx:118
#: src/components/shortcuts-settings.jsx:230 #: src/components/shortcuts-settings.jsx:230
#: src/components/shortcuts-settings.jsx:583 #: src/components/shortcuts-settings.jsx:583
#: src/components/shortcuts-settings.jsx:783 #: src/components/shortcuts-settings.jsx:783
@ -1770,111 +1770,111 @@ msgstr ""
msgid "{0}h" msgid "{0}h"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:29 #: src/components/report-modal.jsx:30
msgid "Spam" msgid "Spam"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:30 #: src/components/report-modal.jsx:31
msgid "Malicious links, fake engagement, or repetitive replies" msgid "Malicious links, fake engagement, or repetitive replies"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:33 #: src/components/report-modal.jsx:34
msgid "Illegal" msgid "Illegal"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:34 #: src/components/report-modal.jsx:35
msgid "Violates the law of your or the server's country" msgid "Violates the law of your or the server's country"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:37 #: src/components/report-modal.jsx:38
msgid "Server rule violation" msgid "Server rule violation"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:38 #: src/components/report-modal.jsx:39
msgid "Breaks specific server rules" msgid "Breaks specific server rules"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:39 #: src/components/report-modal.jsx:40
msgid "Violation" msgid "Violation"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:42 #: src/components/report-modal.jsx:43
msgid "Other" msgid "Other"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:43 #: src/components/report-modal.jsx:44
msgid "Issue doesn't fit other categories" msgid "Issue doesn't fit other categories"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:68 #: src/components/report-modal.jsx:111
msgid "Report Post" msgid "Report Post"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:68 #: src/components/report-modal.jsx:111
msgid "Report @{username}" msgid "Report @{username}"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:104 #: src/components/report-modal.jsx:147
msgid "Pending review" msgid "Pending review"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:146 #: src/components/report-modal.jsx:189
msgid "Post reported" msgid "Post reported"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:146 #: src/components/report-modal.jsx:189
msgid "Profile reported" msgid "Profile reported"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:154 #: src/components/report-modal.jsx:197
msgid "Unable to report post" msgid "Unable to report post"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:155 #: src/components/report-modal.jsx:198
msgid "Unable to report profile" msgid "Unable to report profile"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:163 #: src/components/report-modal.jsx:206
msgid "What's the issue with this post?" msgid "What's the issue with this post?"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:164 #: src/components/report-modal.jsx:207
msgid "What's the issue with this profile?" msgid "What's the issue with this profile?"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:233 #: src/components/report-modal.jsx:276
msgid "Additional info" msgid "Additional info"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:256 #: src/components/report-modal.jsx:299
msgid "Forward to <0>{domain}</0>" msgid "Forward to <0>{domain}</0>"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:266 #: src/components/report-modal.jsx:309
msgid "Send Report" msgid "Send Report"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:275 #: src/components/report-modal.jsx:318
msgid "Muted {username}" msgid "Muted {username}"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:278 #: src/components/report-modal.jsx:321
msgid "Unable to mute {username}" msgid "Unable to mute {username}"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:283 #: src/components/report-modal.jsx:326
msgid "Send Report <0>+ Mute profile</0>" msgid "Send Report <0>+ Mute profile</0>"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:294 #: src/components/report-modal.jsx:337
msgid "Blocked {username}" msgid "Blocked {username}"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:297 #: src/components/report-modal.jsx:340
msgid "Unable to block {username}" msgid "Unable to block {username}"
msgstr "" msgstr ""
#: src/components/report-modal.jsx:302 #: src/components/report-modal.jsx:345
msgid "Send Report <0>+ Block profile</0>" msgid "Send Report <0>+ Block profile</0>"
msgstr "" msgstr ""