Check if the lang is supported by mongo
If an unsoported lang is setted in the language override the document will fail on inserting in the database, causing it to be lost.
This commit is contained in:
parent
804112e459
commit
b178df89b0
1 changed files with 12 additions and 5 deletions
|
@ -151,9 +151,7 @@ func updateBook(coll *mgo.Collection, id string, data map[string]interface{}) er
|
|||
for k, _ := range data {
|
||||
record.Changes[k] = book[k]
|
||||
if k == "lang" {
|
||||
if lang := metadataLang(data); lang != "" {
|
||||
data["_lang"] = lang
|
||||
}
|
||||
data["_lang"] = metadataLang(data)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -220,9 +218,18 @@ func buildQuery(q string) bson.M {
|
|||
}
|
||||
|
||||
func metadataLang(book map[string]interface{}) string {
|
||||
text_search_langs := map[string]bool{
|
||||
"da": true, "nl": true, "en": true, "fi": true, "fr": true, "de": true,
|
||||
"hu": true, "it": true, "nb": true, "pt": true, "ro": true, "ru": true,
|
||||
"es": true, "sv": true, "tr": true}
|
||||
|
||||
lang, ok := book["lang"].([]string)
|
||||
if !ok || len(lang) == 0 || len(lang[0]) < 2 {
|
||||
return ""
|
||||
return "none"
|
||||
}
|
||||
return strings.ToLower(lang[0][0:2])
|
||||
lcode := strings.ToLower(lang[0][0:2])
|
||||
if text_search_langs[lcode] {
|
||||
return lcode
|
||||
}
|
||||
return "none"
|
||||
}
|
||||
|
|
Reference in a new issue