Commit 09f331ba authored by Sietse Ringers's avatar Sietse Ringers
Browse files

refactor: use translation helper for translating email templates and subjects

parent 6378c94e
......@@ -33,3 +33,19 @@ func ParseEmailTemplates(files, subjects map[string]string, defaultLanguage stri
return templates, nil
}
func (conf EmailConfiguration) TranslateString(strings map[string]string, lang string) string {
s, ok := strings[lang]
if ok {
return s
}
return strings[conf.DefaultLanguage]
}
func (conf EmailConfiguration) TranslateTemplate(templates map[string]*template.Template, lang string) *template.Template {
t, ok := templates[lang]
if ok {
return t
}
return templates[conf.DefaultLanguage]
}
......@@ -524,18 +524,9 @@ func (s *Server) doRegistration(msg irma.KeyshareEnrollment) (*irma.Qr, error) {
func (s *Server) sendRegistrationEmail(user *KeyshareUser, language, email string) error {
// Fetch template and configuration data for users language, falling back if needed
template, ok := s.conf.registrationEmailTemplates[language]
if !ok {
template = s.conf.registrationEmailTemplates[s.conf.DefaultLanguage]
}
verificationBaseURL, ok := s.conf.VerificationURL[language]
if !ok {
verificationBaseURL = s.conf.VerificationURL[s.conf.DefaultLanguage]
}
subject, ok := s.conf.RegistrationEmailSubject[language]
if !ok {
subject = s.conf.RegistrationEmailSubject[s.conf.DefaultLanguage]
}
template := s.conf.TranslateTemplate(s.conf.registrationEmailTemplates, language)
verificationBaseURL := s.conf.TranslateString(s.conf.VerificationURL, language)
subject := s.conf.TranslateString(s.conf.RegistrationEmailSubject, language)
// Generate token
token := common.NewSessionToken()
......
......@@ -133,14 +133,8 @@ func (s *Server) sendDeleteEmails(session *Sessiondata) error {
return err
}
template, ok := s.conf.deleteAccountTemplates[userData.language]
if !ok {
template = s.conf.deleteAccountTemplates[s.conf.DefaultLanguage]
}
subject, ok := s.conf.DeleteAccountSubject[userData.language]
if !ok {
subject = s.conf.DeleteAccountSubject[s.conf.DefaultLanguage]
}
template := s.conf.TranslateTemplate(s.conf.deleteAccountTemplates, userData.language)
subject := s.conf.TranslateString(s.conf.DeleteAccountSubject, userData.language)
var emsg bytes.Buffer
err = template.Execute(&emsg, map[string]string{"Username": userData.Username})
if err != nil {
......@@ -211,18 +205,9 @@ func (s *Server) sendLoginEmail(request EmailLoginRequest) error {
return err
}
template, ok := s.conf.loginEmailTemplates[request.Language]
if !ok {
template = s.conf.loginEmailTemplates[s.conf.DefaultLanguage]
}
subject, ok := s.conf.LoginEmailSubject[request.Language]
if !ok {
subject = s.conf.LoginEmailSubject[s.conf.DefaultLanguage]
}
baseURL, ok := s.conf.LoginEmailBaseURL[request.Language]
if !ok {
baseURL = s.conf.LoginEmailBaseURL[s.conf.DefaultLanguage]
}
template := s.conf.TranslateTemplate(s.conf.loginEmailTemplates, request.Language)
subject := s.conf.TranslateString(s.conf.LoginEmailSubject, request.Language)
baseURL := s.conf.TranslateString(s.conf.LoginEmailBaseURL, request.Language)
var emsg bytes.Buffer
err = template.Execute(&emsg, map[string]string{"TokenURL": baseURL + token})
if err != nil {
......@@ -530,14 +515,8 @@ func (s *Server) handleGetLogs(w http.ResponseWriter, r *http.Request) {
}
func (s *Server) sendEmailRemovalEmail(info UserInformation, email string) error {
template, ok := s.conf.deleteEmailTemplates[info.language]
if !ok {
template = s.conf.deleteEmailTemplates[s.conf.DefaultLanguage]
}
subject, ok := s.conf.DeleteEmailSubject[info.language]
if !ok {
subject = s.conf.DeleteEmailSubject[s.conf.DefaultLanguage]
}
template := s.conf.TranslateTemplate(s.conf.deleteEmailTemplates, info.language)
subject := s.conf.TranslateString(s.conf.DeleteEmailSubject, info.language)
var emsg bytes.Buffer
err := template.Execute(&emsg, map[string]string{"Username": info.Username})
......
......@@ -80,14 +80,8 @@ func (t *TaskHandler) sendExpiryEmails(id int64, username, lang string) error {
}
// Prepare email body
template, ok := t.conf.deleteExpiredAccountTemplate[lang]
if !ok {
template = t.conf.deleteExpiredAccountTemplate[t.conf.DefaultLanguage]
}
subject, ok := t.conf.DeleteExpiredAccountSubject[lang]
if !ok {
subject = t.conf.DeleteExpiredAccountSubject[t.conf.DefaultLanguage]
}
template := t.conf.TranslateTemplate(t.conf.deleteExpiredAccountTemplate, lang)
subject := t.conf.TranslateString(t.conf.DeleteExpiredAccountSubject, lang)
var emsg bytes.Buffer
err = template.Execute(&emsg, map[string]string{"Username": username, "Email": email})
if err != nil {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment