diff --git a/sui/core/injections.go b/sui/core/injections.go
index 8cd804cd..d2e72aea 100644
--- a/sui/core/injections.go
+++ b/sui/core/injections.go
@@ -70,8 +70,16 @@ const initScriptTmpl = `
`
const i118nScriptTmpl = `
- function __m(message) {
- return message;
+ let __sui_locale = {};
+ try {
+ __sui_locale = %s;
+ } catch (e) { __sui_locale = {} }
+
+ function __m(message, fmt) {
+ if (fmt && typeof fmt === "function") {
+ return fmt(message, __sui_locale);
+ }
+ return __sui_locale[message] || message;
}
`
@@ -83,6 +91,6 @@ func bodyInjectionScript(jsonRaw string, debug bool) string {
return fmt.Sprintf(``, jsonRaw, jsPrintData)
}
-func headInjectionScript() string {
- return fmt.Sprintf(``)
+func headInjectionScript(jsonRaw string) string {
+ return fmt.Sprintf(``, jsonRaw)
}
diff --git a/sui/core/locale.go b/sui/core/locale.go
index 7ded38a3..b27442fa 100644
--- a/sui/core/locale.go
+++ b/sui/core/locale.go
@@ -142,6 +142,13 @@ func (locale *Locale) MergeTranslations(translations []Translation, prefix ...st
message = locale.Messages[message]
}
locale.Keys[t.Key] = message
+
+ // Script messages
+ if t.Type == "script" {
+ locale.ScriptMessages[t.Message] = locale.Keys[t.Key]
+ continue
+ }
+
msg, has := locale.Messages[t.Message]
if has && msg != t.Message {
continue
diff --git a/sui/core/parser.go b/sui/core/parser.go
index 860f6504..17fa3c9e 100644
--- a/sui/core/parser.go
+++ b/sui/core/parser.go
@@ -122,7 +122,18 @@ func (parser *TemplateParser) Render(html string) (string, error) {
// Append the head
head := doc.Find("head")
if head.Length() > 0 {
- head.AppendHtml(headInjectionScript())
+
+ scriptMessages := map[string]string{}
+ if parser.locale != nil && parser.locale.ScriptMessages != nil {
+ scriptMessages = parser.locale.ScriptMessages
+ }
+
+ data, err := jsoniter.MarshalToString(scriptMessages)
+ if err != nil {
+ data = "{}"
+ }
+
+ head.AppendHtml(headInjectionScript(data))
parser.addScripts(head, parser.scripts)
parser.addStyles(head, parser.styles)
}
diff --git a/sui/core/types.go b/sui/core/types.go
index aa329033..c93c2028 100644
--- a/sui/core/types.go
+++ b/sui/core/types.go
@@ -97,12 +97,13 @@ type Translation struct {
// Locale is the struct for the locale
type Locale struct {
- Name string `json:"name,omitempty"`
- Formatter string `json:"formatter,omitempty"`
- Keys map[string]string `json:"keys,omitempty"`
- Messages map[string]string `json:"messages,omitempty"`
- Direction string `json:"direction,omitempty"`
- Timezone string `json:"timezone,omitempty"`
+ Name string `json:"name,omitempty" yaml:"name,omitempty"`
+ Formatter string `json:"formatter,omitempty" yaml:"formatter,omitempty"`
+ Keys map[string]string `json:"keys,omitempty" yaml:"keys,omitempty"`
+ Messages map[string]string `json:"messages,omitempty" yaml:"messages,omitempty"`
+ ScriptMessages map[string]string `json:"script_messages,omitempty" yaml:"script_messages,omitempty"`
+ Direction string `json:"direction,omitempty" yaml:"direction,omitempty"`
+ Timezone string `json:"timezone,omitempty" yaml:"timezone,omitempty"`
}
// PageTreeNode is the struct for the page tree node
diff --git a/sui/storages/local/build.go b/sui/storages/local/build.go
index c4e9f481..30978f45 100644
--- a/sui/storages/local/build.go
+++ b/sui/storages/local/build.go
@@ -263,12 +263,13 @@ func (tmpl *Template) getLocale(name string, route string, pageOnly ...bool) cor
}
locale := core.Locale{
- Name: name,
- Keys: map[string]string{},
- Messages: map[string]string{},
- Direction: global.Direction,
- Timezone: global.Timezone,
- Formatter: global.Formatter,
+ Name: name,
+ Keys: map[string]string{},
+ Messages: map[string]string{},
+ ScriptMessages: map[string]string{},
+ Direction: global.Direction,
+ Timezone: global.Timezone,
+ Formatter: global.Formatter,
}
raw, err := tmpl.local.fs.ReadFile(file)