ローカライズは関数化

このコミットが含まれているのは:
守矢諏訪子 2023-05-13 12:17:41 +09:00
コミット eae844f78f
5個のファイルの変更87行の追加60行の削除

ファイルの表示

@ -1,3 +1,6 @@
# 2.1.0(未公開)
* ローカライズは関数化
# 2.0.2
* Makefileでの「make install」部分を修正
* manページで「オプションなし」部分を追加

ファイルの表示

@ -1,5 +1,5 @@
NAME=urloli
VERSION=2.0.2
VERSION=$(cat main.go | grep "var version" | awk '{print $4}' | sed "s/\"//g")
# Linux、Cruxの場合は必須。他のディストリビューションはどうでも良い
PREFIX=/usr
# FreeBSDとOpenBSD

49
lang.go ノーマルファイル
ファイルの表示

@ -0,0 +1,49 @@
package main
import (
"encoding/json"
"fmt"
)
func getlist (lang string) []byte {
var jloc = []byte(`{
"top": "トップ",
"fuseiurl": "不正なURL",
"tansyukuzumi": "短縮済み",
"mikensyutu": "未検出",
"errfusei": "URLは「http://」又は「https://」で始めます。",
"errcharlim": "URLは500文字以内です。",
"errurlent": "URLをご入力下さい。",
"errurlnai": "このURLを見つけられませんでした。"
}`)
var eloc = []byte(`{
"top": "Top",
"fuseiurl": "Invalid URL",
"tansyukuzumi": "Shortened",
"mikensyutu": "Not found",
"errfusei": "The URL should start with \"http://\" or \"https://\".",
"errcharlim": "The URL should be less than 500 characters.",
"errurlent": "Please enter a URL.",
"errurlnai": "This URL could not be found."
}`)
if (lang == "en") { return eloc }
return jloc
}
func getloc (str string, lang string) string {
var payload map[string]interface{}
err := json.Unmarshal(getlist(lang), &payload)
if err != nil {
fmt.Println("loc: ", err)
return ""
}
for k, v := range payload {
if str == k {
return v.(string)
}
}
return ""
}

ファイルの表示

@ -6,7 +6,7 @@ import (
"strconv"
)
var version = "2.0.2"
var version = "2.1.0p"
func help () {
fmt.Println("使い方:");

91
srv.go
ファイルの表示

@ -17,9 +17,10 @@ type Page struct {
func serv (cnf Config, port int) {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
ftmpl := []string{cnf.webpath + "/view/index.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := &Page{Tit: "トップ", Ver: version}
data := &Page{Ver: version}
cookie, err := r.Cookie("lang")
if err != nil {
data.Lan = "ja"
@ -30,10 +31,9 @@ func serv (cnf Config, port int) {
uri := r.URL.Path
query := r.URL.Query()
qnewurl := query.Get("newurl")
if data.Lan == "en" {
data.Tit = "Top"
}
tmpl := template.Must(template.ParseFiles(cnf.webpath + "/view/index.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
data.Tit = getloc("top", data.Lan)
ftmpl[0] = cnf.webpath + "/view/index.html"
tmpl := template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
if r.Method == "POST" {
err := r.ParseForm()
@ -44,24 +44,16 @@ func serv (cnf Config, port int) {
chkprx := checkprefix(addurl)
chklim := checkcharlim(addurl)
if !chkprx {
if data.Lan == "ja" {
data.Tit = "不正なURL"
data.Err = "URLは「http://」又は「https://」で始めます。"
} else {
data.Tit = "Invalid URL"
data.Err = "The URL should start with \"http://\" or \"https://\"."
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/404.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
data.Tit = getloc("fuseiurl", data.Lan)
data.Err = getloc("errfusei", data.Lan)
ftmpl[0] = cnf.webpath + "/view/404.html"
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
}
if !chklim {
if data.Lan == "ja" {
data.Tit = "不正なURL"
data.Err = "URLは500文字以内です。"
} else {
data.Tit = "Invalid URL"
data.Err = "The URL should be less than 500 characters."
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/404.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
data.Tit = getloc("fuseiurl", data.Lan)
data.Err = getloc("errcharlim", data.Lan)
ftmpl[0] = cnf.webpath + "/view/404.html"
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
}
if chklim && chkprx {
@ -73,23 +65,16 @@ func serv (cnf Config, port int) {
res := insertjson(addurl, cnf.linkpath)
data.Url = res
data.Dom = cnf.domain
if data.Lan == "ja" {
data.Tit = "短縮済み"
} else {
data.Tit = "Shortened"
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/submitted.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
data.Tit = getloc("tansyukuzumi", data.Lan)
ftmpl[0] = cnf.webpath + "/view/submitted.html"
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
}
}
} else {
if data.Lan == "ja" {
data.Tit = "未検出"
data.Err = "URLをご入力下さい。"
} else {
data.Tit = "Not found"
data.Err = "Please enter a URL."
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/404.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
data.Tit = getloc("mikensyutu", data.Lan)
data.Err = getloc("errurlent", data.Lan)
ftmpl[0] = cnf.webpath + "/view/404.html"
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
}
} else if r.PostForm.Get("langchange") != "" {
cookie, err := r.Cookie("lang")
@ -103,40 +88,30 @@ func serv (cnf Config, port int) {
}
} else {
if uri == "/" && qnewurl == "" {
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/index.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
ftmpl[0] = cnf.webpath + "/view/index.html"
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
} else if uri != "/" && qnewurl == "" {
red, _ := geturl(uri[1:], cnf.linkpath, false)
if red != "" {
http.Redirect(w, r, red, http.StatusSeeOther)
return
} else {
if data.Lan == "ja" {
data.Tit = "未検出"
data.Err = "このURLを見つけられませんでした。"
} else {
data.Tit = "Not found"
data.Err = "This URL could not be found."
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/404.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
data.Tit = getloc("mikensyutu", data.Lan)
data.Err = getloc("errurlnai", data.Lan)
ftmpl[0] = cnf.webpath + "/view/404.html"
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
}
} else if uri == "/" && qnewurl != "" {
data.Url = qnewurl
data.Dom = cnf.domain
if data.Lan == "ja" {
data.Tit = "短縮済み"
} else {
data.Tit = "Shortened"
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/submitted.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
data.Tit = getloc("tansyukuzumi", data.Lan)
ftmpl[0] = cnf.webpath + "/view/submitted.html"
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
} else {
if data.Lan == "ja" {
data.Tit = "未検出"
data.Err = "このURLを見つけられませんでした。"
} else {
data.Tit = "Not found"
data.Err = "This URL could not be found."
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/404.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
data.Tit = getloc("mikensyutu", data.Lan)
data.Err = getloc("errurlnai", data.Lan)
ftmpl[0] = cnf.webpath + "/view/404.html"
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
}
}