diff --git a/sui/core/parser.go b/sui/core/parser.go index 6baf0ad0..edf4c53c 100644 --- a/sui/core/parser.go +++ b/sui/core/parser.go @@ -277,10 +277,9 @@ func (parser *TemplateParser) parseNode(node *html.Node) { func (parser *TemplateParser) parseElementNode(sel *goquery.Selection) { - node := sel.Get(0) + parser.transElementNode(sel) // Translations - // Translations - parser.transElementNode(sel) + node := sel.Get(0) if _, exist := sel.Attr("s:for"); exist { parser.forStatementNode(sel) @@ -303,21 +302,35 @@ func (parser *TemplateParser) parseElementNode(sel *goquery.Selection) { parser.parseElementAttrs(sel) } -func (parser *TemplateParser) transElementNode(sel *goquery.Selection) { - if parser.locale != nil { - if v, exists := sel.Attr("s:trans-node"); exists { - parser.transNode(v, sel) - } +func (parser *TemplateParser) transTextNode(node *html.Node) { + + parentSel := goquery.NewDocumentFromNode(node.Parent).Selection + text := strings.TrimSpace(node.Data) + if text == "" { + return + } + + // Translate the node + if key, exists := parentSel.Attr("s:trans-node"); exists { + text = parser.transNode(key, text) } // Escape the text - if _, exists := sel.Attr("s:trans-escape"); exists { - html, _ := sel.Html() - html = parser.escapeText(html) - sel.SetHtml(html) + if _, exists := parentSel.Attr("s:trans-escape"); exists { + text = parser.escapeText(text) } - // Translate the attributes + // Translate the text + if v, exists := parentSel.Attr("s:trans-text"); exists { + keys := strings.Split(v, ",") + text = parser.transText(text, keys) + } + + node.Data = text +} + +func (parser *TemplateParser) transElementNode(sel *goquery.Selection) { + for _, attr := range sel.Nodes[0].Attr { if strings.HasPrefix(attr.Key, "s:trans-attr-") { keys := strings.Split(attr.Val, ",") @@ -330,38 +343,6 @@ func (parser *TemplateParser) transElementNode(sel *goquery.Selection) { sel.SetAttr(name, newValue) } } - - // Translate the text - if v, exists := sel.Attr("s:trans-text"); exists { - keys := strings.Split(v, ",") - content := strings.TrimSpace(sel.Text()) - if content == "" { - return - } - content = parser.transText(content, keys) - sel.SetText(content) - } -} - -func (parser *TemplateParser) transNode(key string, sel *goquery.Selection) { - - content, _ := sel.Html() - message := strings.TrimSpace(sel.Text()) - if message == "" { - return - } - - if lcMessage, has := parser.locale.Keys[key]; has && lcMessage != message { - content = strings.Replace(content, message, lcMessage, 1) - sel.SetHtml(content) - return - } - - if lcMessage, has := parser.locale.Messages[message]; has { - content = strings.Replace(content, message, lcMessage, 1) - sel.SetHtml(content) - return - } } // 替换转义字符 @@ -395,6 +376,23 @@ func (parser *TemplateParser) escape(value string) string { return value } +func (parser *TemplateParser) transNode(key string, message string) string { + + if parser.locale == nil { + return message + } + + if lcMessage, has := parser.locale.Keys[key]; has && lcMessage != message { + return lcMessage + } + + if lcMessage, has := parser.locale.Messages[message]; has { + return lcMessage + } + + return message +} + func (parser *TemplateParser) transText(content string, keys []string) string { matches := stmtRe.FindAllStringSubmatch(content, -1) @@ -522,6 +520,7 @@ func checkIsRawElement(node *html.Node) bool { return false } func (parser *TemplateParser) parseTextNode(node *html.Node) { + parser.transTextNode(node) // Translations parser.sequence = parser.sequence + 1 res, hasStmt := parser.data.Replace(node.Data) // Bind the variable to the parent node