Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/util/publicSettings/publicSettings.go
akuma06 5376b9e271 New config files (#854)
* New config files

As decided, config files are parsed at runtime.
I decided to go for YAML config files because there can be comments in
it.
There are 2 files:
* config/default_config.yml <= which shouldn't be edited unless we add a
config parameter
* config/config.yml <= which is the user-defined config. This file
shouldn't be commited

Changed every call to config.XXX to config.Conf.XXX (look to the new
stucture of config in config/types.go)

Of course, putting config parameters in config.yml overrides config in
config_default.yml. You don't have to put everything in it, just add
what you want to override.

* Fixing test

Replacing conf.New by config.Conf

* Fixing call to config.Conf to config.Config{} in test files

* Might have fixed testing with this

Printf instead of Fatalf

* Renaming config.yml in example file

* Forbid commiting config.yml

* Should be now fixed

* Do not need this file anymore
2017-05-30 21:21:57 -05:00

168 lignes
4,8 Kio
Go

package publicSettings
import (
"errors"
"fmt"
"html/template"
"net/http"
"path"
"path/filepath"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/model"
"github.com/nicksnyder/go-i18n/i18n"
"github.com/nicksnyder/go-i18n/i18n/language"
)
// UserRetriever : this interface is required to prevent a cyclic import between the languages and userService package.
type UserRetriever interface {
RetrieveCurrentUser(r *http.Request) (model.User, error)
}
// TemplateTfunc : T func used in template
type TemplateTfunc func(string, ...interface{}) template.HTML
var (
defaultLanguage = config.Conf.I18n.DefaultLanguage
userRetriever UserRetriever
)
// InitI18n : Initialize the languages translation
func InitI18n(conf config.I18nConfig, retriever UserRetriever) error {
defaultLanguage = conf.DefaultLanguage
userRetriever = retriever
defaultFilepath := path.Join(conf.Directory, defaultLanguage+".all.json")
err := i18n.LoadTranslationFile(defaultFilepath)
if err != nil {
panic(fmt.Sprintf("failed to load default translation file '%s': %v", defaultFilepath, err))
}
paths, err := filepath.Glob(path.Join(conf.Directory, "*.json"))
if err != nil {
return fmt.Errorf("failed to get translation files: %v", err)
}
for _, file := range paths {
err := i18n.LoadTranslationFile(file)
if err != nil {
return fmt.Errorf("failed to load translation file '%s': %v", file, err)
}
}
return nil
}
// GetDefaultLanguage : returns the default language from config
func GetDefaultLanguage() string {
return defaultLanguage
}
// TfuncAndLanguageWithFallback : When go-i18n finds a language with >0 translations, it uses it as the Tfunc
// However, if said language has a missing translation, it won't fallback to the "main" language
func TfuncAndLanguageWithFallback(language string, languages ...string) (i18n.TranslateFunc, *language.Language, error) {
fallbackLanguage := GetDefaultLanguage()
tFunc, tLang, err1 := i18n.TfuncAndLanguage(language, languages...)
// If fallbackLanguage fails, it will give the "id" field so we don't
// care about the error
fallbackT, fallbackTlang, _ := i18n.TfuncAndLanguage(fallbackLanguage)
translateFunction := func(translationID string, args ...interface{}) string {
if translated := tFunc(translationID, args...); translated != translationID {
return translated
}
return fallbackT(translationID, args...)
}
if err1 != nil {
tLang = fallbackTlang
}
return translateFunction, tLang, err1
}
// GetAvailableLanguages : Get languages available on the website
func GetAvailableLanguages() (languages map[string]string) {
languages = make(map[string]string)
var T i18n.TranslateFunc
for _, languageTag := range i18n.LanguageTags() {
T, _ = i18n.Tfunc(languageTag)
/* Translation files should have an ID with the translated language name.
If they don't, just use the languageTag */
if languageName := T("language_name"); languageName != "language_name" {
languages[languageTag] = languageName
} else {
languages[languageTag] = languageTag
}
}
return
}
// GetDefaultTfunc : Gets T func from default language
func GetDefaultTfunc() (i18n.TranslateFunc, error) {
return i18n.Tfunc(defaultLanguage)
}
// GetTfuncAndLanguageFromRequest : Gets the T func and chosen language from the request
func GetTfuncAndLanguageFromRequest(r *http.Request) (T i18n.TranslateFunc, Tlang *language.Language) {
userLanguage := ""
user, _ := getCurrentUser(r)
if user.ID > 0 {
userLanguage = user.Language
}
cookie, err := r.Cookie("lang")
cookieLanguage := ""
if err == nil {
cookieLanguage = cookie.Value
}
// go-i18n supports the format of the Accept-Language header
headerLanguage := r.Header.Get("Accept-Language")
T, Tlang, _ = TfuncAndLanguageWithFallback(userLanguage, cookieLanguage, headerLanguage)
return
}
// GetTfuncFromRequest : Gets the T func from the request
func GetTfuncFromRequest(r *http.Request) TemplateTfunc {
T, _ := GetTfuncAndLanguageFromRequest(r)
return func(id string, args ...interface{}) template.HTML {
return template.HTML(fmt.Sprintf(T(id), args...))
}
}
// GetThemeFromRequest: Gets the user selected theme from the request
func GetThemeFromRequest(r *http.Request) string {
user, _ := getCurrentUser(r)
if user.ID > 0 {
return user.Theme
}
cookie, err := r.Cookie("theme")
if err == nil {
return cookie.Value
}
return ""
}
// GetThemeFromRequest: Gets the user selected theme from the request
func GetMascotFromRequest(r *http.Request) string {
user, _ := getCurrentUser(r)
if user.ID > 0 {
return user.Mascot
}
cookie, err := r.Cookie("mascot")
if err == nil {
return cookie.Value
}
return "show"
}
func getCurrentUser(r *http.Request) (model.User, error) {
if userRetriever == nil {
return model.User{}, errors.New("failed to get current user: no user retriever set")
}
return userRetriever.RetrieveCurrentUser(r)
}