-
Notifications
You must be signed in to change notification settings - Fork 87
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
Add language/locale #176
Add language/locale #176
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -181,6 +181,8 @@ open class RouteOptions: NSObject, NSSecureCoding { | |
self.attributeOptions = attributeOptions | ||
|
||
includesExitRoundaboutManeuver = decoder.decodeBool(forKey: "includesExitRoundaboutManeuver") | ||
|
||
locale = Locale(identifier: decoder.decodeObject(of: NSString.self, forKey: "locale") as String? ?? "") | ||
} | ||
|
||
open static var supportsSecureCoding = true | ||
|
@@ -195,6 +197,9 @@ open class RouteOptions: NSObject, NSSecureCoding { | |
coder.encode(routeShapeResolution.description, forKey: "routeShapeResolution") | ||
coder.encode(attributeOptions.description, forKey: "attributeOptions") | ||
coder.encode(includesExitRoundaboutManeuver, forKey: "includesExitRoundaboutManeuver") | ||
if let locale = locale, let languageCode = (locale as NSLocale?)?.object(forKey: .languageCode) as? String { | ||
coder.encode(languageCode, forKey: "locale") | ||
} | ||
} | ||
|
||
// MARK: Specifying the Path of the Route | ||
|
@@ -303,6 +308,13 @@ open class RouteOptions: NSObject, NSSecureCoding { | |
*/ | ||
open var includesExitRoundaboutManeuver = false | ||
|
||
/** | ||
If set, this `Locale` will be used to translate instructions. | ||
|
||
See supported languages [here](https://www.mapbox.com/api-documentation/#instructions-languages). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
*/ | ||
open var locale: Locale? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It’d be nice to default to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep this optional for now. |
||
|
||
/** | ||
An array of URL parameters to include in the request URL. | ||
*/ | ||
|
@@ -319,6 +331,10 @@ open class RouteOptions: NSObject, NSSecureCoding { | |
params.append(URLQueryItem(name: "roundabout_exits", value: String(includesExitRoundaboutManeuver))) | ||
} | ||
|
||
if let locale = locale, let languageCode = (locale as NSLocale?)?.object(forKey: .languageCode) as? String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The language code is only the two-letter ISO 369 code. It doesn’t ever include a region or script code; for that, you need the full locale identifier. Unfortunately, the Directions API currently lacks any sort of fallback mechanism. So this code will fail for Simplified Chinese ( Until the Directions API gains basic language fallback capability, we’ll need to hard-code a whitelist of locales that the Directions API supports. |
||
params.append(URLQueryItem(name: "language", value: languageCode)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once we implement fallback logic on the server side, we’ll want to pass in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. |
||
} | ||
|
||
// Include headings and heading accuracies if any waypoint has a nonnegative heading. | ||
if !waypoints.filter({ $0.heading >= 0 }).isEmpty { | ||
let headings = waypoints.map { $0.headingDescription }.joined(separator: ";") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The language code is only part of the locale. It omits the region and script, which are sometimes relevant (
zh-Hans
,pt-BR
) and will become increasingly relevant in the future. Locale bridges cleanly to NSLocale, which conforms to NSSecureCoding, so castlocale
to NSLocale and encode it directly.