Merge pull request #695 from trheyi/main
[feat] SUI i18n client-side script support
This commit is contained in:
commit
5c25859f64
5 changed files with 49 additions and 17 deletions
|
|
@ -70,8 +70,16 @@ const initScriptTmpl = `
|
||||||
`
|
`
|
||||||
|
|
||||||
const i118nScriptTmpl = `
|
const i118nScriptTmpl = `
|
||||||
function __m(message) {
|
let __sui_locale = {};
|
||||||
return message;
|
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)
|
return fmt.Sprintf(`<script type="text/javascript">`+initScriptTmpl+`</script>`, jsonRaw, jsPrintData)
|
||||||
}
|
}
|
||||||
|
|
||||||
func headInjectionScript() string {
|
func headInjectionScript(jsonRaw string) string {
|
||||||
return fmt.Sprintf(`<script type="text/javascript">` + i118nScriptTmpl + `</script>`)
|
return fmt.Sprintf(`<script type="text/javascript">`+i118nScriptTmpl+`</script>`, jsonRaw)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -125,6 +125,10 @@ func (locale *Locale) MergeTranslations(translations []Translation, prefix ...st
|
||||||
locale.Messages = map[string]string{}
|
locale.Messages = map[string]string{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if locale.ScriptMessages == nil {
|
||||||
|
locale.ScriptMessages = map[string]string{}
|
||||||
|
}
|
||||||
|
|
||||||
var reg *regexp.Regexp = nil
|
var reg *regexp.Regexp = nil
|
||||||
if len(prefix) > 0 && prefix[0] != "" {
|
if len(prefix) > 0 && prefix[0] != "" {
|
||||||
reg = regexp.MustCompile(fmt.Sprintf(`^%s_([0-9]+)$`, prefix[0]))
|
reg = regexp.MustCompile(fmt.Sprintf(`^%s_([0-9]+)$`, prefix[0]))
|
||||||
|
|
@ -142,6 +146,13 @@ func (locale *Locale) MergeTranslations(translations []Translation, prefix ...st
|
||||||
message = locale.Messages[message]
|
message = locale.Messages[message]
|
||||||
}
|
}
|
||||||
locale.Keys[t.Key] = 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]
|
msg, has := locale.Messages[t.Message]
|
||||||
if has && msg != t.Message {
|
if has && msg != t.Message {
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
|
|
@ -122,7 +122,18 @@ func (parser *TemplateParser) Render(html string) (string, error) {
|
||||||
// Append the head
|
// Append the head
|
||||||
head := doc.Find("head")
|
head := doc.Find("head")
|
||||||
if head.Length() > 0 {
|
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.addScripts(head, parser.scripts)
|
||||||
parser.addStyles(head, parser.styles)
|
parser.addStyles(head, parser.styles)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -97,12 +97,13 @@ type Translation struct {
|
||||||
|
|
||||||
// Locale is the struct for the locale
|
// Locale is the struct for the locale
|
||||||
type Locale struct {
|
type Locale struct {
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty" yaml:"name,omitempty"`
|
||||||
Formatter string `json:"formatter,omitempty"`
|
Formatter string `json:"formatter,omitempty" yaml:"formatter,omitempty"`
|
||||||
Keys map[string]string `json:"keys,omitempty"`
|
Keys map[string]string `json:"keys,omitempty" yaml:"keys,omitempty"`
|
||||||
Messages map[string]string `json:"messages,omitempty"`
|
Messages map[string]string `json:"messages,omitempty" yaml:"messages,omitempty"`
|
||||||
Direction string `json:"direction,omitempty"`
|
ScriptMessages map[string]string `json:"script_messages,omitempty" yaml:"script_messages,omitempty"`
|
||||||
Timezone string `json:"timezone,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
|
// 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{
|
locale := core.Locale{
|
||||||
Name: name,
|
Name: name,
|
||||||
Keys: map[string]string{},
|
Keys: map[string]string{},
|
||||||
Messages: map[string]string{},
|
Messages: map[string]string{},
|
||||||
Direction: global.Direction,
|
ScriptMessages: map[string]string{},
|
||||||
Timezone: global.Timezone,
|
Direction: global.Direction,
|
||||||
Formatter: global.Formatter,
|
Timezone: global.Timezone,
|
||||||
|
Formatter: global.Formatter,
|
||||||
}
|
}
|
||||||
|
|
||||||
raw, err := tmpl.local.fs.ReadFile(file)
|
raw, err := tmpl.local.fs.ReadFile(file)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue