Merge pull request #633 from trheyi/main

refactor: Update code to support translation attributes and nodes
This commit is contained in:
Max 2024-06-28 16:21:33 +08:00 committed by GitHub
commit abc172587b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 49 additions and 9 deletions

View file

@ -8,6 +8,7 @@ import (
"strings" "strings"
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
"github.com/fatih/color"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/kun/log" "github.com/yaoapp/kun/log"
"golang.org/x/net/html" "golang.org/x/net/html"
@ -431,47 +432,74 @@ func getNodeTranslation(sel *goquery.Selection, index int, namespace string) []T
if typ == "" { if typ == "" {
typ = "html" typ = "html"
} }
key := fmt.Sprintf("%s_index_%d", namespace, index)
translations = append(translations, Translation{ translations = append(translations, Translation{
Key: fmt.Sprintf("%s_index_%d", namespace, index), Key: key,
Message: strings.TrimSpace(sel.Text()), Message: strings.TrimSpace(sel.Text()),
Type: typ, Type: typ,
}) })
sel.SetAttr("s:trans-node", key)
sel.RemoveAttr("s:trans")
} }
// Attributes // Attributes
keys := map[string][]string{}
has := false
for i, attr := range sel.Get(0).Attr { for i, attr := range sel.Get(0).Attr {
keys[attr.Key] = []string{}
// value="::attr" // value="::attr"
if strings.HasPrefix(attr.Val, "::") { if strings.HasPrefix(attr.Val, "::") {
key := fmt.Sprintf("%s_index_attr_%d_%d", namespace, index, i)
translations = append(translations, Translation{ translations = append(translations, Translation{
Key: fmt.Sprintf("%s_index_attr_%d_%d", namespace, index, i), Key: fmt.Sprintf("%s_index_attr_%d_%d", namespace, index, i),
Message: attr.Val[2:], Message: attr.Val[2:],
Name: attr.Key, Name: attr.Key,
Type: "attr", Type: "attr",
}) })
keys[attr.Key] = append(keys[attr.Key], key)
has = true
} }
// value="{{ 'key': '::value' }}" // value="{{ 'key': '::value' }}"
matches := langAttrRe.FindAllStringSubmatch(attr.Val, -1) matches := langAttrRe.FindAllStringSubmatch(attr.Val, -1)
if len(matches) > 0 { if len(matches) > 0 {
for j, match := range matches { for j, match := range matches {
key := fmt.Sprintf("%s_index_attr_%d_%d_%d", namespace, index, i, j)
translations = append(translations, Translation{ translations = append(translations, Translation{
Key: fmt.Sprintf("%s_index_attr_%d_%d_%d", namespace, index, i, j), Key: fmt.Sprintf("%s_index_attr_%d_%d_%d", namespace, index, i, j),
Message: match[1], Message: match[1],
Name: attr.Key, Name: attr.Key,
Type: "attr", 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: case html.TextNode:
if strings.HasPrefix(sel.Text(), "::") { if strings.HasPrefix(sel.Text(), "::") {
key := fmt.Sprintf("%s_index_%d", namespace, index)
translations = append(translations, Translation{ translations = append(translations, Translation{
Key: fmt.Sprintf("%s_index_%d", namespace, index), Key: fmt.Sprintf("%s_index_%d", namespace, index),
Message: strings.TrimSpace(sel.Text()[2:]), Message: strings.TrimSpace(sel.Text()[2:]),
Type: "text", 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 return
} }
if _, exist := sel.Attr("s:trans"); !exist { key, exist := sel.Attr("s:trans-node")
if !exist {
return return
} }
@ -295,6 +296,11 @@ func (parser *TemplateParser) transElementNode(sel *goquery.Selection) {
return 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 { if lcMessage, has := parser.locale.Messages[message]; has {
sel.SetText(strings.Replace(text, message, lcMessage, 1)) sel.SetText(strings.Replace(text, message, lcMessage, 1))
return return

View file

@ -247,6 +247,10 @@ func (page *Page) writeLocaleFiles(data map[string]interface{}) error {
return nil return nil
} }
files := page.localeFiles(data)
for name, file := range files {
// Init Data
keys := map[string]string{} keys := map[string]string{}
messages := map[string]string{} messages := map[string]string{}
for _, t := range page.Page.Translations { for _, t := range page.Page.Translations {
@ -254,13 +258,15 @@ func (page *Page) writeLocaleFiles(data map[string]interface{}) error {
messages[t.Message] = t.Message messages[t.Message] = t.Message
} }
files := page.localeFiles(data)
for name, file := range files {
locale := page.locale(name) locale := page.locale(name)
for key := range keys { for key := range keys {
if _, has := locale.Keys[key]; has { if _, has := locale.Keys[key]; has {
keys[key] = locale.Keys[key] keys[key] = locale.Keys[key]
} }
if msgValue, has := locale.Messages[keys[key]]; has {
keys[key] = msgValue
}
} }
for message := range messages { for message := range messages {