fix: check if two languages are close enough
This commit is contained in:
parent
b733948a53
commit
c3a57a3dad
2 changed files with 39 additions and 13 deletions
|
@ -1019,32 +1019,58 @@ function deleteDraft() {
|
||||||
localStorage.setItem("drafts", JSON.stringify(draftData));
|
localStorage.setItem("drafts", JSON.stringify(draftData));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compare two language codes to determine whether they are decisively different
|
||||||
|
* @returns false if they are close enough
|
||||||
|
*/
|
||||||
|
function isSameLanguage(langCode1: string | null, langCode2: string | null) {
|
||||||
|
if (!langCode1 || !langCode2) return false;
|
||||||
|
|
||||||
|
// Sort them alphabetically
|
||||||
|
if (langCode1 > langCode2) {
|
||||||
|
[langCode1, langCode2] = [langCode2, langCode1];
|
||||||
|
}
|
||||||
|
if (langCode2.startsWith(langCode1)) return true;
|
||||||
|
|
||||||
|
const inSameSeries = (series: (string | null)[]) =>
|
||||||
|
series.includes(langCode1) && series.includes(langCode2);
|
||||||
|
|
||||||
|
if (inSameSeries(["zh", "zh-hant", "zh-hans", "yue", "nan"])) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (inSameSeries(["nb", "no", "nn"])) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
async function post() {
|
async function post() {
|
||||||
// For text that is too short, the false positive rate may be too high, so we don't show alarm.
|
// 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) {
|
if (defaultStore.state.autocorrectNoteLanguage && text.value.length > 10) {
|
||||||
const detectedLanguage: string = detectLanguage(text.value) ?? "";
|
const detectedLanguage: string = detectLanguage(text.value) ?? "";
|
||||||
|
|
||||||
const currentLanguageName : string | undefined | false =
|
const currentLanguageName: string | undefined | false =
|
||||||
language.value &&
|
language.value && langmap[language.value]?.nativeName;
|
||||||
langmap[language.value]?.nativeName
|
const detectedLanguageName: string | undefined | false =
|
||||||
const detectedLanguageName : string | undefined | false =
|
detectedLanguage !== "" && langmap[detectedLanguage]?.nativeName;
|
||||||
detectedLanguage !== "" &&
|
|
||||||
detectedLanguage !== language.value &&
|
|
||||||
langmap[detectedLanguage]?.nativeName;
|
|
||||||
|
|
||||||
if (currentLanguageName && detectedLanguageName) {
|
if (
|
||||||
|
currentLanguageName &&
|
||||||
|
detectedLanguageName &&
|
||||||
|
!isSameLanguage(detectedLanguage, language.value)
|
||||||
|
) {
|
||||||
// "canceled" means "post with detected language".
|
// "canceled" means "post with detected language".
|
||||||
const { canceled } = await os.confirm({
|
const { canceled } = await os.confirm({
|
||||||
type: "warning",
|
type: "warning",
|
||||||
text: i18n.t("incorrectLanguageWarning", {
|
text: i18n.t("incorrectLanguageWarning", {
|
||||||
detected: `${detectedLanguageName}`,
|
detected: detectedLanguageName,
|
||||||
current: `${currentLanguageName}`,
|
current: currentLanguageName,
|
||||||
}),
|
}),
|
||||||
okText: i18n.ts.no,
|
okText: i18n.ts.no,
|
||||||
cancelText: i18n.ts.yes,
|
cancelText: i18n.ts.yes,
|
||||||
isPlaintext: true,
|
isPlaintext: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (canceled) {
|
if (canceled) {
|
||||||
language.value = detectedLanguage;
|
language.value = detectedLanguage;
|
||||||
}
|
}
|
||||||
|
|
|
@ -435,7 +435,7 @@ export const defaultStore = markRaw(
|
||||||
autocorrectNoteLanguage: {
|
autocorrectNoteLanguage: {
|
||||||
where: "account",
|
where: "account",
|
||||||
default: true,
|
default: true,
|
||||||
}
|
},
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue