Skip to content

Commit

Permalink
Merge pull request #695 from trheyi/main
Browse files Browse the repository at this point in the history
[feat] SUI i18n client-side script support
  • Loading branch information
trheyi authored Jul 17, 2024
2 parents f1d1a3e + 57b59d6 commit 5c25859
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
16 changes: 12 additions & 4 deletions sui/core/injections.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
`

Expand All @@ -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)
}
11 changes: 11 additions & 0 deletions sui/core/locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func (locale *Locale) MergeTranslations(translations []Translation, prefix ...st
locale.Messages = map[string]string{}
}

if locale.ScriptMessages == nil {
locale.ScriptMessages = map[string]string{}
}

var reg *regexp.Regexp = nil
if len(prefix) > 0 && prefix[0] != "" {
reg = regexp.MustCompile(fmt.Sprintf(`^%s_([0-9]+)$`, prefix[0]))
Expand All @@ -142,6 +146,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
Expand Down
13 changes: 12 additions & 1 deletion sui/core/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
13 changes: 7 additions & 6 deletions sui/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions sui/storages/local/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5c25859

Please sign in to comment.