50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
|
|
package handlers
|
||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
// SQL fragment: nine i18n text columns on resources.
|
||
|
|
const resourceI18nColsSQL = `
|
||
|
|
COALESCE(r.title_zh_tw,''), COALESCE(r.title_zh_cn,''), COALESCE(r.title_en,''),
|
||
|
|
COALESCE(r.description_zh_tw,''), COALESCE(r.description_zh_cn,''), COALESCE(r.description_en,''),
|
||
|
|
COALESCE(r.body_text_zh_tw,''), COALESCE(r.body_text_zh_cn,''), COALESCE(r.body_text_en,'')`
|
||
|
|
|
||
|
|
type resourceTextI18n struct {
|
||
|
|
TitleZhTw, TitleZhCn, TitleEn string
|
||
|
|
DescZhTw, DescZhCn, DescEn string
|
||
|
|
BodyZhTw, BodyZhCn, BodyEn string
|
||
|
|
}
|
||
|
|
|
||
|
|
func pickLangField(r *http.Request, zhTW, zhCN, en string) string {
|
||
|
|
lang := strings.TrimSpace(r.URL.Query().Get("lang"))
|
||
|
|
if lang == "" {
|
||
|
|
lang = r.Header.Get("Accept-Language")
|
||
|
|
}
|
||
|
|
lang = strings.ToLower(strings.TrimSpace(strings.Split(lang, ",")[0]))
|
||
|
|
switch {
|
||
|
|
case strings.HasPrefix(lang, "zh-cn"), lang == "zh-hans":
|
||
|
|
if zhCN != "" {
|
||
|
|
return zhCN
|
||
|
|
}
|
||
|
|
case strings.HasPrefix(lang, "en"):
|
||
|
|
if en != "" {
|
||
|
|
return en
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if zhTW != "" {
|
||
|
|
return zhTW
|
||
|
|
}
|
||
|
|
if zhCN != "" {
|
||
|
|
return zhCN
|
||
|
|
}
|
||
|
|
return en
|
||
|
|
}
|
||
|
|
|
||
|
|
func (t resourceTextI18n) pick(r *http.Request) (title, description, body string) {
|
||
|
|
return pickLangField(r, t.TitleZhTw, t.TitleZhCn, t.TitleEn),
|
||
|
|
pickLangField(r, t.DescZhTw, t.DescZhCn, t.DescEn),
|
||
|
|
pickLangField(r, t.BodyZhTw, t.BodyZhCn, t.BodyEn)
|
||
|
|
}
|