41 lines
1.2 KiB
Go
41 lines
1.2 KiB
Go
package ai
|
|
|
|
import (
|
|
"errors"
|
|
"golang.org/x/text/language"
|
|
)
|
|
|
|
const (
|
|
TTSProtocolDashScopeHTTP = "dashscope_tts_http"
|
|
TTSProtocolDashScopeTask = "dashscope_task_websocket"
|
|
)
|
|
|
|
// Task language labels/locales express the language; DashScope task parameters
|
|
// use ISO language hints. Do not infer an unspecified language from a region.
|
|
func bailianLanguageHint(value string) (string, error) {
|
|
names := map[string]string{"Chinese": "zh", "English": "en", "Japanese": "ja", "Korean": "ko", "German": "de", "French": "fr", "Russian": "ru", "Italian": "it", "Spanish": "es", "Portuguese": "pt"}
|
|
if code, ok := names[value]; ok {
|
|
return code, nil
|
|
}
|
|
tag, err := language.Parse(value)
|
|
if err != nil {
|
|
return "", errors.New("language cannot be expressed by the selected task protocol")
|
|
}
|
|
base, _, _ := tag.Raw()
|
|
if base.String() == "und" {
|
|
return "", errors.New("task language must be explicit")
|
|
}
|
|
return base.String(), nil
|
|
}
|
|
|
|
func bailianTTSLanguageHints(value string) ([]string, error) {
|
|
if value == "Auto" {
|
|
return nil, nil
|
|
} // Explicit user selection, not a missing-value default.
|
|
hint, err := bailianLanguageHint(value)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []string{hint}, nil
|
|
}
|