92 lines
2.5 KiB
Go
92 lines
2.5 KiB
Go
package localization
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/language"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
type LanguageView struct {
|
|
Lang string `json:"lang"`
|
|
Enabled bool `json:"enabled"`
|
|
IsDefault bool `json:"is_default"`
|
|
VersionCount int `json:"version_count"`
|
|
PublishedCount int `json:"published_count"`
|
|
SocialLocale *string `json:"social_locale"`
|
|
SocialLocaleState string `json:"social_locale_state"`
|
|
}
|
|
|
|
type Option struct {
|
|
Lang string `json:"lang"`
|
|
Substitute *string `json:"substitute"`
|
|
}
|
|
|
|
type View struct {
|
|
Revision string `json:"revision"`
|
|
Default *string `json:"default"`
|
|
NeedsSetup bool `json:"needs_setup"`
|
|
Languages []LanguageView `json:"languages"`
|
|
Available []Option `json:"available"`
|
|
}
|
|
|
|
var unsupportedVariants = []string{"de-AT", "de-DE", "es-ES", "es-MX", "fr-BE", "fr-FR", "pt-PT"}
|
|
|
|
func NeedsSetup(rows []Row) bool {
|
|
return !slices.ContainsFunc(rows, func(row Row) bool { return row.IsDefault && row.Confirmed })
|
|
}
|
|
|
|
func Available() []Option {
|
|
options := []Option{}
|
|
for _, tag := range language.Supported() {
|
|
options = append(options, Option{Lang: tag})
|
|
}
|
|
for _, tag := range unsupportedVariants {
|
|
base, _, _ := strings.Cut(tag, "-")
|
|
substitute := base
|
|
options = append(options, Option{Lang: tag, Substitute: &substitute})
|
|
}
|
|
slices.SortFunc(options, func(left Option, right Option) int { return strings.Compare(left.Lang, right.Lang) })
|
|
return options
|
|
}
|
|
|
|
func BuildView(rows []Row, counts map[string]VersionCount) View {
|
|
view := View{Revision: Revision(rows), NeedsSetup: NeedsSetup(rows), Languages: []LanguageView{}, Available: Available()}
|
|
if view.NeedsSetup {
|
|
return view
|
|
}
|
|
ordered := slices.Clone(rows)
|
|
slices.SortFunc(ordered, compareRows)
|
|
for _, row := range ordered {
|
|
if row.IsDefault {
|
|
tag := row.Tag
|
|
view.Default = &tag
|
|
}
|
|
view.Languages = append(view.Languages, LanguageView{
|
|
Lang: row.Tag,
|
|
Enabled: row.Enabled,
|
|
IsDefault: row.IsDefault,
|
|
VersionCount: counts[row.Tag].Total,
|
|
PublishedCount: counts[row.Tag].Published,
|
|
SocialLocale: row.SocialLocale,
|
|
SocialLocaleState: SocialLocaleState(row),
|
|
})
|
|
}
|
|
return view
|
|
}
|
|
|
|
func compareRows(left Row, right Row) int {
|
|
if left.IsDefault != right.IsDefault {
|
|
if left.IsDefault {
|
|
return -1
|
|
}
|
|
return 1
|
|
}
|
|
if left.Enabled != right.Enabled {
|
|
if left.Enabled {
|
|
return -1
|
|
}
|
|
return 1
|
|
}
|
|
return strings.Compare(left.Tag, right.Tag)
|
|
}
|