From 4071fb57bf228a7ad84b442a17e76b2b0a44d92e Mon Sep 17 00:00:00 2001 From: Elyes Date: Sun, 6 Feb 2022 18:54:05 +0100 Subject: [PATCH 1/2] Added support for Command line language code, Use Locale.amazonIdneitifier to fix unsupported language codes. [ar-SA : arb, zh : cmn-CN] --- Sources/MapboxSpeech/MBSpeechOptions.swift | 30 +++++++++++++++++++++- Sources/MapboxSpeechCLI/main.swift | 7 ++++- 2 files changed, 35 insertions(+), 2 deletions(-) mode change 100755 => 100644 Sources/MapboxSpeechCLI/main.swift diff --git a/Sources/MapboxSpeech/MBSpeechOptions.swift b/Sources/MapboxSpeech/MBSpeechOptions.swift index b59a049..dda4704 100644 --- a/Sources/MapboxSpeech/MBSpeechOptions.swift +++ b/Sources/MapboxSpeech/MBSpeechOptions.swift @@ -74,7 +74,7 @@ open class SpeechOptions: Codable { internal var params: [URLQueryItem] { var params: [URLQueryItem] = [ URLQueryItem(name: "textType", value: String(describing: textType)), - URLQueryItem(name: "language", value: locale.identifier), + URLQueryItem(name: "language", value: locale.amazonIdentifier), URLQueryItem(name: "outputFormat", value: String(describing: outputFormat)) ] @@ -85,3 +85,31 @@ open class SpeechOptions: Codable { return params } } + +public extension Locale { + + /** + `String` Returns the identifier of the locale identifier supported by Amazon Polly [`Supported Language`](https://docs.aws.amazon.com/polly/latest/dg/SupportedLanguage.html). + + While common language identifiers are two-letter `ISO 639-1` standard, three-letter `ISO 639-2` standard or even `RFC 4647` (known as BCP 47), `Amazon Polly` uses `ISO 639-3` + W3C language identification which creates incompatibility with `Locale.current`. + This computed property either return `Locale.identifier` or the supported version of the unknown code. + List of currently unsupported codes : + "ar-SA", "zh-CN", "zh-HK", "zh-Hans", "zh-Hant", "zh-TW" + */ + var amazonIdentifier : String { + let unsupported : Dictionary = [ + "ar-SA" : "arb", + "zh-CN" : "cmn-CN", + "zh-HK" : "cmn-CN", + "zh-Hans" : "cmn-CN" , + "zh-Hant" : "cmn-CN", + "zh-TW" : "cmn-CN", + ] + if unsupported.keys.contains(self.identifier), let patchedIdentifier = unsupported[ self.identifier ] { + return patchedIdentifier + } + return self.identifier + } +} + diff --git a/Sources/MapboxSpeechCLI/main.swift b/Sources/MapboxSpeechCLI/main.swift old mode 100755 new mode 100644 index 6b55711..514eb61 --- a/Sources/MapboxSpeechCLI/main.swift +++ b/Sources/MapboxSpeechCLI/main.swift @@ -18,6 +18,11 @@ let text = CommandLine.arguments[1] let options = SpeechOptions(text: text) var speech = SpeechSynthesizer(accessToken: token) +if CommandLine.arguments.count > 2 { + let language = CommandLine.arguments[2] + options.locale = .init(identifier: language) +} + let url = speech.url(forSynthesizing: options) print("URL: \(url)") @@ -31,4 +36,4 @@ do { RunLoop.main.run(until: Date().addingTimeInterval(audioPlayer.duration)) } catch { print("Error occured: \(error)") -} \ No newline at end of file +} From 59a32100557ca66781814c3d14c413f489fb906f Mon Sep 17 00:00:00 2001 From: Elyes Date: Tue, 8 Feb 2022 19:07:49 +0100 Subject: [PATCH 2/2] Properly support device language code from identifier --- Sources/MapboxSpeech/MBSpeechOptions.swift | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Sources/MapboxSpeech/MBSpeechOptions.swift b/Sources/MapboxSpeech/MBSpeechOptions.swift index dda4704..b136acb 100644 --- a/Sources/MapboxSpeech/MBSpeechOptions.swift +++ b/Sources/MapboxSpeech/MBSpeechOptions.swift @@ -99,15 +99,14 @@ public extension Locale { */ var amazonIdentifier : String { let unsupported : Dictionary = [ - "ar-SA" : "arb", - "zh-CN" : "cmn-CN", - "zh-HK" : "cmn-CN", - "zh-Hans" : "cmn-CN" , - "zh-Hant" : "cmn-CN", - "zh-TW" : "cmn-CN", + "ar" : "arb", + "zh" : "cmn-CN" ] - if unsupported.keys.contains(self.identifier), let patchedIdentifier = unsupported[ self.identifier ] { - return patchedIdentifier + let languageComponents = Locale.components(fromIdentifier: self.identifier) + if let languageCode = languageComponents["kCFLocaleLanguageCodeKey"] { + if unsupported.keys.contains(languageCode), let patchedIdentifier = unsupported[ languageCode ] { + return patchedIdentifier + } } return self.identifier }