Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion Sources/MapboxSpeech/MBSpeechOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
]

Expand All @@ -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<String, String> = [
"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
}
}

7 changes: 6 additions & 1 deletion Sources/MapboxSpeechCLI/main.swift
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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)")

Expand All @@ -31,4 +36,4 @@ do {
RunLoop.main.run(until: Date().addingTimeInterval(audioPlayer.duration))
} catch {
print("Error occured: \(error)")
}
}