diff --git a/api.go b/api.go index d1c382b..ecb47bb 100644 --- a/api.go +++ b/api.go @@ -1085,33 +1085,15 @@ func (app *appContext) GetConfig(gc *gin.Context) { app.info.Println("Config requested") resp := app.configBase // Load language options - loadLangs := func(langs *map[string]map[string]interface{}, settingsKey string) (string, []string) { - langOptions := make([]string, len(*langs)) - chosenLang := app.config.Section("ui").Key("language" + settingsKey).MustString("en-us") - chosenLangName := (*langs)[chosenLang]["meta"].(map[string]interface{})["name"].(string) - i := 0 - for _, lang := range *langs { - langOptions[i] = lang["meta"].(map[string]interface{})["name"].(string) - i++ - } - return chosenLangName, langOptions - } - formChosen, formOptions := loadLangs(&app.storage.lang.Form, "-form") + formChosen, formOptions := app.storage.lang.Form.getOptions(app.config.Section("ui").Key("language-form").MustString("en-us")) fl := resp.Sections["ui"].Settings["language-form"] fl.Options = formOptions fl.Value = formChosen - adminChosen, adminOptions := loadLangs(&app.storage.lang.Admin, "-admin") + adminChosen, adminOptions := app.storage.lang.Admin.getOptions(app.config.Section("ui").Key("language-admin").MustString("en-us")) al := resp.Sections["ui"].Settings["language-admin"] al.Options = adminOptions al.Value = adminChosen - emailOptions := make([]string, len(app.storage.lang.Email)) - chosenLang := app.config.Section("email").Key("language").MustString("en-us") - emailChosen := app.storage.lang.Email.get(chosenLang, "meta", "name") - i := 0 - for langName := range app.storage.lang.Email { - emailOptions[i] = app.storage.lang.Email.get(langName, "meta", "name") - i++ - } + emailChosen, emailOptions := app.storage.lang.Email.getOptions(app.config.Section("email").Key("language").MustString("en-us")) el := resp.Sections["email"].Settings["language"] el.Options = emailOptions el.Value = emailChosen @@ -1136,7 +1118,7 @@ func (app *appContext) GetConfig(gc *gin.Context) { t := resp.Sections["jellyfin"].Settings["type"] opts := make([]string, len(serverTypes)) - i = 0 + i := 0 for _, v := range serverTypes { opts[i] = v i++ @@ -1169,21 +1151,21 @@ func (app *appContext) ModifyConfig(gc *gin.Context) { for setting, value := range settings.(map[string]interface{}) { if section == "ui" && setting == "language-form" { for key, lang := range app.storage.lang.Form { - if lang["meta"].(map[string]interface{})["name"].(string) == value.(string) { + if lang.Meta.Name == value.(string) { tempConfig.Section("ui").Key("language-form").SetValue(key) break } } } else if section == "ui" && setting == "language-admin" { for key, lang := range app.storage.lang.Admin { - if lang["meta"].(map[string]interface{})["name"].(string) == value.(string) { + if lang.Meta.Name == value.(string) { tempConfig.Section("ui").Key("language-admin").SetValue(key) break } } } else if section == "email" && setting == "language" { - for key := range app.storage.lang.Email { - if app.storage.lang.Email.get(key, "meta", "name") == value.(string) { + for key, lang := range app.storage.lang.Email { + if lang.Meta.Name == value.(string) { tempConfig.Section("email").Key("language").SetValue(key) break } @@ -1260,11 +1242,11 @@ func (app *appContext) GetLanguages(gc *gin.Context) { resp := langDTO{} if page == "form" { for key, lang := range app.storage.lang.Form { - resp[key] = lang["meta"].(map[string]interface{})["name"].(string) + resp[key] = lang.Meta.Name } } else if page == "admin" { for key, lang := range app.storage.lang.Admin { - resp[key] = lang["meta"].(map[string]interface{})["name"].(string) + resp[key] = lang.Meta.Name } } if len(resp) == 0 { @@ -1274,6 +1256,19 @@ func (app *appContext) GetLanguages(gc *gin.Context) { gc.JSON(200, resp) } +func (app *appContext) ServeLang(gc *gin.Context) { + page := gc.Param("page") + lang := strings.Replace(gc.Param("file"), ".json", "", 1) + if page == "admin" { + gc.JSON(200, app.storage.lang.Admin[lang]) + return + } else if page == "form" { + gc.JSON(200, app.storage.lang.Form[lang]) + return + } + respondBool(400, false, gc) +} + // func Restart() error { // defer func() { // if r := recover(); r != nil { diff --git a/email.go b/email.go index 27b411e..505bcf6 100644 --- a/email.go +++ b/email.go @@ -73,8 +73,7 @@ func (sm *SMTP) send(address, fromName, fromAddr string, email *Email) error { // Emailer contains the email sender, email content, and methods to construct message content. type Emailer struct { fromAddr, fromName string - lang *EmailLang - cLang string + lang emailLang sender emailClient } @@ -110,8 +109,7 @@ func NewEmailer(app *appContext) *Emailer { emailer := &Emailer{ fromAddr: app.config.Section("email").Key("address").String(), fromName: app.config.Section("email").Key("from").String(), - lang: &(app.storage.lang.Email), - cLang: app.storage.lang.chosenEmailLang, + lang: app.storage.lang.Email[app.storage.lang.chosenEmailLang], } method := app.config.Section("email").Key("method").String() if method == "smtp" { @@ -137,7 +135,7 @@ func (emailer *Emailer) NewMailgun(url, key string) { sender := &Mailgun{ client: mailgun.NewMailgun(strings.Split(emailer.fromAddr, "@")[1], key), } - // Mailgun client takes the base url, so we need to trim off the end (e.g 'v3/messages' + // Mailgun client takes the base url, so we need to trim off the end (e.g 'v3/messages') if strings.Contains(url, "messages") { url = url[0:strings.LastIndex(url, "/")] url = url[0:strings.LastIndex(url, "/")] @@ -157,9 +155,8 @@ func (emailer *Emailer) NewSMTP(server string, port int, username, password stri } func (emailer *Emailer) constructInvite(code string, invite Invite, app *appContext) (*Email, error) { - lang := emailer.cLang email := &Email{ - subject: app.config.Section("invite_emails").Key("subject").MustString(emailer.lang.get(lang, "inviteEmail", "title")), + subject: app.config.Section("invite_emails").Key("subject").MustString(emailer.lang.InviteEmail.get("title")), } expiry := invite.ValidTill d, t, expiresIn := emailer.formatExpiry(expiry, false, app.datePattern, app.timePattern) @@ -175,11 +172,11 @@ func (emailer *Emailer) constructInvite(code string, invite Invite, app *appCont } var tplData bytes.Buffer err = tpl.Execute(&tplData, map[string]string{ - "hello": emailer.lang.get(lang, "inviteEmail", "hello"), - "youHaveBeenInvited": emailer.lang.get(lang, "inviteEmail", "youHaveBeenInvited"), - "toJoin": emailer.lang.get(lang, "inviteEmail", "toJoin"), - "inviteExpiry": emailer.lang.format(lang, "inviteEmail", "inviteExpiry", d, t, expiresIn), - "linkButton": emailer.lang.get(lang, "inviteEmail", "linkButton"), + "hello": emailer.lang.InviteEmail.get("hello"), + "youHaveBeenInvited": emailer.lang.InviteEmail.get("youHaveBeenInvited"), + "toJoin": emailer.lang.InviteEmail.get("toJoin"), + "inviteExpiry": emailer.lang.InviteEmail.format("inviteExpiry", d, t, expiresIn), + "linkButton": emailer.lang.InviteEmail.get("linkButton"), "invite_link": inviteLink, "message": message, }) @@ -196,9 +193,8 @@ func (emailer *Emailer) constructInvite(code string, invite Invite, app *appCont } func (emailer *Emailer) constructExpiry(code string, invite Invite, app *appContext) (*Email, error) { - lang := emailer.cLang email := &Email{ - subject: emailer.lang.get(lang, "inviteExpiry", "title"), + subject: emailer.lang.InviteExpiry.get("title"), } expiry := app.formatDatetime(invite.ValidTill) for _, key := range []string{"html", "text"} { @@ -209,9 +205,9 @@ func (emailer *Emailer) constructExpiry(code string, invite Invite, app *appCont } var tplData bytes.Buffer err = tpl.Execute(&tplData, map[string]string{ - "inviteExpired": emailer.lang.get(lang, "inviteExpiry", "inviteExpired"), - "expiredAt": emailer.lang.format(lang, "inviteExpiry", "expiredAt", "\""+code+"\"", expiry), - "notificationNotice": emailer.lang.get(lang, "inviteExpiry", "notificationNotice"), + "inviteExpired": emailer.lang.InviteExpiry.get("inviteExpired"), + "expiredAt": emailer.lang.InviteExpiry.format("expiredAt", "\""+code+"\"", expiry), + "notificationNotice": emailer.lang.InviteExpiry.get("notificationNotice"), }) if err != nil { return nil, err @@ -226,9 +222,8 @@ func (emailer *Emailer) constructExpiry(code string, invite Invite, app *appCont } func (emailer *Emailer) constructCreated(code, username, address string, invite Invite, app *appContext) (*Email, error) { - lang := emailer.cLang email := &Email{ - subject: emailer.lang.get(lang, "userCreated", "title"), + subject: emailer.lang.UserCreated.get("title"), } created := app.formatDatetime(invite.Created) var tplAddress string @@ -245,14 +240,14 @@ func (emailer *Emailer) constructCreated(code, username, address string, invite } var tplData bytes.Buffer err = tpl.Execute(&tplData, map[string]string{ - "aUserWasCreated": emailer.lang.format(lang, "userCreated", "aUserWasCreated", "\""+code+"\""), - "name": emailer.lang.get(lang, "userCreated", "name"), - "address": emailer.lang.get(lang, "userCreated", "emailAddress"), - "time": emailer.lang.get(lang, "userCreated", "time"), + "aUserWasCreated": emailer.lang.UserCreated.format("aUserWasCreated", "\""+code+"\""), + "name": emailer.lang.UserCreated.get("name"), + "address": emailer.lang.UserCreated.get("emailAddress"), + "time": emailer.lang.UserCreated.get("time"), "nameVal": username, "addressVal": tplAddress, "timeVal": created, - "notificationNotice": emailer.lang.get(lang, "userCreated", "notificationNotice"), + "notificationNotice": emailer.lang.UserCreated.get("notificationNotice"), }) if err != nil { return nil, err @@ -267,9 +262,8 @@ func (emailer *Emailer) constructCreated(code, username, address string, invite } func (emailer *Emailer) constructReset(pwr PasswordReset, app *appContext) (*Email, error) { - lang := emailer.cLang email := &Email{ - subject: emailer.lang.get(lang, "passwordReset", "title"), + subject: emailer.lang.PasswordReset.get("title"), } d, t, expiresIn := emailer.formatExpiry(pwr.Expiry, true, app.datePattern, app.timePattern) message := app.config.Section("email").Key("message").String() @@ -281,12 +275,12 @@ func (emailer *Emailer) constructReset(pwr PasswordReset, app *appContext) (*Ema } var tplData bytes.Buffer err = tpl.Execute(&tplData, map[string]string{ - "helloUser": emailer.lang.format(lang, "passwordReset", "helloUser", pwr.Username), - "someoneHasRequestedReset": emailer.lang.get(lang, "passwordReset", "someoneHasRequestedReset"), - "ifItWasYou": emailer.lang.get(lang, "passwordReset", "ifItWasYou"), - "codeExpiry": emailer.lang.format(lang, "passwordReset", "codeExpiry", d, t, expiresIn), - "ifItWasNotYou": emailer.lang.get(lang, "passwordReset", "ifItWasNotYou"), - "pin": emailer.lang.get(lang, "passwordReset", "pin"), + "helloUser": emailer.lang.PasswordReset.format("helloUser", pwr.Username), + "someoneHasRequestedReset": emailer.lang.PasswordReset.get("someoneHasRequestedReset"), + "ifItWasYou": emailer.lang.PasswordReset.get("ifItWasYou"), + "codeExpiry": emailer.lang.PasswordReset.format("codeExpiry", d, t, expiresIn), + "ifItWasNotYou": emailer.lang.PasswordReset.get("ifItWasNotYou"), + "pin": emailer.lang.PasswordReset.get("pin"), "pinVal": pwr.Pin, "message": message, }) @@ -303,9 +297,8 @@ func (emailer *Emailer) constructReset(pwr PasswordReset, app *appContext) (*Ema } func (emailer *Emailer) constructDeleted(reason string, app *appContext) (*Email, error) { - lang := emailer.cLang email := &Email{ - subject: emailer.lang.get(lang, "userDeleted", "title"), + subject: emailer.lang.UserDeleted.get("title"), } for _, key := range []string{"html", "text"} { fpath := app.config.Section("deletion").Key("email_" + key).String() @@ -315,8 +308,8 @@ func (emailer *Emailer) constructDeleted(reason string, app *appContext) (*Email } var tplData bytes.Buffer err = tpl.Execute(&tplData, map[string]string{ - "yourAccountWasDeleted": emailer.lang.get(lang, "userDeleted", "yourAccountWasDeleted"), - "reason": emailer.lang.get(lang, "userDeleted", "reason"), + "yourAccountWasDeleted": emailer.lang.UserDeleted.get("yourAccountWasDeleted"), + "reason": emailer.lang.UserDeleted.get("reason"), "reasonVal": reason, }) if err != nil { diff --git a/html/admin.html b/html/admin.html index 1fea414..519b821 100644 --- a/html/admin.html +++ b/html/admin.html @@ -253,9 +253,9 @@