Update asset metadata timestamps and enhance translation handling

- Update timestamps in `bindata.go` for various asset files to reflect recent modifications.
- Modify translation handling in `parser.go` to merge messages from YAML locale files with script messages, ensuring all translation keys are available at runtime.
- Refactor `writeLocaleFiles` in `page.go` to accept a build context, allowing for better integration of script translations during the build process.
This commit is contained in:
Max 2026-02-12 19:06:56 +08:00
parent 62a4723c71
commit d598394337
5 changed files with 370 additions and 343 deletions

File diff suppressed because it is too large Load diff

View file

@ -15,7 +15,7 @@ var slotRe = regexp.MustCompile(`\[\{([^\}]+)\}\]`)
var cssRe = regexp.MustCompile(`([\.a-z0-9A-Z-:# ]+)\{`)
var transStmtReSingle = regexp.MustCompile(`'::([^:']+)'`)
var transStmtReDouble = regexp.MustCompile(`"::([^:"]+)"`)
var transFuncRe = regexp.MustCompile(`__m\s*\(\s*["'](.*?)["']\s*\)`)
var transFuncRe = regexp.MustCompile(`(?:__m|(?:^|[^a-zA-Z0-9_.$])T)\s*\(\s*["'](.*?)["']\s*\)`)
// Build build the page
func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Document, []string, error) {

View file

@ -85,6 +85,8 @@ const i118nScriptTmpl = `
}
return __sui_locale[message] || message;
}
var T = __m;
`
const pageEventScriptTmpl = `

View file

@ -140,12 +140,23 @@ func (parser *TemplateParser) Render(html string) (string, error) {
head := doc.Find("head")
if head.Length() > 0 {
scriptMessages := map[string]string{}
if parser.locale != nil && parser.locale.ScriptMessages != nil {
scriptMessages = parser.locale.ScriptMessages
// Merge Messages and ScriptMessages so that __sui_locale (and thus
// __m / T) can resolve all translation keys at runtime — not just those
// extracted from __m("literal") calls during build. Messages written in
// the YAML locale files are now also available to JS code.
allMessages := map[string]string{}
if parser.locale != nil {
// Messages first (from YAML locale files, used for server-side rendering)
for k, v := range parser.locale.Messages {
allMessages[k] = v
}
// ScriptMessages override (extracted from __m() calls during build)
for k, v := range parser.locale.ScriptMessages {
allMessages[k] = v
}
}
data, err := jsoniter.MarshalToString(scriptMessages)
data, err := jsoniter.MarshalToString(allMessages)
if err != nil {
data = "{}"
}

View file

@ -317,7 +317,7 @@ func (page *Page) Build(globalCtx *core.GlobalBuildContext, option *core.BuildOp
}
// Write locale files from page's __locales directory
err = page.writeLocaleFiles(option.Data)
err = page.writeLocaleFiles(ctx, option.Data)
if err != nil {
log.Warn("[Agent] Write locale files error: %s", err.Error())
// Don't fail the build for locale errors
@ -474,8 +474,9 @@ func (page *Page) AssistantID() string {
return page.assistantID
}
// writeLocaleFiles writes locale files from page's __locales directory to public
func (page *Page) writeLocaleFiles(data map[string]interface{}) error {
// writeLocaleFiles writes locale files from page's __locales directory to public,
// merging script translations extracted from __m() calls during build.
func (page *Page) writeLocaleFiles(ctx *core.BuildContext, data map[string]interface{}) error {
fs := page.tmpl.agent.fs
// Check if page has __locales directory
@ -484,6 +485,13 @@ func (page *Page) writeLocaleFiles(data map[string]interface{}) error {
return nil
}
// Get translations from build context (includes __m() calls marked as type "script")
var translations []core.Translation
if ctx != nil {
translations = ctx.GetTranslations()
}
prefix := core.TranslationKeyPrefix(page.Route)
// Get the public root
root, err := page.tmpl.agent.DSL.PublicRoot(data)
if err != nil {
@ -544,7 +552,7 @@ func (page *Page) writeLocaleFiles(data map[string]interface{}) error {
}
}
// Extract script_messages
// Extract script_messages (if manually specified in source locale)
if scriptMessages, ok := localeData["script_messages"].(map[string]interface{}); ok {
for k, v := range scriptMessages {
if strVal, ok := v.(string); ok {
@ -553,6 +561,12 @@ func (page *Page) writeLocaleFiles(data map[string]interface{}) error {
}
}
// Merge translations from build context — this populates ScriptMessages
// from __m() calls found in TS/JS scripts during compilation
if len(translations) > 0 {
locale.MergeTranslations(translations, prefix)
}
// Extract timezone and direction
if tz, ok := localeData["timezone"].(string); ok {
locale.Timezone = tz