Conversation
|
|
||
| // Only localized languages will have a proper fallback voice | ||
| utterance.voice = AVSpeechSynthesisVoice(language: Bundle.main.preferredLocalizations.first) | ||
| utterance.voice = AVSpeechSynthesisVoice(language: Locale.preferredLanguages.first!) |
There was a problem hiding this comment.
Bundle.main.preferredLocalizations.first is the localization used by the current application (taking into account the Preferred Languages in Settings), whereas Locale.preferredLanguages are the locales listed under Preferred Languages in Settings (regardless of the available localizations).
For example, if the application is localized into only English and Simplified Chinese, but the user's preferred languages are Spanish and English (in that order), this new code will cause the application to read Spanish instructions (because OSRM Text Instructions are available in Spanish) with an English voice, spliced into English format strings. You'd hear, "In 3 kilómetros, gire a la izquierda, then ir por 10 kilómetros" – all with an English voice. The old code would've given English instructions in English, consistent with an interface already in English. If the application wants to support Spanish, it needs to provide a Spanish localization.
There was a problem hiding this comment.
Instead of this change, I suspect this line that determines the Polly voice needs to be switched from Locale to Bundle.
There was a problem hiding this comment.
@1ec5 how can we get even more specific localization than just en. If I set my region to en-us, Bundle.main.preferredLocalizations.first will only return en.
There was a problem hiding this comment.
en should be good enough for AVSpeechSynthesizer. If it isn’t specific enough for Polly, use Locale.current.regionCode. (regionCode is only available on iOS 10+, but you can use Locale.object(forKey:) with NSLocale.Key.countryCode to get the same information.
|
While we’re at it, we should also be using the Amy or Emma voice for British English. |
That’s the expected behavior, since France uses the metric system. |
MapboxNavigation/Locale.swift
Outdated
| extension Locale { | ||
| static var preferredLocalLanguageCountryCode: String { | ||
| let bundleLanguage = Bundle.main.preferredLocalizations.first! | ||
| let bundleLanguages = bundleLanguage.components(separatedBy: "-") |
There was a problem hiding this comment.
This isn’t a list of languages, it’s a list of components detailing a single locale. See this article for more information.
MapboxNavigation/Locale.swift
Outdated
| return bundleLanguage | ||
| } | ||
|
|
||
| let countryCode = NSLocale.Key.countryCode |
There was a problem hiding this comment.
No need for a separate variable for this constant.
MapboxNavigation/Locale.swift
Outdated
| } | ||
|
|
||
| let countryCode = NSLocale.Key.countryCode | ||
| let locale = NSLocale.current as NSLocale |
There was a problem hiding this comment.
No need for a separate variable for the result of this cast. Surround it with parentheses and inline it below.
MapboxNavigation/Locale.swift
Outdated
| let bundleLanguage = Bundle.main.preferredLocalizations.first! | ||
| let bundleLanguages = bundleLanguage.components(separatedBy: "-") | ||
|
|
||
| if bundleLanguages.count == 2 { |
There was a problem hiding this comment.
It’s possible for the host application to have a localization whose locale code contains more than two components, for instance zh-Hant-HK for Traditional Chinese in Hong Kong versus zh-Hant-TW for the same in Taiwan.
| super.viewDidLoad() | ||
| automaticallyAdjustsScrollViewInsets = false | ||
|
|
||
| distanceFormatter.numberFormatter.locale = Locale.init(identifier: Locale.preferredLocalLanguageCountryCode) |
| override public init() { | ||
| super.init() | ||
| maneuverVoiceDistanceFormatter.unitStyle = .long | ||
| maneuverVoiceDistanceFormatter.numberFormatter.locale = Locale.init(identifier: Locale.preferredLocalLanguageCountryCode) |
|
|
||
| turnArrowView.step = step | ||
| titleLabel.text = routeStepFormatter.string(for: step) | ||
| distanceFormatter.numberFormatter.locale = Locale.init(identifier: Locale.preferredLocalLanguageCountryCode) |
There was a problem hiding this comment.
Given how often we have to write this, it might be more convenient to add another variable to Locale called nationalizedCurrent that returns the same Locale as this line is creating.
|
@1ec5 updated |
1ec5
left a comment
There was a problem hiding this comment.
Looks good. Just a couple stylistic comments below.
MapboxNavigation/Locale.swift
Outdated
| return firstBundleLocale | ||
| } | ||
|
|
||
| let countryCode = NSLocale.Key.countryCode |
There was a problem hiding this comment.
Nit: inline this expression. It’s a constant, so there’s no advantage to breaking it out. In fact, since NSLocale.object(forKey:) takes an NSLocale.Key, which is an enumeration, you can pass in just .countryCode.
| super.viewDidLoad() | ||
| automaticallyAdjustsScrollViewInsets = false | ||
|
|
||
| distanceFormatter.numberFormatter.locale = Locale.nationalizedCurrent |
There was a problem hiding this comment.
You might be able to say just .nationalizedCurrent here, but I’m not sure.
Closes: #183
Bundle.main.preferredLocalizations.firstonly would specifyenvs withLocale.preferredLanguageswhich producesen-us. We use the same lookup for Polly voices./cc @incanus