diff --git a/Sources/MapboxSpeech/MBSpeechOptions.swift b/Sources/MapboxSpeech/MBSpeechOptions.swift index b59a049..b136acb 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,30 @@ 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" : "arb", + "zh" : "cmn-CN" + ] + 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 + } +} + 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 +}