Skip to content

Commit ae86da0

Browse files
committed
Fix a compilation & made refactoring
1 parent 6561ca9 commit ae86da0

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

Sources/SwiftBoost/Foundation/Extensions/LocaleExtension.swift

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,29 @@ public extension Locale {
1212
}
1313

1414
func localised(in locale: Locale) -> String? {
15-
guard let currentLanguageCode = {
16-
if #available(iOS 16, *) {
17-
return self.language.languageCode?.identifier
18-
} else {
19-
return self.languageCode
20-
}
21-
}() else { return nil }
22-
guard let toLanguageCode = {
23-
if #available(iOS 16, *) {
15+
/**
16+
Returns the identifier for the language code of the given `Locale`.
17+
18+
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`.
19+
20+
- Parameter locale: The `Locale` for which the language code identifier is required.
21+
22+
- 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.
23+
*/
24+
func supportedLangCodeId(for locale: Locale) -> String? {
25+
// Checks for the availability of specific APIs introduced in iOS 16.0 and macOS 13.0 versions or later.
26+
if #available(iOS 16.0, macOS 13.0, *) {
2427
return locale.language.languageCode?.identifier
2528
} else {
2629
return locale.languageCode
2730
}
31+
}
32+
33+
guard let currentLanguageCode = {
34+
supportedLangCodeId(for: self)
35+
}() else { return nil }
36+
guard let toLanguageCode = {
37+
supportedLangCodeId(for: locale)
2838
}() else { return nil }
2939
let nslocale = NSLocale(localeIdentifier: toLanguageCode)
3040
let text = nslocale.displayName(forKey: NSLocale.Key.identifier, value: currentLanguageCode)

0 commit comments

Comments
 (0)