lure/internal/translations/translations.go

57 lines
1.5 KiB
Go
Raw Permalink Normal View History

2023-09-20 22:38:22 +00:00
/*
* LURE - Linux User REpository
* Copyright (C) 2023 Elara Musayelyan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2023-09-19 21:28:05 +00:00
package translations
import (
2023-10-06 21:21:12 +00:00
"context"
2023-09-19 21:28:05 +00:00
"embed"
2023-10-06 22:07:53 +00:00
"sync"
2023-09-19 21:28:05 +00:00
"go.elara.ws/logger"
2023-10-08 00:34:39 +00:00
"lure.sh/lure/pkg/loggerctx"
2023-09-19 21:28:05 +00:00
"go.elara.ws/translate"
"golang.org/x/text/language"
)
//go:embed files
var translationFS embed.FS
2023-10-06 22:07:53 +00:00
var (
mu sync.Mutex
translator *translate.Translator
)
2023-09-19 21:28:05 +00:00
2023-10-06 21:21:12 +00:00
func Translator(ctx context.Context) *translate.Translator {
2023-10-06 22:07:53 +00:00
mu.Lock()
defer mu.Unlock()
2023-10-06 21:21:12 +00:00
log := loggerctx.From(ctx)
2023-09-19 21:28:05 +00:00
if translator == nil {
t, err := translate.NewFromFS(translationFS)
if err != nil {
log.Fatal("Error creating new translator").Err(err).Send()
}
translator = &t
}
return translator
}
2023-10-06 21:21:12 +00:00
func NewLogger(ctx context.Context, l logger.Logger, lang language.Tag) *translate.TranslatedLogger {
return translate.NewLogger(l, *Translator(ctx), lang)
2023-09-19 21:28:05 +00:00
}