2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
2022-08-07 18:59:56 +03:00
|
|
|
"os"
|
2021-09-07 17:36:50 +03:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var stringResources = map[string]map[string]string{}
|
|
|
|
|
2024-05-20 16:51:30 +03:00
|
|
|
func (resources *resourceManager) scanEmbedStringsDir(fs *embed.FS, dir string) {
|
2021-09-07 17:36:50 +03:00
|
|
|
if files, err := fs.ReadDir(dir); err == nil {
|
|
|
|
for _, file := range files {
|
|
|
|
name := file.Name()
|
|
|
|
path := dir + "/" + name
|
|
|
|
if file.IsDir() {
|
2024-05-20 16:51:30 +03:00
|
|
|
resources.scanEmbedStringsDir(fs, path)
|
2021-09-07 17:36:50 +03:00
|
|
|
} else if strings.ToLower(filepath.Ext(name)) == ".rui" {
|
|
|
|
if data, err := fs.ReadFile(path); err == nil {
|
|
|
|
loadStringResources(string(data))
|
|
|
|
} else {
|
|
|
|
ErrorLog(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-20 16:51:30 +03:00
|
|
|
func (resources *resourceManager) scanStringsDir(path string) {
|
2022-08-07 18:59:56 +03:00
|
|
|
if files, err := os.ReadDir(path); err == nil {
|
2021-09-07 17:36:50 +03:00
|
|
|
for _, file := range files {
|
|
|
|
filename := file.Name()
|
|
|
|
if filename[0] != '.' {
|
|
|
|
newPath := path + `/` + filename
|
|
|
|
if file.IsDir() {
|
2024-05-20 16:51:30 +03:00
|
|
|
resources.scanStringsDir(newPath)
|
2021-09-07 17:36:50 +03:00
|
|
|
} else if strings.ToLower(filepath.Ext(newPath)) == ".rui" {
|
2022-08-07 18:59:56 +03:00
|
|
|
if data, err := os.ReadFile(newPath); err == nil {
|
2021-09-07 17:36:50 +03:00
|
|
|
loadStringResources(string(data))
|
|
|
|
} else {
|
|
|
|
ErrorLog(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
Added some properties and functions
* Added "resize", "grid-auto-flow", "caret-color", and "backdrop-filter" properties
* Added BlurView, BlurViewByID, GetResize, GetGridAutoFlow, GetCaretColor, GetBackdropFilter functions
* The "warp" property for ListView and ListLayout renamed to "list-warp"
* The "warp" property for EditView renamed to "edit-warp"
* Added CertFile and KeyFile optional fields to the AppParams struct.If they are set, then an https connection is created, otherwise http.
2022-06-07 13:07:10 +03:00
|
|
|
DebugLog(err.Error())
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadStringResources(text string) {
|
|
|
|
data := ParseDataText(text)
|
|
|
|
if data == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
parseStrings := func(obj DataObject, lang string) {
|
|
|
|
table, ok := stringResources[lang]
|
|
|
|
if !ok {
|
|
|
|
table = map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < obj.PropertyCount(); i++ {
|
|
|
|
if prop := obj.Property(i); prop != nil && prop.Type() == TextNode {
|
|
|
|
table[prop.Tag()] = prop.Text()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stringResources[lang] = table
|
|
|
|
}
|
|
|
|
|
|
|
|
tag := data.Tag()
|
|
|
|
if tag == "strings" {
|
|
|
|
for i := 0; i < data.PropertyCount(); i++ {
|
|
|
|
if prop := data.Property(i); prop != nil && prop.Type() == ObjectNode {
|
|
|
|
parseStrings(prop.Object(), prop.Tag())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if strings.HasPrefix(tag, "strings:") {
|
|
|
|
if lang := tag[8:]; lang != "" {
|
|
|
|
parseStrings(data, lang)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetString returns the text for the language which is defined by "lang" parameter
|
|
|
|
func GetString(tag, lang string) (string, bool) {
|
|
|
|
if table, ok := stringResources[lang]; ok {
|
|
|
|
if text, ok := table[tag]; ok {
|
|
|
|
return text, true
|
|
|
|
}
|
|
|
|
DebugLogF(`There is no "%s" string resource`, tag)
|
|
|
|
}
|
|
|
|
DebugLogF(`There are no "%s" language resources`, lang)
|
|
|
|
return tag, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *sessionData) GetString(tag string) (string, bool) {
|
|
|
|
getString := func(tag, lang string) (string, bool) {
|
|
|
|
if table, ok := stringResources[lang]; ok {
|
|
|
|
if text, ok := table[tag]; ok {
|
|
|
|
return text, true
|
|
|
|
}
|
|
|
|
DebugLogF(`There is no "%s" string in "%s" resources`, tag, lang)
|
|
|
|
}
|
|
|
|
return tag, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.language != "" {
|
|
|
|
if text, ok := getString(tag, session.language); ok {
|
|
|
|
return text, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.languages != nil {
|
|
|
|
for _, lang := range session.languages {
|
|
|
|
if lang != session.language {
|
|
|
|
if text, ok := getString(tag, lang); ok {
|
|
|
|
return text, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tag, false
|
|
|
|
}
|