-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.go
76 lines (67 loc) · 1.83 KB
/
i18n.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package i18n
import (
"fmt"
"os"
"strings"
)
var EasyI18nTranslations = map[string]map[string]string{}
var currentLanguage string
func SetCustomTranslations(translations map[string]map[string]string) {
EasyI18nTranslations = translations
}
func InitLanguage() {
envVars := []string{"LANG", "LC_ALL", "LC_MESSAGES"}
var languageCode string
for _, envVar := range envVars {
if lang, exists := os.LookupEnv(envVar); exists {
parts := strings.Split(lang, "_")
if len(parts) > 0 {
languageCode = parts[0]
break
}
}
}
switch {
case strings.HasPrefix(languageCode, "zh"):
if len(strings.Split(languageCode, "_")) > 1 && strings.HasSuffix(languageCode, "TW") {
currentLanguage = "zht"
} else {
currentLanguage = "zhs"
}
case strings.HasPrefix(languageCode, "ja"):
currentLanguage = "jp"
case strings.HasPrefix(languageCode, "fr"):
currentLanguage = "fr"
case strings.HasPrefix(languageCode, "es"):
currentLanguage = "es"
case strings.HasPrefix(languageCode, "de"):
currentLanguage = "de"
case strings.HasPrefix(languageCode, "it"):
currentLanguage = "it"
case strings.HasPrefix(languageCode, "pt"):
currentLanguage = "pt"
case strings.HasPrefix(languageCode, "ru"):
currentLanguage = "ru"
case strings.HasPrefix(languageCode, "ko"):
currentLanguage = "ko"
case strings.HasPrefix(languageCode, "ar"):
currentLanguage = "ar"
case strings.HasPrefix(languageCode, "hi"):
currentLanguage = "hi"
default:
currentLanguage = "en"
}
}
func SetLanguage(languageCode string) {
currentLanguage = languageCode
}
func Translate(key string, args ...interface{}) string {
if val, ok := EasyI18nTranslations[currentLanguage][key]; ok {
return fmt.Sprintf(val, args...)
}
if val, ok := EasyI18nTranslations["en"][key]; ok {
return fmt.Sprintf(val, args...)
}
missingKey := "$" + key
return missingKey
}