Merge pull request #688 from trheyi/main
sui page parser to escape i18n variables
This commit is contained in:
commit
1510f3ce34
1 changed files with 40 additions and 4 deletions
|
|
@ -310,6 +310,12 @@ func (parser *TemplateParser) transElementNode(sel *goquery.Selection) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Escape the text
|
||||||
|
if _, exists := sel.Attr("s:trans-node"); exists {
|
||||||
|
content := parser.escapeText(strings.TrimSpace(sel.Text()))
|
||||||
|
sel.SetText(content)
|
||||||
|
}
|
||||||
|
|
||||||
// Translate the attributes
|
// Translate the attributes
|
||||||
for _, attr := range sel.Nodes[0].Attr {
|
for _, attr := range sel.Nodes[0].Attr {
|
||||||
if strings.HasPrefix(attr.Key, "s:trans-attr-") {
|
if strings.HasPrefix(attr.Key, "s:trans-attr-") {
|
||||||
|
|
@ -338,29 +344,58 @@ func (parser *TemplateParser) transElementNode(sel *goquery.Selection) {
|
||||||
|
|
||||||
func (parser *TemplateParser) transNode(key string, sel *goquery.Selection) {
|
func (parser *TemplateParser) transNode(key string, sel *goquery.Selection) {
|
||||||
|
|
||||||
text := sel.Text()
|
content := sel.Text()
|
||||||
message := strings.TrimSpace(text)
|
message := strings.TrimSpace(content)
|
||||||
if message == "" {
|
if message == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if lcMessage, has := parser.locale.Keys[key]; has && lcMessage != message {
|
if lcMessage, has := parser.locale.Keys[key]; has && lcMessage != message {
|
||||||
sel.SetText(strings.Replace(text, message, lcMessage, 1))
|
content = strings.Replace(content, message, lcMessage, 1)
|
||||||
|
sel.SetText(content)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if lcMessage, has := parser.locale.Messages[message]; has {
|
if lcMessage, has := parser.locale.Messages[message]; has {
|
||||||
sel.SetText(strings.Replace(text, message, lcMessage, 1))
|
content = strings.Replace(content, message, lcMessage, 1)
|
||||||
|
sel.SetText(content)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 替换转义字符
|
||||||
|
func (parser *TemplateParser) escapeText(content string) string {
|
||||||
|
matches := stmtRe.FindAllStringSubmatch(content, -1)
|
||||||
|
newContent := content
|
||||||
|
for _, match := range matches {
|
||||||
|
text := strings.TrimSpace(match[1])
|
||||||
|
newContent = strings.Replace(newContent, text, parser.escape(text), 1)
|
||||||
|
}
|
||||||
|
return newContent
|
||||||
|
}
|
||||||
|
|
||||||
|
func (parser *TemplateParser) escape(value string) string {
|
||||||
|
if strings.HasPrefix(value, "':::") {
|
||||||
|
return "'::" + strings.TrimPrefix(value, "':::")
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(value, "\":::") {
|
||||||
|
return "\"::" + strings.TrimPrefix(value, "\":::")
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
func (parser *TemplateParser) transText(content string, keys []string) string {
|
func (parser *TemplateParser) transText(content string, keys []string) string {
|
||||||
|
|
||||||
matches := stmtRe.FindAllStringSubmatch(content, -1)
|
matches := stmtRe.FindAllStringSubmatch(content, -1)
|
||||||
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, "\":::") {
|
||||||
|
escaped := parser.escape(text)
|
||||||
|
newContent = strings.Replace(newContent, text, escaped, 1)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
transMatches := transStmtReSingle.FindAllStringSubmatch(text, -1)
|
transMatches := transStmtReSingle.FindAllStringSubmatch(text, -1)
|
||||||
if len(transMatches) == 0 {
|
if len(transMatches) == 0 {
|
||||||
transMatches = transStmtReDouble.FindAllStringSubmatch(text, -1)
|
transMatches = transStmtReDouble.FindAllStringSubmatch(text, -1)
|
||||||
|
|
@ -371,6 +406,7 @@ func (parser *TemplateParser) transText(content string, keys []string) string {
|
||||||
|
|
||||||
for i, transMatch := range transMatches {
|
for i, transMatch := range transMatches {
|
||||||
message := strings.TrimSpace(transMatch[1])
|
message := strings.TrimSpace(transMatch[1])
|
||||||
|
|
||||||
if parser.locale == nil {
|
if parser.locale == nil {
|
||||||
newContent = strings.Replace(newContent, "::"+message, message, 1)
|
newContent = strings.Replace(newContent, "::"+message, message, 1)
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue