feat: Automatically detect and warn to correct the language of post

This commit is contained in:
Lhcfl 2024-03-20 01:08:14 +08:00
parent 605e97181b
commit 872ea8adb0
7 changed files with 47 additions and 1 deletions

View file

@ -2227,3 +2227,5 @@ moreUrlsDescription: "Enter the pages you want to pin to the help menu in the lo
left corner using this notation:\n\"Display name\": https://example.com/"
messagingUnencryptedInfo: "Chats on Firefish are not end-to-end encrypted. Don't share
any sensitive infomation over Firefish."
autocorrectNoteLanguage: Automatically detect and warn to correct the language of your post.
incorrectLanguageWarning: "It looks like your post is in {detected}, but the language you selected is {current}.\nWould you like to post in {detected} instead?"

View file

@ -2056,3 +2056,5 @@ searchRangeDescription: "如果您要过滤时间段,请按以下格式输入
messagingUnencryptedInfo: "Firefish 上的聊天没有经过端到端加密,请不要在聊天中分享您的敏感信息。"
noAltTextWarning: 有些附件没有描述。您是否忘记写描述了?
showNoAltTextWarning: 当您尝试发布没有描述的帖子附件时显示警告
autocorrectNoteLanguage: 自动检测和弹窗纠正您所使用的帖子语言
incorrectLanguageWarning: "看上去您帖子使用的语言是{detected},但您选择的语言是{current}。\n要改为以{detected}发帖吗?"

View file

@ -1020,6 +1020,37 @@ function deleteDraft() {
}
async function post() {
// For text that is too short, the false positive rate may be too high, so we don't show alarm.
if (defaultStore.state.autocorrectNoteLanguage && text.value.length > 10) {
const detectedLanguage: string = detectLanguage(text.value) ?? "";
const currentLanguageName : string | undefined | false =
language.value &&
langmap[language.value]?.nativeName
const detectedLanguageName : string | undefined | false =
detectedLanguage !== "" &&
detectedLanguage !== language.value &&
langmap[detectedLanguage]?.nativeName;
if (currentLanguageName && detectedLanguageName) {
// "canceled" means "post with detected language".
const { canceled } = await os.confirm({
type: "warning",
text: i18n.t("incorrectLanguageWarning", {
detected: `${detectedLanguageName}`,
current: `${currentLanguageName}`,
}),
okText: i18n.ts.no,
cancelText: i18n.ts.yes,
isPlaintext: true,
});
if (canceled) {
language.value = detectedLanguage;
}
}
}
if (
defaultStore.state.showNoAltTextWarning &&
files.value.some((f) => f.comment == null || f.comment.length === 0)

View file

@ -22,7 +22,7 @@ class I18n<T extends Record<string, any>> {
if (args) {
for (const [k, v] of Object.entries(args)) {
str = str.replace(`{${k}}`, v.toString());
str = str.replaceAll(`{${k}}`, v.toString());
}
}
return str;

View file

@ -124,6 +124,9 @@
<FormSwitch v-model="showNoAltTextWarning" class="_formBlock">{{
i18n.ts.showNoAltTextWarning
}}</FormSwitch>
<FormSwitch v-model="autocorrectNoteLanguage" class="_formBlock">{{
i18n.ts.autocorrectNoteLanguage
}}</FormSwitch>
<FormSelect v-model="serverDisconnectedBehavior" class="_formBlock">
<template #label>{{ i18n.ts.whenServerDisconnected }}</template>
@ -530,6 +533,9 @@ const pullToRefreshThreshold = computed(
const showNoAltTextWarning = computed(
defaultStore.makeGetterSetter("showNoAltTextWarning"),
);
const autocorrectNoteLanguage = computed(
defaultStore.makeGetterSetter("autocorrectNoteLanguage"),
);
// This feature (along with injectPromo) is currently disabled
// function onChangeInjectFeaturedNote(v) {

View file

@ -125,6 +125,7 @@ const defaultStoreSaveKeys: (keyof (typeof defaultStore)["state"])[] = [
"enablePullToRefresh",
"pullToRefreshThreshold",
"showNoAltTextWarning",
"autocorrectNoteLanguage",
];
const coldDeviceStorageSaveKeys: (keyof typeof ColdDeviceStorage.default)[] = [
"lightTheme",

View file

@ -432,6 +432,10 @@ export const defaultStore = markRaw(
where: "account",
default: true,
},
autocorrectNoteLanguage: {
where: "account",
default: true,
}
}),
);