Skip to content

Commit

Permalink
Updated locale extensinon.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvorobei committed Oct 30, 2023
1 parent 5885499 commit e5935c8
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions Sources/SwiftBoost/Foundation/Extensions/LocaleExtension.swift
Expand Up @@ -12,17 +12,8 @@ public extension Locale {
}

func localised(in locale: Locale) -> String? {
/**
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 {
Expand All @@ -33,9 +24,11 @@ public extension Locale {
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)
return text?.localizedCapitalized
Expand Down

0 comments on commit e5935c8

Please sign in to comment.