I18N
+ Added language detection and default language
This commit is contained in:
parent
dfd35a6164
commit
1f82828c82
@ -10,6 +10,7 @@ func checkConfiguration() {
|
|||||||
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameDATABASE, `Check now the configuration database.`)
|
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameDATABASE, `Check now the configuration database.`)
|
||||||
defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameDATABASE, `Done checking the configuration database.`)
|
defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameDATABASE, `Done checking the configuration database.`)
|
||||||
|
|
||||||
|
CheckSingleConfigurationPresentsAndAddIfMissing(`DefaultLanguageCode`, `en-GB`)
|
||||||
CheckSingleConfigurationPresentsAndAddIfMissing(`AdminWebServerBinding`, `127.0.0.1:60000`)
|
CheckSingleConfigurationPresentsAndAddIfMissing(`AdminWebServerBinding`, `127.0.0.1:60000`)
|
||||||
CheckSingleConfigurationPresentsAndAddIfMissing(`AdminWebServerEnabled`, `True`)
|
CheckSingleConfigurationPresentsAndAddIfMissing(`AdminWebServerEnabled`, `True`)
|
||||||
CheckSingleConfigurationPresentsAndAddIfMissing(`AdminWebServerReadTimeoutSeconds`, `10`)
|
CheckSingleConfigurationPresentsAndAddIfMissing(`AdminWebServerReadTimeoutSeconds`, `10`)
|
||||||
|
@ -31,4 +31,7 @@ func init() {
|
|||||||
allHostsIPAddresses := ReadAllIPAddresses4ThisHost()
|
allHostsIPAddresses := ReadAllIPAddresses4ThisHost()
|
||||||
port := ConfigurationDB.Read(`PublicWebServerPort`)
|
port := ConfigurationDB.Read(`PublicWebServerPort`)
|
||||||
localIPAddressAndPort = allHostsIPAddresses[0] + `:` + port
|
localIPAddressAndPort = allHostsIPAddresses[0] + `:` + port
|
||||||
|
|
||||||
|
// Read the default language:
|
||||||
|
defaultLanguage = ConfigurationDB.Read(`DefaultLanguageCode`)
|
||||||
}
|
}
|
||||||
|
51
Tools/RequestLanguage.go
Normal file
51
Tools/RequestLanguage.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package Tools
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetRequestLanguage(request *http.Request) (resultLangs Languages) {
|
||||||
|
languageCodeForm := request.FormValue(`lang`)
|
||||||
|
languageCodeHeader := request.Header.Get(`Accept-Language`)
|
||||||
|
|
||||||
|
if languageCodeForm != `` && ((len(languageCodeForm) == 2) || (len(languageCodeForm) == 5 && strings.Contains(languageCodeForm, `-`))) {
|
||||||
|
resultLangs = make(Languages, 1)
|
||||||
|
resultLangs[0].Factor = 1.0
|
||||||
|
resultLangs[0].Language = strings.ToLower(languageCodeForm)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if languageCodeHeader == `` {
|
||||||
|
resultLangs = make(Languages, 1)
|
||||||
|
resultLangs[0].Factor = 1.0
|
||||||
|
resultLangs[0].Language = defaultLanguage
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
values := strings.Split(languageCodeHeader, `,`)
|
||||||
|
langs := make(Languages, len(values))
|
||||||
|
for n, langData := range values {
|
||||||
|
if factorData := strings.Split(langData, `;q=`); len(factorData) == 2 {
|
||||||
|
if factor, errFactor := strconv.ParseFloat(factorData[1], 32); errFactor != nil {
|
||||||
|
langs[n] = Language{}
|
||||||
|
langs[n].Language = strings.ToLower(strings.Trim(factorData[0], ` `))
|
||||||
|
langs[n].Factor = 1.0
|
||||||
|
} else {
|
||||||
|
langs[n] = Language{}
|
||||||
|
langs[n].Language = strings.ToLower(strings.Trim(factorData[0], ` `))
|
||||||
|
langs[n].Factor = float32(factor)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
langs[n] = Language{}
|
||||||
|
langs[n].Language = strings.ToLower(strings.Trim(langData, ` `))
|
||||||
|
langs[n].Factor = 1.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(langs)
|
||||||
|
resultLangs = langs
|
||||||
|
return
|
||||||
|
}
|
9
Tools/ResponseLanguage.go
Normal file
9
Tools/ResponseLanguage.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package Tools
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SendChosenLanguage(response http.ResponseWriter, lang Language) {
|
||||||
|
response.Header().Add(`Content-Language`, lang.Language)
|
||||||
|
}
|
20
Tools/SchemeLanguage.go
Normal file
20
Tools/SchemeLanguage.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package Tools
|
||||||
|
|
||||||
|
type Language struct {
|
||||||
|
Language string
|
||||||
|
Factor float32
|
||||||
|
}
|
||||||
|
|
||||||
|
type Languages []Language
|
||||||
|
|
||||||
|
func (lang Languages) Len() int {
|
||||||
|
return len(lang)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lang Languages) Less(i, j int) bool {
|
||||||
|
return lang[i].Factor > lang[j].Factor
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lang Languages) Swap(i, j int) {
|
||||||
|
lang[i], lang[j] = lang[j], lang[i]
|
||||||
|
}
|
@ -10,4 +10,5 @@ var (
|
|||||||
localIPAddressAndPort string = `127.0.0.1:60000`
|
localIPAddressAndPort string = `127.0.0.1:60000`
|
||||||
ipAddresses []string = nil
|
ipAddresses []string = nil
|
||||||
internalCommPassword string = ``
|
internalCommPassword string = ``
|
||||||
|
defaultLanguage string = `en`
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user