Skip to content
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

Merged
merged 6 commits into from
Oct 9, 2017
Merged

Add language/locale #176

merged 6 commits into from
Oct 9, 2017

Conversation

bsudekum
Copy link

Closes: #133

/cc @1ec5

@bsudekum bsudekum requested a review from 1ec5 September 15, 2017 22:55
@@ -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 {
Copy link
Contributor

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 cast locale to NSLocale and encode it directly.

@@ -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 {
Copy link
Contributor

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 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 (zh-Hant), yet if we were to pass in the full locale identifier, the code would break for a Spanish speaker in Mexico (es-MX) instead.

Until the Directions API gains basic language fallback capability, we’ll need to hard-code a whitelist of locales that the Directions API supports.

@bsudekum
Copy link
Author

@1ec5 updated.

@@ -315,6 +316,8 @@ open class RouteOptions: NSObject, NSSecureCoding {
*/
open var locale: Locale?

private let supportedLocales: [String] = ["de", "en", "es", "fr", "id", "nl", "ru", "sv", "vi", "zh-hans"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the locale is Simplified Chinese, its locale identifier will be zh-Hans, not zh-hans. The Directions API accepts zh-Hans as a language.

if supportedLocales.contains(languageCode) {
params.append(URLQueryItem(name: "language", value: languageCode))
} else {
params.append(URLQueryItem(name: "language", value: "en"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to append any language parameter in this case?

@@ -332,7 +335,11 @@ open class RouteOptions: NSObject, NSSecureCoding {
}

if let locale = locale, let languageCode = (locale as NSLocale?)?.object(forKey: .languageCode) as? String {
params.append(URLQueryItem(name: "language", value: languageCode))
if supportedLocales.contains(languageCode) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail for a Locale with the identifier zh-Hans, since its languageCode will be just zh.

@1ec5
Copy link
Contributor

1ec5 commented Sep 16, 2017

Added the DO NOT MERGE label because we’re looking into whether this language fallback logic can be implemented in the Directions API itself.

if languageCode == "zh" {
languageCode = "zh-Hans"
}
params.append(URLQueryItem(name: "language", value: languageCode))
Copy link
Contributor

Choose a reason for hiding this comment

The 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 locale.localeIdentifier here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

@bsudekum
Copy link
Author

bsudekum commented Oct 6, 2017

@1ec5 once Project-OSRM/osrm-text-instructions#168 is deployed, we can start to look at merging this.

Copy link
Contributor

@1ec5 1ec5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, other than the documentation comment.

@@ -304,6 +310,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).
Copy link
Contributor

@1ec5 1ec5 Oct 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The locale in which the routes’ instructions are written.

If you use MapboxDirections.swift with the Mapbox Directions API, this property affects the sentence contained within the RouteStep.instructions property, but it does not affect any road names contained in that property or other properties such as RouteStep.name.

The Directions API can provide instructions in a number of languages. Set this property to Bundle.main.preferredLocalizations.first or Locale.autoupdatingCurrent to match the application’s language or the system language, respectively.

By default, this property is set to nil, causing instructions to be written in the default language, English.


See supported languages [here](https://www.mapbox.com/api-documentation/#instructions-languages).
*/
open var locale: Locale?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’d be nice to default to Locale.autoupdatingCurrent or Bundle.main.preferredLocalizations.first so the behavior is always what the user would expect, but that can happen as tail work, possibly at the same time that we set a default locale in MapboxGeocoder.swift.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this optional for now.

@bsudekum bsudekum merged commit c897c6d into master Oct 9, 2017
@bsudekum bsudekum deleted the locale branch October 9, 2017 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants