From 7db394091490599baacb3b588877f1da5c8a5f4c Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 28 Jun 2024 16:20:57 +0800 Subject: [PATCH] refactor: Update code to support translation attributes and nodes --- sui/core/build.go | 30 +++++++++++++++++++++++++++++- sui/core/parser.go | 8 +++++++- sui/storages/local/build.go | 20 +++++++++++++------- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/sui/core/build.go b/sui/core/build.go index 76501607..19ea51a7 100644 --- a/sui/core/build.go +++ b/sui/core/build.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/PuerkitoBio/goquery" + "github.com/fatih/color" jsoniter "github.com/json-iterator/go" "github.com/yaoapp/kun/log" "golang.org/x/net/html" @@ -431,47 +432,74 @@ func getNodeTranslation(sel *goquery.Selection, index int, namespace string) []T if typ == "" { typ = "html" } + + key := fmt.Sprintf("%s_index_%d", namespace, index) translations = append(translations, Translation{ - Key: fmt.Sprintf("%s_index_%d", namespace, index), + Key: key, Message: strings.TrimSpace(sel.Text()), Type: typ, }) + sel.SetAttr("s:trans-node", key) + sel.RemoveAttr("s:trans") } // Attributes + keys := map[string][]string{} + has := false for i, attr := range sel.Get(0).Attr { + keys[attr.Key] = []string{} + // value="::attr" if strings.HasPrefix(attr.Val, "::") { + key := fmt.Sprintf("%s_index_attr_%d_%d", namespace, index, i) translations = append(translations, Translation{ Key: fmt.Sprintf("%s_index_attr_%d_%d", namespace, index, i), Message: attr.Val[2:], Name: attr.Key, Type: "attr", }) + keys[attr.Key] = append(keys[attr.Key], key) + has = true } // value="{{ 'key': '::value' }}" matches := langAttrRe.FindAllStringSubmatch(attr.Val, -1) if len(matches) > 0 { for j, match := range matches { + key := fmt.Sprintf("%s_index_attr_%d_%d_%d", namespace, index, i, j) translations = append(translations, Translation{ Key: fmt.Sprintf("%s_index_attr_%d_%d_%d", namespace, index, i, j), Message: match[1], Name: attr.Key, Type: "attr", }) + keys[attr.Key] = append(keys[attr.Key], key) + has = true } } } + if has { + raw, err := jsoniter.Marshal(keys) + if err != nil { + fmt.Println(color.RedString(err.Error())) + break + } + sel.SetAttr("s:trans-attrs", string(raw)) + sel.RemoveAttr("s:trans") + } + case html.TextNode: if strings.HasPrefix(sel.Text(), "::") { + key := fmt.Sprintf("%s_index_%d", namespace, index) translations = append(translations, Translation{ Key: fmt.Sprintf("%s_index_%d", namespace, index), Message: strings.TrimSpace(sel.Text()[2:]), Type: "text", }) + sel.SetAttr("s:trans-node", key) + sel.RemoveAttr("s:trans") } } diff --git a/sui/core/parser.go b/sui/core/parser.go index dd9293bb..94f47942 100644 --- a/sui/core/parser.go +++ b/sui/core/parser.go @@ -285,7 +285,8 @@ func (parser *TemplateParser) transElementNode(sel *goquery.Selection) { return } - if _, exist := sel.Attr("s:trans"); !exist { + key, exist := sel.Attr("s:trans-node") + if !exist { return } @@ -295,6 +296,11 @@ func (parser *TemplateParser) transElementNode(sel *goquery.Selection) { return } + if lcMessage, has := parser.locale.Keys[key]; has && lcMessage != message { + sel.SetText(strings.Replace(text, message, lcMessage, 1)) + return + } + if lcMessage, has := parser.locale.Messages[message]; has { sel.SetText(strings.Replace(text, message, lcMessage, 1)) return diff --git a/sui/storages/local/build.go b/sui/storages/local/build.go index 9b910f5b..bece2152 100644 --- a/sui/storages/local/build.go +++ b/sui/storages/local/build.go @@ -247,20 +247,26 @@ func (page *Page) writeLocaleFiles(data map[string]interface{}) error { return nil } - keys := map[string]string{} - messages := map[string]string{} - for _, t := range page.Page.Translations { - keys[t.Key] = t.Message - messages[t.Message] = t.Message - } - files := page.localeFiles(data) for name, file := range files { + + // Init Data + keys := map[string]string{} + messages := map[string]string{} + for _, t := range page.Page.Translations { + keys[t.Key] = t.Message + messages[t.Message] = t.Message + } + locale := page.locale(name) for key := range keys { if _, has := locale.Keys[key]; has { keys[key] = locale.Keys[key] } + + if msgValue, has := locale.Messages[keys[key]]; has { + keys[key] = msgValue + } } for message := range messages {