2024-10-17 10:19:17 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2024-10-17 10:43:41 -05:00
|
|
|
import { detect } from 'tinyld/heavy';
|
2024-10-17 10:19:17 -05:00
|
|
|
import * as mfm from 'mfm-js';
|
2024-11-02 10:37:22 -05:00
|
|
|
import { miLocalStorage } from '@/local-storage.js';
|
2024-10-17 10:19:17 -05:00
|
|
|
|
|
|
|
export default function detectLanguage(text: string): string {
|
2024-11-02 10:37:22 -05:00
|
|
|
const localLang = (miLocalStorage.getItem('lang') ?? navigator.language).slice(0, 2);
|
2024-10-17 10:19:17 -05:00
|
|
|
const nodes = mfm.parse(text);
|
|
|
|
const filtered = mfm.extract(nodes, (node) => {
|
|
|
|
return node.type === 'text' || node.type === 'quote';
|
|
|
|
});
|
|
|
|
const purified = mfm.toString(filtered);
|
|
|
|
|
2024-11-02 10:37:22 -05:00
|
|
|
if (detect(purified) === '') return localLang;
|
2024-10-17 10:19:17 -05:00
|
|
|
return detect(purified);
|
|
|
|
}
|