diff --git a/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift b/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift index c2c89503..ed5f4a7d 100644 --- a/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift +++ b/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift @@ -1,6 +1,7 @@ import DesignSystem import Env import Models +import NaturalLanguage import Network import PhotosUI import SwiftUI @@ -126,6 +127,21 @@ public class StatusEditorViewModel: ObservableObject { multiple: pollVotingFrequency.canVoteMultipleTimes, expires_in: pollDuration.rawValue) } + + if !hasExplicitlySelectedLanguage { + // Attempt language resolution using Natural Language + let recognizer = NLLanguageRecognizer() + recognizer.processString(statusText.string) + // Use languageHypotheses to get the probability with it + let hypotheses = recognizer.languageHypotheses(withMaximum: 1) + // Assert that 85% probability is enough :) + // A one word toot that is en/fr compatible is only ~50% confident, for instance + if let (language, probability) = hypotheses.first, probability > 0.85 { + // rawValue return the IETF BCP 47 language tag + selectedLanguage = language.rawValue + } + } + let data = StatusData(status: statusText.string, visibility: visibility, inReplyToId: mode.replyToStatus?.id,