You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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) {
/// 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
+
publicstaticvar 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.
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`.
/// Returns an attribute container storing only the attributes in `self` with the `inheritedByAddedText` property set to `true`
111
+
/// Returns a copy of the attribute container with only attributes that specify the provided inheritance behavior.
112
+
/// - Parameter inheritedByAddedText: An `inheritedByAddedText` value to filter. Attributes matching this value are included in the returned container.
113
+
/// - Returns: A copy of the attribute container with only attributes whose `inheritedByAddedText` property matches the provided value.
/// Returns an attribute container storing only the attributes in `self` with a matching run boundary property
124
-
///
125
-
/// Note: if `nil` is provided then only attributes not bound to any particular boundary will be returned
125
+
/// Returns a copy of the attribute container with only attributes that have the provided run boundaries.
126
+
/// - Parameter runBoundaries: The required `runBoundaries` value of the filtered attributes. If `nil` is provided, only attributes not bound to any specific boundary will be returned.
127
+
/// - Returns: A copy of the attribute container with only attributes whose `runBoundaries` property matches the provided value.
0 commit comments