Refactor everything

This commit is contained in:
Andrew-71 2024-10-21 16:46:25 +03:00
parent b56ce43c80
commit 57903d4724
45 changed files with 514 additions and 416 deletions

View file

@ -0,0 +1,35 @@
{
"lang": "en-UK",
"title.today": "Your day so far",
"title.days": "Previous days",
"title.notes": "Notes",
"title.info": "Info",
"link.today": "today",
"link.tomorrow": "tomorrow",
"link.days": "previous days",
"link.notes": "notes",
"link.info": "app information",
"time.date": "Today is",
"time.grace": "grace period active",
"button.save": "Save",
"button.notes": "New note",
"prompt.notes": "Note name",
"noscript.notes": "/notes/<name>",
"prompt.days": "To the bottom",
"info.version": "Version",
"info.version.link": "source and changelog",
"info.export": "Export data",
"info.readme": "Edit readme.txt",
"info.config": "Edit config",
"info.telegram.auth_fail": "Failed auth attempt in Hibiscus.txt",
"info.telegram.scram": "Hibiscus SCRAM triggered, shutting down",
"error.404": "The page you were looking for doesn't exist",
"error.500": "It's probably not your fault, but something broke",
"error.prompt": "Go home?"
}

View file

@ -0,0 +1,35 @@
{
"lang": "ru",
"title.today": "Сегодняшний день",
"title.days": "Предыдущие дни",
"title.notes": "Заметки",
"title.info": "Информация",
"link.today": "сегодня",
"link.tomorrow": "завтра",
"link.days": "раньше",
"link.notes": "заметки",
"link.info": "системная информация",
"time.date": "Сегодня",
"time.grace": "редактируется вчерашний день",
"button.save": "Сохранить",
"button.notes": "Новая заметка",
"prompt.notes": "Название заметки",
"noscript.notes": "/notes/<название>",
"prompt.days": "Вниз",
"info.version": "Версия",
"info.version.link": "исходный код",
"info.export": "Экспорт данных",
"info.readme": "Редактировать readme.txt",
"info.config": "Редактировать конфиг",
"info.telegram_notification": "Неверная попытка авторизации в Hibiscus.txt",
"info.telegram.scram": "Активирована функция SCRAM в Hibiscus.txt, сервер выключается",
"error.404": "Страница, которую вы ищете, не существует",
"error.500": "Что-то сломалось",
"error.prompt": "На главную?"
}

41
internal/lang/main.go Normal file
View file

@ -0,0 +1,41 @@
package lang
import (
"embed"
"encoding/json"
"log/slog"
)
//go:embed lang
var lang embed.FS
var translations = map[string]string{}
// SetLanguage loads a json file for selected language into the Translations map, with English language as a fallback.
func SetLanguage(language string) error {
loadLanguage := func(language string) error {
filename := "lang/" + language + ".json"
fileContents, err := lang.ReadFile(filename)
if err != nil {
slog.Error("error reading language file",
"error", err,
"file", filename)
return err
}
return json.Unmarshal(fileContents, &translations)
}
translations = map[string]string{} // Clear the map to avoid previous language remaining
err := loadLanguage("en") // Load English as fallback
if err != nil {
return err
}
return loadLanguage(language)
}
// Translate attempts to match an id to a string in current language.
func Translate(id string) string {
if v, ok := translations[id]; !ok {
return id
} else {
return v
}
}