Files
AynaLivePlayer/pkg/i18n/i18n.go
Aynakeya 5cc5948a85 Merge 1.0.x branch (#8)
* rewrite

* update submodule

* make width height configurable

* update dependency

* update

* update file

* update dep

* fix basic config layout

* update plugin management

* more stuff

* add blacklist

* fix todo

* fix windows gethandle

* update windows update guide

* update windows build guide

* include go mod tidy in script

* update todo

* fix source session

* fix text output

* add plugin play duration control

* fix id diange not working

* update todo

* update version number
2024-04-22 21:21:02 -07:00

64 lines
1.4 KiB
Go

package i18n
import (
config2 "AynaLivePlayer/pkg/config"
"AynaLivePlayer/pkg/util"
"encoding/json"
"os"
)
const FILENAME = "translation.json"
type Translation struct {
Languages []string
Messages map[string]map[string]string
}
func (t *Translation) HasLanguage(lang string) bool {
for _, l := range t.Languages {
if l == lang {
return true
}
}
return false
}
var TranslationMap Translation
var CurrentLanguage string
func init() {
TranslationMap = Translation{make([]string, 0), make(map[string]map[string]string)}
file, err := os.ReadFile(config2.GetAssetPath(FILENAME))
if err == nil {
_ = json.Unmarshal([]byte(file), &TranslationMap)
}
LoadLanguage(config2.General.Language)
}
func LoadLanguage(lang string) {
CurrentLanguage = lang
if TranslationMap.HasLanguage(lang) {
return
}
TranslationMap.Languages = append(TranslationMap.Languages, lang)
for id, m := range TranslationMap.Messages {
m[lang] = id
}
}
func T(id string) string {
if x, ok := TranslationMap.Messages[id]; ok {
return x[CurrentLanguage]
}
TranslationMap.Messages[id] = make(map[string]string)
for _, l := range TranslationMap.Languages {
TranslationMap.Messages[id][l] = id
}
return id
}
func SaveTranslation() {
content, _ := util.MarshalIndentUnescape(TranslationMap, "", " ")
_ = os.WriteFile(config2.GetAssetPath(FILENAME), []byte(content), 0666)
}