fix: Escape translation variables in SUI page parser

This commit is contained in:
Max 2024-07-16 10:43:43 +08:00
parent 2966256ad6
commit 55e3a2b4dd
2 changed files with 16 additions and 6 deletions

View file

@ -669,7 +669,7 @@ func (page *Page) translateNode(node *html.Node, sequence *int) ([]Translation,
parentSel.SetAttr("s:trans-node", key) parentSel.SetAttr("s:trans-node", key)
*sequence = *sequence + 1 *sequence = *sequence + 1
} }
parentSel.RemoveAttr("s:trans") parentSel.SetAttr("s:trans-escape", "true")
} }
if _, has := parentSel.Attr("s:trans-text"); has { if _, has := parentSel.Attr("s:trans-text"); has {
@ -682,7 +682,6 @@ func (page *Page) translateNode(node *html.Node, sequence *int) ([]Translation,
if len(keys) > 0 { if len(keys) > 0 {
raw := strings.Join(keys, ",") raw := strings.Join(keys, ",")
parentSel.SetAttr("s:trans-text", raw) parentSel.SetAttr("s:trans-text", raw)
parentSel.RemoveAttr("s:trans")
translations = append(translations, trans...) translations = append(translations, trans...)
} }
break break

View file

@ -311,9 +311,10 @@ func (parser *TemplateParser) transElementNode(sel *goquery.Selection) {
} }
// Escape the text // Escape the text
if _, exists := sel.Attr("s:trans-node"); exists { if _, exists := sel.Attr("s:trans-escape"); exists {
content := parser.escapeText(strings.TrimSpace(sel.Text())) html, _ := sel.Html()
sel.SetText(content) html = parser.escapeText(html)
sel.SetHtml(html)
} }
// Translate the attributes // Translate the attributes
@ -378,9 +379,19 @@ func (parser *TemplateParser) escape(value string) string {
if strings.HasPrefix(value, "':::") { if strings.HasPrefix(value, "':::") {
return "'::" + strings.TrimPrefix(value, "':::") return "'::" + strings.TrimPrefix(value, "':::")
} }
if strings.HasPrefix(value, "':::") {
return "'::" + strings.TrimPrefix(value, "':::")
}
if strings.HasPrefix(value, "\":::") { if strings.HasPrefix(value, "\":::") {
return "\"::" + strings.TrimPrefix(value, "\":::") return "\"::" + strings.TrimPrefix(value, "\":::")
} }
if strings.HasPrefix(value, "":::") {
return ""::" + strings.TrimPrefix(value, "":::")
}
return value return value
} }
@ -390,7 +401,7 @@ func (parser *TemplateParser) transText(content string, keys []string) string {
newContent := content newContent := content
for _, match := range matches { for _, match := range matches {
text := strings.TrimSpace(match[1]) text := strings.TrimSpace(match[1])
if strings.HasPrefix(text, "':::") || strings.HasPrefix(text, "\":::") { if strings.HasPrefix(text, "':::") || strings.HasPrefix(text, "\":::") || strings.HasPrefix(text, "':::") || strings.HasPrefix(text, "":::") {
escaped := parser.escape(text) escaped := parser.escape(text)
newContent = strings.Replace(newContent, text, escaped, 1) newContent = strings.Replace(newContent, text, escaped, 1)
continue continue