Skip to content

Commit a8217de

Browse files
Locale.preferredLocales (#1291)
* Locale.preferredLocales Initial proposal * Update Proposals/0026-preferredLocales.md Fix link Co-authored-by: Tina L <[email protected]> --------- Co-authored-by: Tina L <[email protected]>
1 parent bfc4580 commit a8217de

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

Proposals/0026-preferredLocales.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Locale.preferredLocales
2+
3+
* Proposal: [SF-0026](0026-preferredLocales.md)
4+
* Authors: [करन मिश्र · Karan Miśra](https://github.com/karan-misra)
5+
* Review Manager: TBD
6+
* Status: **Awaiting review**
7+
* Review: ([pitch](https://forums.swift.org/t/pitch-introduce-locale-preferredlocales/79900))
8+
9+
## Introduction
10+
11+
Add `Locale.preferredLocales` as an alternative to `Locale.preferredLanguages` that returns `[Locale]` instead of `[String]`.
12+
13+
## Motivation
14+
15+
Currently, `Locale.preferredLanguages` is the only way to retrieve the list of languages that the user has specified in Language & Region settings. This follows its predecessor `+[NSLocale preferredLanguages]` and returns an array of `String`s instead of `Locale`s. Processing and manipulating strings is complex and errorprone for clients.
16+
17+
This proposal introduces `Locale.preferredLocales` as a way to retrieve the same information, but in the form of an array of `Locale`s which will allow clients to use the information more easily and with fewer errors, specifically when used to customize the presentation of data within their apps such that content in the user’s preferred languages is more prominent.
18+
19+
## Proposed solution
20+
21+
We propose adding `preferredLocales` as a static variable on `Locale`, similarly to `preferredLanguages`. One of the primary use cases is to allow apps to build language selection menus in which the user’s preferred locales are bubbled up to the top. This can be achieved with the proposed `preferredLocales` API as follows:
22+
23+
```swift
24+
// When building a language selection menu, `matchedLocales` would be shown at the top, and `otherLocales` would be shown below, with a visual divider.
25+
var matchedLocales = []
26+
var otherLocales = []
27+
let availableLocales = // ... array of Locale objects ...
28+
for locale in availableLocales {
29+
var foundMatch = false
30+
for preferredLocale in preferredLocales {
31+
if locale.language.isEquivalent(to: preferredLocale.language) {
32+
matchedLocales.append(locale)
33+
foundMatch = true
34+
break
35+
}
36+
}
37+
if !foundMatch {
38+
otherLocales.append(locale)
39+
}
40+
}
41+
```
42+
43+
## Detailed design
44+
45+
```swift
46+
public struct Locale : Hashable, Equatable, Sendable {
47+
48+
/// Returns a list of the user’s preferred locales, as specified in Language & Region settings, taking into account any per-app language overrides.
49+
@available(FoundationPreview 6.2, *)
50+
public static var preferredLocales: [Locale]
51+
}
52+
```
53+
54+
## Source compatibility
55+
56+
There is no impact on source compatibility or existing code.
57+
58+
## Implications on adoption
59+
60+
This feature can be freely adopted and un-adopted in source code with no deployment constraints and without affecting source compatibility.
61+
62+
## Future directions
63+
64+
In order to further support the use case of building language selection UIs, we can consider adding convenience functions on `Locale` that allow sorting and splitting a list of available `Locale`s into `preferred` and `remaining`, which can then be used to directly populate the UI.
65+
66+
```swift
67+
public static func sorted(_ available: [Locale]) -> (preferred: [Locale], remaining: [Locale])
68+
```
69+
70+
We can also consider adding APIs that work with `Locale.Language` in addition to `Locale` since in many use cases, the developer is handling a list of languages does not need the additional functionality in `Locale`.
71+
72+
Lastly, we can choose to deprecate `Locale.preferredLanguages` since it returns the same information but using `String` which is not a good container for a language identifier and leads to incorrect usage.
73+
74+
## Alternatives considered
75+
76+
* Naming-wise, another possibility was `Locale.preferred`. However, following the current naming convention, this would be confused as returning `Locale` and not `[Locale]`. Additionally, it would be best to keep `Locale.preferred` open in case we need a way to get the first, most preferred `Locale` in the future.
77+
* Deprecate `preferredLanguages` and encourage developers to only use `preferredLocales`.

0 commit comments

Comments
 (0)