A little fix + refactoring with translation stuff

This commit is contained in:
Lim Chee Aun 2024-10-17 21:13:40 +08:00
parent a3474094bd
commit 350aafaadd
2 changed files with 175 additions and 158 deletions

View file

@ -125,6 +125,46 @@ function getPostText(status) {
);
}
function isTranslateble(content) {
if (!content) return false;
content = content.trim();
if (!content) return false;
const text = getHTMLText(content, {
preProcess: (dom) => {
// Remove .mention, pre, code, a:has(.invisible)
for (const a of dom.querySelectorAll(
'.mention, pre, code, a:has(.invisible)',
)) {
a.remove();
}
},
});
return !!text;
}
function getHTMLTextForDetectLang(content) {
return getHTMLText(content, {
preProcess: (dom) => {
// Remove anything that can skew the language detection
// Remove .mention, .hashtag, pre, code, a:has(.invisible)
for (const a of dom.querySelectorAll(
'.mention, .hashtag, pre, code, a:has(.invisible)',
)) {
a.remove();
}
// Remove links that contains text that starts with https?://
for (const a of dom.querySelectorAll('a')) {
const text = a.innerText.trim();
if (text.startsWith('https://') || text.startsWith('http://')) {
a.remove();
}
}
},
});
}
const HTTP_REGEX = /^http/i;
const PostContent =
/*memo(*/
@ -343,32 +383,10 @@ function Status({
useEffect(() => {
if (!content) return;
if (_language) return;
if (languageAutoDetected) return;
let timer;
timer = setTimeout(async () => {
let detected = await detectLang(
getHTMLText(content, {
preProcess: (dom) => {
// Remove anything that can skew the language detection
// Remove .mention, .hashtag, pre, code, a:has(.invisible)
dom
.querySelectorAll(
'.mention, .hashtag, pre, code, a:has(.invisible)',
)
.forEach((a) => {
a.remove();
});
// Remove links that contains text that starts with https?://
dom.querySelectorAll('a').forEach((a) => {
const text = a.innerText.trim();
if (text.startsWith('https://') || text.startsWith('http://')) {
a.remove();
}
});
},
}),
);
let detected = await detectLang(getHTMLTextForDetectLang(content));
setLanguageAutoDetected(detected);
}, 1000);
return () => clearTimeout(timer);
@ -2101,8 +2119,7 @@ function Status({
/>
)}
{(((enableTranslate || inlineTranslate) &&
!!content.trim() &&
!!getHTMLText(emojifyText(content, emojis)) &&
isTranslateble(content) &&
differentLanguage) ||
forceTranslate) && (
<TranslationBlock

264
src/locales/en.po generated
View file

@ -32,7 +32,7 @@ msgstr ""
#: src/components/account-block.jsx:168
#: src/components/account-info.jsx:639
#: src/components/status.jsx:480
#: src/components/status.jsx:498
#: src/pages/catchup.jsx:1471
msgid "Group"
msgstr ""
@ -108,11 +108,11 @@ msgstr ""
#: src/components/compose.jsx:2488
#: src/components/media-alt-modal.jsx:45
#: src/components/media-modal.jsx:283
#: src/components/status.jsx:1703
#: src/components/status.jsx:1720
#: src/components/status.jsx:1844
#: src/components/status.jsx:2442
#: src/components/status.jsx:2445
#: src/components/status.jsx:1721
#: src/components/status.jsx:1738
#: src/components/status.jsx:1862
#: src/components/status.jsx:2459
#: src/components/status.jsx:2462
#: src/pages/account-statuses.jsx:528
#: src/pages/accounts.jsx:109
#: src/pages/hashtag.jsx:199
@ -181,7 +181,7 @@ msgid "Original"
msgstr ""
#: src/components/account-info.jsx:862
#: src/components/status.jsx:2233
#: src/components/status.jsx:2250
#: src/pages/catchup.jsx:71
#: src/pages/catchup.jsx:1445
#: src/pages/catchup.jsx:2056
@ -277,30 +277,30 @@ msgid "Add/Remove from Lists"
msgstr ""
#: src/components/account-info.jsx:1302
#: src/components/status.jsx:1143
#: src/components/status.jsx:1161
msgid "Link copied"
msgstr ""
#: src/components/account-info.jsx:1305
#: src/components/status.jsx:1146
#: src/components/status.jsx:1164
msgid "Unable to copy link"
msgstr ""
#: src/components/account-info.jsx:1311
#: src/components/shortcuts-settings.jsx:1059
#: src/components/status.jsx:1152
#: src/components/status.jsx:3219
#: src/components/status.jsx:1170
#: src/components/status.jsx:3236
msgid "Copy"
msgstr ""
#: src/components/account-info.jsx:1326
#: src/components/shortcuts-settings.jsx:1077
#: src/components/status.jsx:1168
#: src/components/status.jsx:1186
msgid "Sharing doesn't seem to work."
msgstr ""
#: src/components/account-info.jsx:1332
#: src/components/status.jsx:1174
#: src/components/status.jsx:1192
msgid "Share…"
msgstr ""
@ -417,9 +417,9 @@ msgstr ""
#: src/components/shortcuts-settings.jsx:230
#: src/components/shortcuts-settings.jsx:583
#: src/components/shortcuts-settings.jsx:783
#: src/components/status.jsx:2944
#: src/components/status.jsx:3183
#: src/components/status.jsx:3681
#: src/components/status.jsx:2961
#: src/components/status.jsx:3200
#: src/components/status.jsx:3698
#: src/pages/accounts.jsx:36
#: src/pages/catchup.jsx:1581
#: src/pages/filters.jsx:224
@ -609,7 +609,7 @@ msgid "Attachment #{i} failed"
msgstr ""
#: src/components/compose.jsx:1139
#: src/components/status.jsx:2029
#: src/components/status.jsx:2047
#: src/components/timeline.jsx:984
msgid "Content warning"
msgstr ""
@ -645,7 +645,7 @@ msgstr ""
#: src/components/compose.jsx:1206
#: src/components/status.jsx:97
#: src/components/status.jsx:1907
#: src/components/status.jsx:1925
msgid "Private mention"
msgstr ""
@ -675,10 +675,10 @@ msgstr ""
#: src/components/compose.jsx:1504
#: src/components/keyboard-shortcuts-help.jsx:143
#: src/components/status.jsx:895
#: src/components/status.jsx:1683
#: src/components/status.jsx:1684
#: src/components/status.jsx:2338
#: src/components/status.jsx:913
#: src/components/status.jsx:1701
#: src/components/status.jsx:1702
#: src/components/status.jsx:2355
msgid "Reply"
msgstr ""
@ -893,7 +893,7 @@ msgstr ""
#: src/components/drafts.jsx:127
#: src/components/list-add-edit.jsx:183
#: src/components/status.jsx:1318
#: src/components/status.jsx:1336
#: src/pages/filters.jsx:587
msgid "Delete…"
msgstr ""
@ -1093,10 +1093,10 @@ msgid "<0>l</0> or <1>f</1>"
msgstr ""
#: src/components/keyboard-shortcuts-help.jsx:164
#: src/components/status.jsx:903
#: src/components/status.jsx:2364
#: src/components/status.jsx:2396
#: src/components/status.jsx:2397
#: src/components/status.jsx:921
#: src/components/status.jsx:2381
#: src/components/status.jsx:2413
#: src/components/status.jsx:2414
msgid "Boost"
msgstr ""
@ -1105,9 +1105,9 @@ msgid "<0>Shift</0> + <1>b</1>"
msgstr ""
#: src/components/keyboard-shortcuts-help.jsx:172
#: src/components/status.jsx:988
#: src/components/status.jsx:2421
#: src/components/status.jsx:2422
#: src/components/status.jsx:1006
#: src/components/status.jsx:2438
#: src/components/status.jsx:2439
msgid "Bookmark"
msgstr ""
@ -1166,15 +1166,15 @@ msgid "Media description"
msgstr ""
#: src/components/media-alt-modal.jsx:57
#: src/components/status.jsx:1032
#: src/components/status.jsx:1059
#: src/components/status.jsx:1050
#: src/components/status.jsx:1077
#: src/components/translation-block.jsx:195
msgid "Translate"
msgstr ""
#: src/components/media-alt-modal.jsx:68
#: src/components/status.jsx:1046
#: src/components/status.jsx:1073
#: src/components/status.jsx:1064
#: src/components/status.jsx:1091
msgid "Speak"
msgstr ""
@ -1211,9 +1211,9 @@ msgid "Filtered: {filterTitleStr}"
msgstr ""
#: src/components/media-post.jsx:133
#: src/components/status.jsx:3511
#: src/components/status.jsx:3607
#: src/components/status.jsx:3685
#: src/components/status.jsx:3528
#: src/components/status.jsx:3624
#: src/components/status.jsx:3702
#: src/components/timeline.jsx:973
#: src/pages/catchup.jsx:75
#: src/pages/catchup.jsx:1876
@ -1501,8 +1501,8 @@ msgid "[Unknown notification type: {type}]"
msgstr ""
#: src/components/notification.jsx:434
#: src/components/status.jsx:1002
#: src/components/status.jsx:1012
#: src/components/status.jsx:1020
#: src/components/status.jsx:1030
msgid "Boosted/Liked by…"
msgstr ""
@ -1524,7 +1524,7 @@ msgid "Learn more <0/>"
msgstr ""
#: src/components/notification.jsx:756
#: src/components/status.jsx:211
#: src/components/status.jsx:251
msgid "Read more →"
msgstr ""
@ -1817,7 +1817,7 @@ msgid "Move down"
msgstr ""
#: src/components/shortcuts-settings.jsx:379
#: src/components/status.jsx:1280
#: src/components/status.jsx:1298
#: src/pages/list.jsx:170
msgid "Edit"
msgstr ""
@ -2015,297 +2015,297 @@ msgstr ""
msgid "Import/export settings from/to instance server (Very experimental)"
msgstr ""
#: src/components/status.jsx:504
#: src/components/status.jsx:522
msgid "<0/> <1>boosted</1>"
msgstr ""
#: src/components/status.jsx:603
#: src/components/status.jsx:621
msgid "Sorry, your current logged-in instance can't interact with this post from another instance."
msgstr ""
#: src/components/status.jsx:756
#: src/components/status.jsx:774
msgid "Unliked @{0}'s post"
msgstr ""
#: src/components/status.jsx:757
#: src/components/status.jsx:775
msgid "Liked @{0}'s post"
msgstr ""
#: src/components/status.jsx:796
#: src/components/status.jsx:814
msgid "Unbookmarked @{0}'s post"
msgstr ""
#: src/components/status.jsx:797
#: src/components/status.jsx:815
msgid "Bookmarked @{0}'s post"
msgstr ""
#: src/components/status.jsx:903
#: src/components/status.jsx:965
#: src/components/status.jsx:2364
#: src/components/status.jsx:2396
#: src/components/status.jsx:921
#: src/components/status.jsx:983
#: src/components/status.jsx:2381
#: src/components/status.jsx:2413
msgid "Unboost"
msgstr ""
#: src/components/status.jsx:919
#: src/components/status.jsx:2379
#: src/components/status.jsx:937
#: src/components/status.jsx:2396
msgid "Quote"
msgstr ""
#: src/components/status.jsx:927
#: src/components/status.jsx:2388
#: src/components/status.jsx:945
#: src/components/status.jsx:2405
msgid "Some media have no descriptions."
msgstr ""
#: src/components/status.jsx:934
#: src/components/status.jsx:952
msgid "Old post (<0>{0}</0>)"
msgstr ""
#: src/components/status.jsx:953
#: src/components/status.jsx:1408
#: src/components/status.jsx:971
#: src/components/status.jsx:1426
msgid "Unboosted @{0}'s post"
msgstr ""
#: src/components/status.jsx:954
#: src/components/status.jsx:1409
#: src/components/status.jsx:972
#: src/components/status.jsx:1427
msgid "Boosted @{0}'s post"
msgstr ""
#: src/components/status.jsx:966
#: src/components/status.jsx:984
msgid "Boost…"
msgstr ""
#: src/components/status.jsx:978
#: src/components/status.jsx:1693
#: src/components/status.jsx:2409
#: src/components/status.jsx:996
#: src/components/status.jsx:1711
#: src/components/status.jsx:2426
msgid "Unlike"
msgstr ""
#: src/components/status.jsx:979
#: src/components/status.jsx:1693
#: src/components/status.jsx:1694
#: src/components/status.jsx:2409
#: src/components/status.jsx:2410
#: src/components/status.jsx:997
#: src/components/status.jsx:1711
#: src/components/status.jsx:1712
#: src/components/status.jsx:2426
#: src/components/status.jsx:2427
msgid "Like"
msgstr ""
#: src/components/status.jsx:988
#: src/components/status.jsx:2421
#: src/components/status.jsx:1006
#: src/components/status.jsx:2438
msgid "Unbookmark"
msgstr ""
#: src/components/status.jsx:1096
#: src/components/status.jsx:1114
msgid "View post by <0>@{0}</0>"
msgstr ""
#: src/components/status.jsx:1117
#: src/components/status.jsx:1135
msgid "Show Edit History"
msgstr ""
#: src/components/status.jsx:1120
#: src/components/status.jsx:1138
msgid "Edited: {editedDateText}"
msgstr ""
#: src/components/status.jsx:1187
#: src/components/status.jsx:3188
#: src/components/status.jsx:1205
#: src/components/status.jsx:3205
msgid "Embed post"
msgstr ""
#: src/components/status.jsx:1201
#: src/components/status.jsx:1219
msgid "Conversation unmuted"
msgstr ""
#: src/components/status.jsx:1201
#: src/components/status.jsx:1219
msgid "Conversation muted"
msgstr ""
#: src/components/status.jsx:1207
#: src/components/status.jsx:1225
msgid "Unable to unmute conversation"
msgstr ""
#: src/components/status.jsx:1208
#: src/components/status.jsx:1226
msgid "Unable to mute conversation"
msgstr ""
#: src/components/status.jsx:1217
#: src/components/status.jsx:1235
msgid "Unmute conversation"
msgstr ""
#: src/components/status.jsx:1224
#: src/components/status.jsx:1242
msgid "Mute conversation"
msgstr ""
#: src/components/status.jsx:1240
#: src/components/status.jsx:1258
msgid "Post unpinned from profile"
msgstr ""
#: src/components/status.jsx:1241
#: src/components/status.jsx:1259
msgid "Post pinned to profile"
msgstr ""
#: src/components/status.jsx:1246
#: src/components/status.jsx:1264
msgid "Unable to unpin post"
msgstr ""
#: src/components/status.jsx:1246
#: src/components/status.jsx:1264
msgid "Unable to pin post"
msgstr ""
#: src/components/status.jsx:1255
#: src/components/status.jsx:1273
msgid "Unpin from profile"
msgstr ""
#: src/components/status.jsx:1262
#: src/components/status.jsx:1280
msgid "Pin to profile"
msgstr ""
#: src/components/status.jsx:1291
#: src/components/status.jsx:1309
msgid "Delete this post?"
msgstr ""
#: src/components/status.jsx:1307
#: src/components/status.jsx:1325
msgid "Post deleted"
msgstr ""
#: src/components/status.jsx:1310
#: src/components/status.jsx:1328
msgid "Unable to delete post"
msgstr ""
#: src/components/status.jsx:1338
#: src/components/status.jsx:1356
msgid "Report post…"
msgstr ""
#: src/components/status.jsx:1694
#: src/components/status.jsx:1730
#: src/components/status.jsx:2410
#: src/components/status.jsx:1712
#: src/components/status.jsx:1748
#: src/components/status.jsx:2427
msgid "Liked"
msgstr ""
#: src/components/status.jsx:1727
#: src/components/status.jsx:2397
#: src/components/status.jsx:1745
#: src/components/status.jsx:2414
msgid "Boosted"
msgstr ""
#: src/components/status.jsx:1737
#: src/components/status.jsx:2422
#: src/components/status.jsx:1755
#: src/components/status.jsx:2439
msgid "Bookmarked"
msgstr ""
#: src/components/status.jsx:1741
#: src/components/status.jsx:1759
msgid "Pinned"
msgstr ""
#: src/components/status.jsx:1786
#: src/components/status.jsx:2241
#: src/components/status.jsx:1804
#: src/components/status.jsx:2258
msgid "Deleted"
msgstr ""
#: src/components/status.jsx:1827
#: src/components/status.jsx:1845
msgid "{repliesCount, plural, one {# reply} other {# replies}}"
msgstr ""
#: src/components/status.jsx:1916
#: src/components/status.jsx:1934
msgid "Thread{0}"
msgstr ""
#: src/components/status.jsx:1992
#: src/components/status.jsx:2054
#: src/components/status.jsx:2139
#: src/components/status.jsx:2010
#: src/components/status.jsx:2072
#: src/components/status.jsx:2156
msgid "Show less"
msgstr ""
#: src/components/status.jsx:1992
#: src/components/status.jsx:2054
#: src/components/status.jsx:2010
#: src/components/status.jsx:2072
msgid "Show content"
msgstr ""
#: src/components/status.jsx:2139
#: src/components/status.jsx:2156
msgid "Show media"
msgstr ""
#: src/components/status.jsx:2262
#: src/components/status.jsx:2279
msgid "Edited"
msgstr ""
#: src/components/status.jsx:2339
#: src/components/status.jsx:2356
msgid "Comments"
msgstr ""
#. More from [Author]
#: src/components/status.jsx:2648
#: src/components/status.jsx:2665
msgid "More from <0/>"
msgstr "More from <0/>"
#: src/components/status.jsx:2949
#: src/components/status.jsx:2966
msgid "Edit History"
msgstr ""
#: src/components/status.jsx:2953
#: src/components/status.jsx:2970
msgid "Failed to load history"
msgstr ""
#: src/components/status.jsx:2958
#: src/components/status.jsx:2975
msgid "Loading…"
msgstr ""
#: src/components/status.jsx:3193
#: src/components/status.jsx:3210
msgid "HTML Code"
msgstr ""
#: src/components/status.jsx:3210
#: src/components/status.jsx:3227
msgid "HTML code copied"
msgstr ""
#: src/components/status.jsx:3213
#: src/components/status.jsx:3230
msgid "Unable to copy HTML code"
msgstr ""
#: src/components/status.jsx:3225
#: src/components/status.jsx:3242
msgid "Media attachments:"
msgstr ""
#: src/components/status.jsx:3247
#: src/components/status.jsx:3264
msgid "Account Emojis:"
msgstr ""
#: src/components/status.jsx:3278
#: src/components/status.jsx:3323
#: src/components/status.jsx:3295
#: src/components/status.jsx:3340
msgid "static URL"
msgstr ""
#: src/components/status.jsx:3292
#: src/components/status.jsx:3309
msgid "Emojis:"
msgstr ""
#: src/components/status.jsx:3337
#: src/components/status.jsx:3354
msgid "Notes:"
msgstr ""
#: src/components/status.jsx:3341
#: src/components/status.jsx:3358
msgid "This is static, unstyled and scriptless. You may need to apply your own styles and edit as needed."
msgstr ""
#: src/components/status.jsx:3347
#: src/components/status.jsx:3364
msgid "Polls are not interactive, becomes a list with vote counts."
msgstr ""
#: src/components/status.jsx:3352
#: src/components/status.jsx:3369
msgid "Media attachments can be images, videos, audios or any file types."
msgstr ""
#: src/components/status.jsx:3358
#: src/components/status.jsx:3375
msgid "Post could be edited or deleted later."
msgstr ""
#: src/components/status.jsx:3364
#: src/components/status.jsx:3381
msgid "Preview"
msgstr ""
#: src/components/status.jsx:3373
#: src/components/status.jsx:3390
msgid "Note: This preview is lightly styled."
msgstr ""
#. [Name] [Visibility icon] boosted
#: src/components/status.jsx:3615
#: src/components/status.jsx:3632
msgid "<0/> <1/> boosted"
msgstr ""