This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
lang.go
78 lines (63 loc) · 2.12 KB
/
lang.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
77
78
package lang
import (
"fmt"
"github.com/g45t345rt/g45w/assets"
)
type Lang struct {
Key string
Name string
ImgPath string
}
// don't use map[string] the ordering is not guaranteed
var Languages = []Lang{
{Key: "en", Name: "English", ImgPath: "lang/en.png"}, //@lang.Translate("English")
{Key: "fr", Name: "French", ImgPath: "lang/fr.png"}, //@lang.Translate("French")
{Key: "es", Name: "Spanish", ImgPath: "lang/es.png"}, //@lang.Translate("Spanish")
{Key: "it", Name: "Italian", ImgPath: "lang/it.png"}, //@lang.Translate("Italian")
{Key: "nl", Name: "Dutch", ImgPath: "lang/nl.png"}, //@lang.Translate("Dutch")
{Key: "de", Name: "German", ImgPath: "lang/de.png"}, //@lang.Translate("German")
{Key: "ru", Name: "Russian", ImgPath: "lang/ru.png"}, //@lang.Translate("Russian")
{Key: "pt", Name: "Portuguese", ImgPath: "lang/pt.png"}, //@lang.Translate("Portuguese")
{Key: "ro", Name: "Romanian", ImgPath: "lang/ro.png"}, //@lang.Translate("Romanian")
{Key: "jp", Name: "Japanese", ImgPath: "lang/jp.png"}, //@lang.Translate("Japanese")
{Key: "ko", Name: "Korean", ImgPath: "lang/ko.png"}, //@lang.Translate("Korean")
{Key: "zh_s", Name: "Chinese Simplified", ImgPath: "lang/zh.png"}, //@lang.Translate("Chinese Simplified")
{Key: "zh_t", Name: "Chinese Traditional", ImgPath: "lang/zh.png"}, //@lang.Translate("Chinese Traditional")
}
var langValues = make(map[string]map[string]string)
var Current string
func Get(key string) *Lang {
for _, lang := range Languages {
if lang.Key == key {
return &lang
}
}
return nil
}
func Load() error {
for _, lang := range Languages {
if lang.Key == "en" {
continue
}
values, err := assets.GetLang(fmt.Sprintf("%s.json", lang.Key))
if err != nil {
return err
}
langValues[lang.Key] = values
}
return nil
}
func Translate(eng string) string {
values, ok := langValues[Current]
if !ok {
return eng
}
value, ok := values[eng]
if !ok {
return eng
}
if value == "" {
return eng
}
return value
}