23a686fe96
Removed the Tempra Cursive Theme. You can now do bulk moderation actions with Shadow. Added: Argon2 as a dependency. The EmailStore. The ReportStore. The Copy method to *Setting. The AddColumn method to the query builder and adapters. The textarea setting type. More logging to better debug issues. The GetOffset method to the UserStore. Removed: Sortable from Code Climate's Analysis. MemberCheck and memberCheck as they're obsolete now. The obsolete url_tags setting. The BcryptGeneratePasswordNoSalt function. Some redundant fields from some of the page structs. Revamped: The Control Panel Setting List and Editor. Refactored: The password hashing logic to make it more amenable to multiple hashing algorithms. The email portion of the Account Manager. The Control Panel User List. The report system. simplePanelUserCheck and simpleUserCheck to remove the duplicated logic as the two do the exact same thing. Fixed: Missing slugs in the profile links in the User Manager. A few template initialisers potentially reducing the number of odd template edge cases. Some problems with the footer. Custom selection colour not applying to images on Shadow. The avatars of the bottom row of the topic list on Conflux leaking out. Other: Moved the startTime variable into package common and exported it. Moved the password hashing logic from user.go to auth.go Split common/themes.go into common/theme.go and common/theme_list.go Replaced the SettingLabels phrase category with the more generic SettingPhrases category. Moved a load of routes, including panel ones into the routes and panel packages. Hid the notifications link from the Account Menu. Moved more inline CSS into the CSS files and made things a little more flexible here and there. Continued work on PgSQL, still a ways away. Guests now have a default avatar like everyone else. Tweaked some of the font sizes on Cosora to make the text look a little nicer. Partially implemented the theme dock override logic. Partially implemented a "symlink" like feature for theme directories. ... And a bunch of other things I might have missed. You will need to run this update script / patcher for this commit. Warning: This is an "unstable commit", therefore some things may be a little less stable than I'd like. For instance, the Shadow Theme is a little broken in this commit.
285 lines
7.2 KiB
Go
285 lines
7.2 KiB
Go
/*
|
|
*
|
|
* Gosora Phrase System
|
|
* Copyright Azareal 2017 - 2018
|
|
*
|
|
*/
|
|
package common
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"sync/atomic"
|
|
)
|
|
|
|
// TODO: Add a phrase store?
|
|
// TODO: Let the admin edit phrases from inside the Control Panel? How should we persist these? Should we create a copy of the langpack or edit the primaries? Use the changeLangpack mutex for this?
|
|
// nolint Be quiet megacheck, this *is* used
|
|
var currentLangPack atomic.Value
|
|
var langPackCount int // TODO: Use atomics for this
|
|
|
|
// TODO: We'll be implementing the level phrases in the software proper very very soon!
|
|
type LevelPhrases struct {
|
|
Level string
|
|
LevelMax string // ? Add a max level setting?
|
|
|
|
// Override the phrase for individual levels, if the phrases exist
|
|
Levels []string // index = level
|
|
}
|
|
|
|
// ! For the sake of thread safety, you must never modify a *LanguagePack directly, but to create a copy of it and overwrite the entry in the sync.Map
|
|
type LanguagePack struct {
|
|
Name string
|
|
Phrases map[string]string // Should we use a sync map or a struct for these? It would be nice, if we could keep all the phrases consistent.
|
|
Levels LevelPhrases
|
|
GlobalPerms map[string]string
|
|
LocalPerms map[string]string
|
|
SettingPhrases map[string]string
|
|
PermPresets map[string]string
|
|
Accounts map[string]string // TODO: Apply these phrases in the software proper
|
|
UserAgents map[string]string
|
|
OperatingSystems map[string]string
|
|
HumanLanguages map[string]string
|
|
Errors map[string]map[string]string // map[category]map[name]value
|
|
NoticePhrases map[string]string
|
|
PageTitles map[string]string
|
|
TmplPhrases map[string]string
|
|
|
|
TmplIndicesToPhrases [][][]byte // [tmplID][index]phrase
|
|
}
|
|
|
|
// TODO: Add the ability to edit language JSON files from the Control Panel and automatically scan the files for changes
|
|
var langPacks sync.Map // nolint it is used
|
|
var langTmplIndicesToNames [][]string // [tmplID][index]phraseName
|
|
|
|
func InitPhrases() error {
|
|
log.Print("Loading the language packs")
|
|
err := filepath.Walk("./langs", func(path string, f os.FileInfo, err error) error {
|
|
if f.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var ext = filepath.Ext("/langs/" + path)
|
|
if ext != ".json" {
|
|
log.Printf("Found a '%s' in /langs/", ext)
|
|
return nil
|
|
}
|
|
|
|
var langPack LanguagePack
|
|
err = json.Unmarshal(data, &langPack)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
langPack.TmplIndicesToPhrases = make([][][]byte, len(langTmplIndicesToNames))
|
|
for tmplID, phraseNames := range langTmplIndicesToNames {
|
|
var phraseSet = make([][]byte, len(phraseNames))
|
|
for index, phraseName := range phraseNames {
|
|
phrase, ok := langPack.TmplPhrases[phraseName]
|
|
if !ok {
|
|
log.Print("Couldn't find template phrase '" + phraseName + "'")
|
|
}
|
|
phraseSet[index] = []byte(phrase)
|
|
}
|
|
langPack.TmplIndicesToPhrases[tmplID] = phraseSet
|
|
}
|
|
|
|
log.Print("Adding the '" + langPack.Name + "' language pack")
|
|
langPacks.Store(langPack.Name, &langPack)
|
|
langPackCount++
|
|
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if langPackCount == 0 {
|
|
return errors.New("You don't have any language packs")
|
|
}
|
|
|
|
langPack, ok := langPacks.Load(Site.Language)
|
|
if !ok {
|
|
return errors.New("Couldn't find the " + Site.Language + " language pack")
|
|
}
|
|
currentLangPack.Store(langPack)
|
|
return nil
|
|
}
|
|
|
|
// TODO: Implement this
|
|
func LoadLangPack(name string) error {
|
|
_ = name
|
|
return nil
|
|
}
|
|
|
|
// TODO: Implement this
|
|
func SaveLangPack(langPack *LanguagePack) error {
|
|
_ = langPack
|
|
return nil
|
|
}
|
|
|
|
func GetPhrase(name string) (string, bool) {
|
|
res, ok := currentLangPack.Load().(*LanguagePack).Phrases[name]
|
|
return res, ok
|
|
}
|
|
|
|
func GetGlobalPermPhrase(name string) string {
|
|
res, ok := currentLangPack.Load().(*LanguagePack).GlobalPerms[name]
|
|
if !ok {
|
|
return getPhrasePlaceholder("perms", name)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func GetLocalPermPhrase(name string) string {
|
|
res, ok := currentLangPack.Load().(*LanguagePack).LocalPerms[name]
|
|
if !ok {
|
|
return getPhrasePlaceholder("perms", name)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func GetSettingPhrase(name string) string {
|
|
res, ok := currentLangPack.Load().(*LanguagePack).SettingPhrases[name]
|
|
if !ok {
|
|
return getPhrasePlaceholder("settings", name)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func GetAllSettingPhrases() map[string]string {
|
|
return currentLangPack.Load().(*LanguagePack).SettingPhrases
|
|
}
|
|
|
|
func GetAllPermPresets() map[string]string {
|
|
return currentLangPack.Load().(*LanguagePack).PermPresets
|
|
}
|
|
|
|
func GetAccountPhrase(name string) string {
|
|
res, ok := currentLangPack.Load().(*LanguagePack).Accounts[name]
|
|
if !ok {
|
|
return getPhrasePlaceholder("account", name)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func GetUserAgentPhrase(name string) (string, bool) {
|
|
res, ok := currentLangPack.Load().(*LanguagePack).UserAgents[name]
|
|
if !ok {
|
|
return "", false
|
|
}
|
|
return res, true
|
|
}
|
|
|
|
func GetOSPhrase(name string) (string, bool) {
|
|
res, ok := currentLangPack.Load().(*LanguagePack).OperatingSystems[name]
|
|
if !ok {
|
|
return "", false
|
|
}
|
|
return res, true
|
|
}
|
|
|
|
func GetHumanLangPhrase(name string) (string, bool) {
|
|
res, ok := currentLangPack.Load().(*LanguagePack).HumanLanguages[name]
|
|
if !ok {
|
|
return getPhrasePlaceholder("humanlang", name), false
|
|
}
|
|
return res, true
|
|
}
|
|
|
|
// TODO: Does comma ok work with multi-dimensional maps?
|
|
func GetErrorPhrase(category string, name string) string {
|
|
res, ok := currentLangPack.Load().(*LanguagePack).Errors[category][name]
|
|
if !ok {
|
|
return getPhrasePlaceholder("error", name)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func GetNoticePhrase(name string) string {
|
|
res, ok := currentLangPack.Load().(*LanguagePack).NoticePhrases[name]
|
|
if !ok {
|
|
return getPhrasePlaceholder("notices", name)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func GetTitlePhrase(name string) string {
|
|
res, ok := currentLangPack.Load().(*LanguagePack).PageTitles[name]
|
|
if !ok {
|
|
return getPhrasePlaceholder("title", name)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func GetTmplPhrase(name string) string {
|
|
res, ok := currentLangPack.Load().(*LanguagePack).TmplPhrases[name]
|
|
if !ok {
|
|
return getPhrasePlaceholder("tmpl", name)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func GetTmplPhrases() map[string]string {
|
|
return currentLangPack.Load().(*LanguagePack).TmplPhrases
|
|
}
|
|
|
|
func getPhrasePlaceholder(prefix string, suffix string) string {
|
|
return "{lang." + prefix + "[" + suffix + "]}"
|
|
}
|
|
|
|
// ? - Use runtime reflection for updating phrases?
|
|
// TODO: Implement these
|
|
func AddPhrase() {
|
|
|
|
}
|
|
func UpdatePhrase() {
|
|
|
|
}
|
|
func DeletePhrase() {
|
|
|
|
}
|
|
|
|
// TODO: Use atomics to store the pointer of the current active langpack?
|
|
// nolint
|
|
func ChangeLanguagePack(name string) (exists bool) {
|
|
pack, ok := langPacks.Load(name)
|
|
if !ok {
|
|
return false
|
|
}
|
|
currentLangPack.Store(pack)
|
|
return true
|
|
}
|
|
|
|
func CurrentLanguagePackName() (name string) {
|
|
return currentLangPack.Load().(*LanguagePack).Name
|
|
}
|
|
|
|
func GetLanguagePackByName(name string) (pack *LanguagePack, ok bool) {
|
|
packInt, ok := langPacks.Load(name)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
return packInt.(*LanguagePack), true
|
|
}
|
|
|
|
// Template Transpiler Stuff
|
|
|
|
func RegisterTmplPhraseNames(phraseNames []string) (tmplID int) {
|
|
langTmplIndicesToNames = append(langTmplIndicesToNames, phraseNames)
|
|
return len(langTmplIndicesToNames) - 1
|
|
}
|
|
|
|
func GetTmplPhrasesBytes(tmplID int) [][]byte {
|
|
return currentLangPack.Load().(*LanguagePack).TmplIndicesToPhrases[tmplID]
|
|
}
|