refactor: Update code to support translation attributes and nodes

This commit is contained in:
Max 2024-06-28 16:20:57 +08:00
parent 671bba4aa4
commit 7db3940914
3 changed files with 49 additions and 9 deletions

View file

@ -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")
}
}

View file

@ -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

View file

@ -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 {