add ToProviderFormat for language code conversion
This commit is contained in:
@@ -118,3 +118,49 @@ func IsValidCode(code string) bool {
|
||||
_, ok := codeIndex[code]
|
||||
return ok
|
||||
}
|
||||
|
||||
// ToProviderFormat converts a canonical language code to the format expected by a specific provider.
|
||||
// Each provider may have different expectations:
|
||||
// - whisper-cpp: uses standard codes like 'en', 'auto' for auto-detect
|
||||
// - openai: uses standard codes like 'en', empty string for auto-detect
|
||||
// - groq: same as openai (OpenAI-compatible)
|
||||
// - mistral: same as openai (OpenAI-compatible)
|
||||
// - deepgram: uses locale codes like 'en-US', 'es' for Spanish
|
||||
// - elevenlabs: uses standard codes or full names depending on API version
|
||||
func ToProviderFormat(code string, providerName string) string {
|
||||
// handle auto-detect (empty code)
|
||||
if code == "" {
|
||||
switch providerName {
|
||||
case "whisper-cpp":
|
||||
return "auto"
|
||||
default:
|
||||
// most providers use empty string or omit the parameter
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
switch providerName {
|
||||
case "deepgram":
|
||||
// deepgram prefers locale codes for some languages
|
||||
return toDeepgramFormat(code)
|
||||
default:
|
||||
// whisper-cpp, openai, groq, mistral, elevenlabs use standard codes
|
||||
return code
|
||||
}
|
||||
}
|
||||
|
||||
// toDeepgramFormat maps standard codes to Deepgram's preferred format
|
||||
func toDeepgramFormat(code string) string {
|
||||
// deepgram uses locale codes for English variants, standard for most others
|
||||
deepgramMappings := map[string]string{
|
||||
"en": "en-US",
|
||||
"es": "es", // Spanish uses base code
|
||||
"pt": "pt-BR", // Portuguese defaults to Brazilian
|
||||
"zh": "zh-CN", // Chinese defaults to Simplified
|
||||
}
|
||||
|
||||
if mapped, ok := deepgramMappings[code]; ok {
|
||||
return mapped
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
@@ -117,3 +117,49 @@ func TestAuto(t *testing.T) {
|
||||
t.Errorf("Auto.Name = %q, want 'Auto-detect'", Auto.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToProviderFormat(t *testing.T) {
|
||||
tests := []struct {
|
||||
code string
|
||||
provider string
|
||||
want string
|
||||
}{
|
||||
// whisper-cpp
|
||||
{"en", "whisper-cpp", "en"},
|
||||
{"es", "whisper-cpp", "es"},
|
||||
{"", "whisper-cpp", "auto"},
|
||||
|
||||
// openai
|
||||
{"en", "openai", "en"},
|
||||
{"", "openai", ""},
|
||||
|
||||
// groq (openai-compatible)
|
||||
{"en", "groq", "en"},
|
||||
{"", "groq", ""},
|
||||
|
||||
// mistral (openai-compatible)
|
||||
{"en", "mistral", "en"},
|
||||
{"", "mistral", ""},
|
||||
|
||||
// deepgram (uses locale codes)
|
||||
{"en", "deepgram", "en-US"},
|
||||
{"es", "deepgram", "es"},
|
||||
{"pt", "deepgram", "pt-BR"},
|
||||
{"zh", "deepgram", "zh-CN"},
|
||||
{"fr", "deepgram", "fr"}, // no special mapping, passthrough
|
||||
{"", "deepgram", ""},
|
||||
|
||||
// elevenlabs
|
||||
{"en", "elevenlabs", "en"},
|
||||
{"", "elevenlabs", ""},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.code+"_"+tt.provider, func(t *testing.T) {
|
||||
got := ToProviderFormat(tt.code, tt.provider)
|
||||
if got != tt.want {
|
||||
t.Errorf("ToProviderFormat(%q, %q) = %q, want %q", tt.code, tt.provider, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user