mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 11:34:26 +00:00
52 lines
1018 B
Go
52 lines
1018 B
Go
|
package i18n
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"strings"
|
||
|
|
||
|
"golang.org/x/text/language"
|
||
|
)
|
||
|
|
||
|
var supportedLanguages []language.Tag
|
||
|
|
||
|
func SupportedLanguages() []language.Tag {
|
||
|
if supportedLanguages == nil {
|
||
|
panic("supported languages not loaded")
|
||
|
}
|
||
|
return supportedLanguages
|
||
|
}
|
||
|
|
||
|
func SupportLanguages(languages ...language.Tag) {
|
||
|
supportedLanguages = languages
|
||
|
}
|
||
|
|
||
|
func MustLoadSupportedLanguagesFromDir() {
|
||
|
var err error
|
||
|
defer func() {
|
||
|
if err != nil {
|
||
|
panic("failed to load supported languages: " + err.Error())
|
||
|
}
|
||
|
}()
|
||
|
if supportedLanguages != nil {
|
||
|
return
|
||
|
}
|
||
|
i18nDir, err := LoadFilesystem(LOGIN).Open(i18nPath)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
defer func() {
|
||
|
err = errors.Join(err, i18nDir.Close())
|
||
|
}()
|
||
|
files, err := i18nDir.Readdir(0)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
supportedLanguages = make([]language.Tag, 0, len(files))
|
||
|
for _, file := range files {
|
||
|
lang := language.Make(strings.TrimSuffix(file.Name(), ".yaml"))
|
||
|
if lang != language.Und {
|
||
|
supportedLanguages = append(supportedLanguages, lang)
|
||
|
}
|
||
|
}
|
||
|
}
|