@@ -5,45 +5,91 @@ import (
|
||||
"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,'')`
|
||||
COALESCE(r.title_zh,''), COALESCE(r.title_en,''), COALESCE(r.title_ja,''),
|
||||
COALESCE(r.title_ko,''), COALESCE(r.title_vi,''), COALESCE(r.title_id,''), COALESCE(r.title_ms,''),
|
||||
COALESCE(r.description_zh,''), COALESCE(r.description_en,''), COALESCE(r.description_ja,''),
|
||||
COALESCE(r.description_ko,''), COALESCE(r.description_vi,''), COALESCE(r.description_id,''), COALESCE(r.description_ms,''),
|
||||
COALESCE(r.body_text_zh,''), COALESCE(r.body_text_en,''), COALESCE(r.body_text_ja,''),
|
||||
COALESCE(r.body_text_ko,''), COALESCE(r.body_text_vi,''), COALESCE(r.body_text_id,''), COALESCE(r.body_text_ms,'')`
|
||||
|
||||
type resourceTextI18n struct {
|
||||
TitleZhTw, TitleZhCn, TitleEn string
|
||||
DescZhTw, DescZhCn, DescEn string
|
||||
BodyZhTw, BodyZhCn, BodyEn string
|
||||
TitleZh, TitleEn, TitleJa, TitleKo, TitleVi, TitleId, TitleMs string
|
||||
DescZh, DescEn, DescJa, DescKo, DescVi, DescId, DescMs string
|
||||
BodyZh, BodyEn, BodyJa, BodyKo, BodyVi, BodyId, BodyMs string
|
||||
}
|
||||
|
||||
func pickLangField(r *http.Request, zhTW, zhCN, en string) string {
|
||||
func pickLangField(r *http.Request, zh, en, ja, ko, vi, id, ms 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, "zh"):
|
||||
if zh != "" {
|
||||
return zh
|
||||
}
|
||||
case strings.HasPrefix(lang, "en"):
|
||||
if en != "" {
|
||||
return en
|
||||
}
|
||||
case strings.HasPrefix(lang, "ja"):
|
||||
if ja != "" {
|
||||
return ja
|
||||
}
|
||||
case strings.HasPrefix(lang, "ko"):
|
||||
if ko != "" {
|
||||
return ko
|
||||
}
|
||||
case strings.HasPrefix(lang, "vi"):
|
||||
if vi != "" {
|
||||
return vi
|
||||
}
|
||||
case lang == "id", strings.HasPrefix(lang, "in"):
|
||||
if id != "" {
|
||||
return id
|
||||
}
|
||||
case strings.HasPrefix(lang, "ms"):
|
||||
if ms != "" {
|
||||
return ms
|
||||
}
|
||||
}
|
||||
if zhTW != "" {
|
||||
return zhTW
|
||||
if zh != "" {
|
||||
return zh
|
||||
}
|
||||
if zhCN != "" {
|
||||
return zhCN
|
||||
if en != "" {
|
||||
return en
|
||||
}
|
||||
return en
|
||||
if ja != "" {
|
||||
return ja
|
||||
}
|
||||
if ko != "" {
|
||||
return ko
|
||||
}
|
||||
if vi != "" {
|
||||
return vi
|
||||
}
|
||||
if id != "" {
|
||||
return id
|
||||
}
|
||||
return ms
|
||||
}
|
||||
|
||||
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)
|
||||
return pickLangField(r, t.TitleZh, t.TitleEn, t.TitleJa, t.TitleKo, t.TitleVi, t.TitleId, t.TitleMs),
|
||||
pickLangField(r, t.DescZh, t.DescEn, t.DescJa, t.DescKo, t.DescVi, t.DescId, t.DescMs),
|
||||
pickLangField(r, t.BodyZh, t.BodyEn, t.BodyJa, t.BodyKo, t.BodyVi, t.BodyId, t.BodyMs)
|
||||
}
|
||||
|
||||
func scanResourceTextI18n(
|
||||
titleZh, titleEn, titleJa, titleKo, titleVi, titleId, titleMs string,
|
||||
descZh, descEn, descJa, descKo, descVi, descId, descMs string,
|
||||
bodyZh, bodyEn, bodyJa, bodyKo, bodyVi, bodyId, bodyMs string,
|
||||
) resourceTextI18n {
|
||||
return resourceTextI18n{
|
||||
TitleZh: titleZh, TitleEn: titleEn, TitleJa: titleJa, TitleKo: titleKo, TitleVi: titleVi, TitleId: titleId, TitleMs: titleMs,
|
||||
DescZh: descZh, DescEn: descEn, DescJa: descJa, DescKo: descKo, DescVi: descVi, DescId: descId, DescMs: descMs,
|
||||
BodyZh: bodyZh, BodyEn: bodyEn, BodyJa: bodyJa, BodyKo: bodyKo, BodyVi: bodyVi, BodyId: bodyId, BodyMs: bodyMs,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user