[feat] SUI i18n client-side script support
This commit is contained in:
parent
41495a2de0
commit
11a66d0aed
5 changed files with 45 additions and 17 deletions
|
|
@ -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(`<script type="text/javascript">`+initScriptTmpl+`</script>`, jsonRaw, jsPrintData)
|
||||
}
|
||||
|
||||
func headInjectionScript() string {
|
||||
return fmt.Sprintf(`<script type="text/javascript">` + i118nScriptTmpl + `</script>`)
|
||||
func headInjectionScript(jsonRaw string) string {
|
||||
return fmt.Sprintf(`<script type="text/javascript">`+i118nScriptTmpl+`</script>`, jsonRaw)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue