add i18n
This commit is contained in:
6
internal/i18nresource/locale.en.toml
Normal file
6
internal/i18nresource/locale.en.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
InvalidCredentials = "Invalid credentials."
|
||||
AuthenticationSuccess = "Authentication was a success."
|
||||
ValidationRequired = "This field is required."
|
||||
ValidationTooShort = "This field is too short. It should be at least %s characters."
|
||||
ValidationTooLong = "This field is too long. It should be under %s characters."
|
||||
ValidationPropertyFail = "Validation failed for '%s' property."
|
||||
6
internal/i18nresource/locale.fr.toml
Normal file
6
internal/i18nresource/locale.fr.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
InvalidCredentials = "Identifiants invalides."
|
||||
AuthenticationSuccess = "Connexion réussie."
|
||||
ValidationRequired = "Ce champ est requis."
|
||||
ValidationTooShort = "Ce champ est trop court. Il devrait contenir au moins %s caractères."
|
||||
ValidationTooLong = "Ce champ est trop long. Il ne devrait pas dépasser %s caractères."
|
||||
ValidationPropertyFail = "La validation a échoué pour la propriété '%s'."
|
||||
32
internal/i18nresource/main.go
Normal file
32
internal/i18nresource/main.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package i18nresource
|
||||
|
||||
import (
|
||||
"embed"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"github.com/nicksnyder/go-i18n/v2/i18n"
|
||||
"github.com/pelletier/go-toml"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
//go:embed locale.*.toml
|
||||
var localeFS embed.FS
|
||||
|
||||
func InitializeI18n() *i18n.Bundle {
|
||||
bundle := i18n.NewBundle(language.English)
|
||||
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
|
||||
bundle.LoadMessageFileFS(localeFS, "locale.en.toml")
|
||||
bundle.LoadMessageFileFS(localeFS, "locale.fr.toml")
|
||||
return bundle
|
||||
}
|
||||
|
||||
func GetTranslatedMessage(ac appcontext.AppContext, messageID string) string {
|
||||
localizer := i18n.NewLocalizer(ac.I18n, ac.C.GetHeader("Accept-Language"))
|
||||
message, err := localizer.LocalizeMessage(&i18n.Message{
|
||||
ID: messageID,
|
||||
})
|
||||
if err != nil {
|
||||
message = err.Error()
|
||||
}
|
||||
return message
|
||||
}
|
||||
Reference in New Issue
Block a user