2025-01-18 16:30:02 +00:00
|
|
|
// This file was originally part of the project "LURE - Linux User REpository", created by Elara Musayelyan.
|
|
|
|
// It has been modified as part of "ALR - Any Linux Repository" by Евгений Храмов.
|
|
|
|
//
|
|
|
|
// ALR - Any Linux Repository
|
|
|
|
// Copyright (C) 2025 Евгений Храмов
|
|
|
|
//
|
|
|
|
// 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/>.
|
2024-01-22 10:36:06 +00:00
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"golang.org/x/text/language"
|
2025-01-18 16:30:02 +00:00
|
|
|
|
|
|
|
"plemya-x.ru/alr/pkg/loggerctx"
|
2024-01-22 10:36:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
langMtx sync.Mutex
|
|
|
|
lang language.Tag
|
|
|
|
langSet bool
|
|
|
|
)
|
|
|
|
|
|
|
|
// Language returns the system language.
|
|
|
|
// The first time it's called, it'll detect the langauge based on
|
|
|
|
// the $LANG environment variable.
|
|
|
|
// Subsequent calls will just return the same value.
|
|
|
|
func Language(ctx context.Context) language.Tag {
|
|
|
|
langMtx.Lock()
|
|
|
|
defer langMtx.Unlock()
|
|
|
|
log := loggerctx.From(ctx)
|
|
|
|
if !langSet {
|
|
|
|
syslang := SystemLang()
|
|
|
|
tag, err := language.Parse(syslang)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error parsing system language").Err(err).Send()
|
|
|
|
}
|
|
|
|
base, _ := tag.Base()
|
|
|
|
lang = language.Make(base.String())
|
|
|
|
langSet = true
|
|
|
|
}
|
|
|
|
return lang
|
|
|
|
}
|
|
|
|
|
|
|
|
// SystemLang returns the system language based on
|
|
|
|
// the $LANG environment variable.
|
|
|
|
func SystemLang() string {
|
|
|
|
lang := os.Getenv("LANG")
|
|
|
|
lang, _, _ = strings.Cut(lang, ".")
|
|
|
|
if lang == "" || lang == "C" {
|
|
|
|
lang = "en"
|
|
|
|
}
|
|
|
|
return lang
|
|
|
|
}
|