Ariver
2026-07-12 24f551a39eae849d891da26cc91f90d354e3a2db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package language
 
import (
    "os"
    "strings"
 
    "voicesnap/internal/model"
)
 
type SystemLocaleDetector interface {
    Detect() string
}
 
type SystemDetector struct{}
 
func NewSystemDetector() SystemDetector {
    return SystemDetector{}
}
 
func (SystemDetector) Detect() string {
    if locale := platformLocale(); strings.TrimSpace(locale) != "" {
        return locale
    }
    for _, key := range []string{"LC_ALL", "LC_MESSAGES", "LANG"} {
        if locale := os.Getenv(key); strings.TrimSpace(locale) != "" {
            return locale
        }
    }
    return ""
}
 
func NormalizeLocale(locale string) string {
    normalized := strings.ToLower(strings.TrimSpace(locale))
    normalized = strings.ReplaceAll(normalized, "_", "-")
    if idx := strings.Index(normalized, "."); idx >= 0 {
        normalized = normalized[:idx]
    }
 
    for _, profile := range model.ListLanguageProfiles() {
        for _, matcher := range profile.SystemMatchers {
            m := strings.ToLower(strings.ReplaceAll(strings.TrimSpace(matcher), "_", "-"))
            if normalized == m || strings.HasPrefix(normalized, m+"-") {
                return profile.ID
            }
        }
    }
 
    return model.EnglishLanguageID
}