Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a compilation failure & made refactoring #12

Merged
merged 1 commit into from
May 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 19 additions & 9 deletions Sources/SwiftBoost/Foundation/Extensions/LocaleExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,29 @@ public extension Locale {
}

func localised(in locale: Locale) -> String? {
guard let currentLanguageCode = {
if #available(iOS 16, *) {
return self.language.languageCode?.identifier
} else {
return self.languageCode
}
}() else { return nil }
guard let toLanguageCode = {
if #available(iOS 16, *) {
/**
Returns the identifier for the language code of the given `Locale`.

This method checks for the availability of specific APIs introduced in iOS 16.0 and macOS 13.0 versions or later. If these APIs are available, it returns the `identifier` property of the `languageCode` from the `Locale`. Otherwise, it uses the `languageCode` property directly from the `Locale`.

- Parameter locale: The `Locale` for which the language code identifier is required.

- Returns: The language code identifier of the given `Locale`. If the specific APIs are unavailable (i.e., the device's OS version is older than iOS 16.0 or macOS 13.0), it returns the language code directly.
*/
func supportedLangCodeId(for locale: Locale) -> String? {
// Checks for the availability of specific APIs introduced in iOS 16.0 and macOS 13.0 versions or later.
if #available(iOS 16.0, macOS 13.0, *) {
return locale.language.languageCode?.identifier
} else {
return locale.languageCode
}
}

guard let currentLanguageCode = {
supportedLangCodeId(for: self)
}() else { return nil }
guard let toLanguageCode = {
supportedLangCodeId(for: locale)
}() else { return nil }
let nslocale = NSLocale(localeIdentifier: toLanguageCode)
let text = nslocale.displayName(forKey: NSLocale.Key.identifier, value: currentLanguageCode)
Expand Down