split a bunch of lists free of UI templates, and also fixed profile language display

This commit is contained in:
2025-10-20 15:53:23 -06:00
parent e5ad827f24
commit 8b00cfce1f
6 changed files with 215 additions and 126 deletions
+56
View File
@@ -0,0 +1,56 @@
/*
* Amsterdam Web Communities System
* Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Package util contains utility definitions.
package util
import (
"slices"
"strings"
"sync"
"git.erbosoft.com/amy/amsterdam/config"
"github.com/biter777/countries"
)
// cachedCountryList is the cached country list after sorting.
var cachedCountryList []countries.CountryCode = nil
// countryListMutex control access to internalGetCountryList.
var countryListMutex sync.Mutex
// AmCountryList is a wrapper around countries.All() that sorts it by country name.
func AmCountryList() []countries.CountryCode {
countryListMutex.Lock()
defer countryListMutex.Unlock()
if cachedCountryList == nil {
countryList := countries.All()
slices.SortFunc(countryList, func(a countries.CountryCode, b countries.CountryCode) int {
return strings.Compare(a.Info().Name, b.Info().Name)
})
if config.GlobalConfig.Rendering.CountryList.Prioritize != "" {
for i, c := range countryList {
if c.Info().Alpha2 == config.GlobalConfig.Rendering.CountryList.Prioritize {
newList := make([]countries.CountryCode, len(countryList))
newList[0] = c
copy(newList[1:], countryList[:i])
copy(newList[i+1:], countryList[i+1:])
countryList = newList
}
}
}
cachedCountryList = countryList
}
return cachedCountryList
}
// init preloads the country list.
func init() {
go AmCountryList()
}
+94
View File
@@ -0,0 +1,94 @@
/*
* Amsterdam Web Communities System
* Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Package util contains utility definitions.
package util
import (
_ "embed"
"slices"
"strings"
"sync"
log "github.com/sirupsen/logrus"
"golang.org/x/text/language"
"golang.org/x/text/language/display"
)
//go:embed languages.txt
var knownLanguages string
// Language is a type for a list of all supported languages.
type Language struct {
Tag string // the BCP 47 tag, such as "en-US"
Name string // the human-readable name, like "American English"
}
// cachedLanguageList is the cached language list.
var cachedLanguageList []Language = nil
// mapping from language tag names to actual language entries
var languageTagMapper map[string]*Language
// languageListMutex controls access to internalGetLanguageList.
var languageListMutex sync.Mutex
// AmLanguageList returns a list of all known languages.
func AmLanguageList() []Language {
languageListMutex.Lock()
defer languageListMutex.Unlock()
if cachedLanguageList == nil {
langs := strings.Split(knownLanguages, "\n")
enNamer := display.English.Tags()
cachedLanguageList = make([]Language, 0, len(langs))
for _, l := range langs {
tag, err := language.Parse(l)
if err == nil {
cachedLanguageList = append(cachedLanguageList, Language{
Tag: tag.String(),
Name: enNamer.Name(tag),
})
} else {
log.Errorf("*** PUKE on parsing language tag %s: %v", l, err)
}
}
slices.SortFunc(cachedLanguageList, func(a Language, b Language) int {
return strings.Compare(a.Name, b.Name)
})
languageTagMapper = make(map[string]*Language)
for i := range cachedLanguageList {
languageTagMapper[strings.ToLower(cachedLanguageList[i].Tag)] = &(cachedLanguageList[i])
}
}
return cachedLanguageList
}
/* AmLanguageInLanguage displays a language name in any other language.
* Parameters:
* lang - The language to be displayed.
* inLang - The language to display the other language's name in.
* Returns:
* The full translated language name.
*/
func AmLanguageInLanguage(lang language.Tag, inLang language.Tag) string {
namer := display.Tags(inLang)
if namer == nil {
namer = display.English.Tags()
}
s := namer.Name(lang)
if s == "" {
s = display.English.Tags().Name(lang)
}
return s
}
func init() {
go AmLanguageList() // preload the list in the background
}
+150
View File
@@ -0,0 +1,150 @@
ja-JP
es-PE
en
es-PA
sr-BA
mk
es-GT
ar-AE
no-NO
sq-AL
bg
ar-IQ
ar-YE
hu
pt-PT
el-CY
ar-QA
mk-MK
sv
de-CH
en-US
fi-FI
is
cs
en-MT
sl-SI
sk-SK
it
tr-TR
zh
th
ar-SA
no
en-GB
sr-CS
lt
ro
en-NZ
nn-NO
lt-LT
es-NI
nl
ga-IE
fr-BE
es-ES
ar-LB
ko
fr-CA
et-EE
ar-KW
sr-RS
es-US
es-MX
ar-SD
in-ID
ru
lv
es-UY
lv-LV
iw
pt-BR
ar-SY
hr
et
es-DO
fr-CH
hi-IN
es-VE
ar-BH
en-PH
ar-TN
fi
de-AT
es
nl-NL
es-EC
zh-TW
ar-JO
be
is-IS
es-CO
es-CR
es-CL
ar-EG
en-ZA
th-TH
el-GR
it-IT
ca
hu-HU
fr
en-IE
uk-UA
pl-PL
fr-LU
nl-BE
en-IN
ca-ES
ar-MA
es-BO
en-AU
sr
zh-SG
pt
uk
es-SV
ru-RU
ko-KR
vi
ar-DZ
vi-VN
sr-ME
sq
ar-LY
ar
zh-CN
be-BY
zh-HK
ja
iw-IL
bg-BG
in
mt-MT
es-PY
sl
fr-FR
cs-CZ
it-CH
ro-RO
es-PR
en-CA
de-DE
ga
de-LU
de
es-AR
sk
ms-MY
hr-HR
en-SG
da
mt
pl
ar-OM
tr
el
ms
sv-SE
da-DK
es-HN
+57
View File
@@ -0,0 +1,57 @@
/*
* Amsterdam Web Communities System
* Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Package util contains utility definitions.
package util
import (
"slices"
"sync"
"time"
"github.com/tkuchiki/go-timezone"
)
// cachedTimeZoneList is a wrapper around timezone.Timezones() that produces it by timezone name.
var cachedTimeZoneList []string = nil
// timeZoneListMutex controls access to internalGetTimeZoneList.
var timeZoneListMutex sync.Mutex
// AmTimeZoneList is a wrapper around TimeZone.TimeZones() that sorts and compacts the list.
func AmTimeZoneList() []string {
timeZoneListMutex.Lock()
defer timeZoneListMutex.Unlock()
if cachedTimeZoneList == nil {
timezones := timezone.New().Timezones()
ilist := make([]string, 0, len(timezones)*5)
for k, v := range timezones {
ilist = append(ilist, k)
ilist = append(ilist, v...)
}
slices.Sort(ilist)
cachedTimeZoneList = slices.Compact(ilist)
}
return cachedTimeZoneList
}
// AmMonthList is a simple wrapper that returns the names of the months to the template context.
func AmMonthList() []string {
rc := make([]string, 12)
for m := time.January; m <= time.December; m++ {
rc[m-time.January] = m.String()
}
return rc
}
// init preloads the time zone list.
func init() {
go AmTimeZoneList()
}