Merge pull request #685 from trheyi/main
feat: SUI i18n supports both attributes and variables.
This commit is contained in:
commit
80d28649e9
1 changed files with 74 additions and 10 deletions
|
|
@ -130,7 +130,7 @@ func (parser *TemplateParser) Locale() *Locale {
|
||||||
}
|
}
|
||||||
|
|
||||||
root := parser.option.Root
|
root := parser.option.Root
|
||||||
route := strings.TrimPrefix(parser.option.Route, root)
|
route := parser.option.Route
|
||||||
disableCache := parser.option.Preview || parser.option.Debug || parser.option.Editor || parser.option.DisableCache
|
disableCache := parser.option.Preview || parser.option.Debug || parser.option.Editor || parser.option.DisableCache
|
||||||
locales, ok = Locales[name]
|
locales, ok = Locales[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
@ -142,7 +142,7 @@ func (parser *TemplateParser) Locale() *Locale {
|
||||||
return locale
|
return locale
|
||||||
}
|
}
|
||||||
|
|
||||||
path := filepath.Join("public", parser.option.Root, ".locales", name, route+".yml")
|
path := filepath.Join("public", parser.option.Root, ".locales", name, strings.TrimPrefix(route, root)+".yml")
|
||||||
if exists, err := application.App.Exists(path); !exists {
|
if exists, err := application.App.Exists(path); !exists {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[parser] %s Locale %s", route, err.Error())
|
log.Error("[parser] %s Locale %s", route, err.Error())
|
||||||
|
|
@ -279,6 +279,9 @@ func (parser *TemplateParser) parseElementNode(sel *goquery.Selection) {
|
||||||
|
|
||||||
node := sel.Get(0)
|
node := sel.Get(0)
|
||||||
|
|
||||||
|
// Translations
|
||||||
|
parser.transElementNode(sel)
|
||||||
|
|
||||||
if _, exist := sel.Attr("s:for"); exist {
|
if _, exist := sel.Attr("s:for"); exist {
|
||||||
parser.forStatementNode(sel)
|
parser.forStatementNode(sel)
|
||||||
}
|
}
|
||||||
|
|
@ -298,21 +301,43 @@ func (parser *TemplateParser) parseElementNode(sel *goquery.Selection) {
|
||||||
|
|
||||||
// Parse the attributes
|
// Parse the attributes
|
||||||
parser.parseElementAttrs(sel)
|
parser.parseElementAttrs(sel)
|
||||||
|
|
||||||
// Translations
|
|
||||||
parser.transElementNode(sel)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *TemplateParser) transElementNode(sel *goquery.Selection) {
|
func (parser *TemplateParser) transElementNode(sel *goquery.Selection) {
|
||||||
if parser.locale == nil {
|
if parser.locale != nil {
|
||||||
return
|
if v, exists := sel.Attr("s:trans-node"); exists {
|
||||||
|
parser.transNode(v, sel)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
key, exist := sel.Attr("s:trans-node")
|
// Transalte the attributes
|
||||||
if !exist {
|
for _, attr := range sel.Nodes[0].Attr {
|
||||||
return
|
if strings.HasPrefix(attr.Key, "s:trans-attr-") {
|
||||||
|
keys := strings.Split(attr.Val, ",")
|
||||||
|
name := strings.TrimPrefix(attr.Key, "s:trans-attr-")
|
||||||
|
value := sel.AttrOr(name, "")
|
||||||
|
if value == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newValue := parser.transText(value, keys)
|
||||||
|
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) {
|
||||||
|
|
||||||
text := sel.Text()
|
text := sel.Text()
|
||||||
message := strings.TrimSpace(text)
|
message := strings.TrimSpace(text)
|
||||||
if message == "" {
|
if message == "" {
|
||||||
|
|
@ -330,6 +355,45 @@ func (parser *TemplateParser) transElementNode(sel *goquery.Selection) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (parser *TemplateParser) transText(content string, keys []string) string {
|
||||||
|
|
||||||
|
matches := stmtRe.FindAllStringSubmatch(content, -1)
|
||||||
|
newContent := content
|
||||||
|
for _, match := range matches {
|
||||||
|
text := strings.TrimSpace(match[1])
|
||||||
|
transMatches := transStmtReSingle.FindAllStringSubmatch(text, -1)
|
||||||
|
if len(transMatches) == 0 {
|
||||||
|
transMatches = transStmtReDouble.FindAllStringSubmatch(text, -1)
|
||||||
|
}
|
||||||
|
if len(transMatches) > len(keys) {
|
||||||
|
return content
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, transMatch := range transMatches {
|
||||||
|
message := strings.TrimSpace(transMatch[1])
|
||||||
|
if parser.locale == nil {
|
||||||
|
newContent = strings.Replace(newContent, "::"+message, message, 1)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
key := keys[i]
|
||||||
|
if lcMessage, has := parser.locale.Keys[key]; has && lcMessage != message {
|
||||||
|
newContent = strings.Replace(newContent, "::"+message, lcMessage, 1)
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if lcMessage, has := parser.locale.Messages[message]; has {
|
||||||
|
newContent = strings.Replace(newContent, "::"+message, lcMessage, 1)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
newContent = strings.Replace(newContent, "::"+message, message, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newContent
|
||||||
|
}
|
||||||
|
|
||||||
// Remove the tag and replace it with the children
|
// Remove the tag and replace it with the children
|
||||||
func (parser *TemplateParser) removeWrapper(sel *goquery.Selection) {
|
func (parser *TemplateParser) removeWrapper(sel *goquery.Selection) {
|
||||||
children := sel.Children()
|
children := sel.Children()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue