iosswiftlocalizationlocale

Detect if current settings language in iOS is Latin OR non-Latin (e.g. chinese, japanese)


Currently I used this snippet to check language code (ISO. e.g. en, fr), but now I want to check if there is any way in iOS SDK to detect if preferred language in iOS is Latin OR non-Latin locales (e.g. japanese, chinese, etc.)

private func currentLanguageCode() -> String? {
        guard #available(iOS 16, *), let localeLanguageCode = Locale.current.language.languageCode else {
            return Locale.current.languageCode
        }
        return localeLanguageCode.identifier
    }

Just something like func isLatinLanguage() -> Bool checking default user settings language.


Solution

  • The thing you're looking for is the script:

    Locale(identifier: "fr").language.script   // Latn
    Locale(identifier: "en").language.script   // Latn
    Locale(identifier: "vi").language.script   // Latn
    Locale(identifier: "zh").language.script   // Hans
    Locale(identifier: "ja").language.script   // Jpan
    

    So to your question:

    func isLatinLanguage() -> Bool {
        Locale.current.language.script == .latin
    }