From eae844f78fc598ba45520c8dac82d2cf53bd4a03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Sat, 13 May 2023 12:17:41 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=AD=E3=83=BC=E3=82=AB=E3=83=A9=E3=82=A4?= =?UTF-8?q?=E3=82=BA=E3=81=AF=E9=96=A2=E6=95=B0=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++ Makefile | 2 +- lang.go | 49 ++++++++++++++++++++++++++++ main.go | 2 +- srv.go | 91 +++++++++++++++++++--------------------------------- 5 files changed, 87 insertions(+), 60 deletions(-) create mode 100644 lang.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 56617c9..1bd97e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.1.0(未公開) +* ローカライズは関数化 + # 2.0.2 * Makefileでの「make install」部分を修正 * manページで「オプションなし」部分を追加 diff --git a/Makefile b/Makefile index 045c6ee..2fba68b 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/lang.go b/lang.go new file mode 100644 index 0000000..8d28acc --- /dev/null +++ b/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 "" +} diff --git a/main.go b/main.go index 9687541..df2abf7 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,7 @@ import ( "strconv" ) -var version = "2.0.2" +var version = "2.1.0p" func help () { fmt.Println("使い方:"); diff --git a/srv.go b/srv.go index 09998ba..d64daa6 100644 --- a/srv.go +++ b/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])) } }