diff --git a/CHANGELOG.md b/CHANGELOG.md
index 23702d1..4007733 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+# 1.1.0
+* 言語はliblocale化
+* 複数言語対応
+
# 1.0.0
* PHPからGoに交換しました
* 今度からバージョンを付きます
diff --git a/go.mod b/go.mod
index be53e79..1175266 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,5 @@
module 076/hozonsite
go 1.20
+
+require gitler.moe/suwako/goliblocale v1.0.0
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..7642e2e
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,2 @@
+gitler.moe/suwako/goliblocale v1.0.0 h1:QiQKNzdgpavwmAaYhAb5pth0I6qS8IJ7q2hYAgpXacU=
+gitler.moe/suwako/goliblocale v1.0.0/go.mod h1:pdv9Go5taevY8ClBOA+oLXjGap7G1RmIVKUMF8HSJmU=
diff --git a/lang.go b/lang.go
deleted file mode 100644
index 71169c5..0000000
--- a/lang.go
+++ /dev/null
@@ -1,39 +0,0 @@
-package main
-
-import (
- "encoding/json"
- "fmt"
-)
-
-func getlist (lang string) []byte {
- var jloc = []byte(`{
- "top": "トップ",
- "errfuseiurl": "URLは「http://」又は「https://」で始めます。",
- "errfusei": "不正なエラー。"
- }`)
- var eloc = []byte(`{
- "top": "Top",
- "errfuseiurl": "The URL should start with \"http://\" or \"https://\".",
- "errfusei": "Unknown error."
- }`)
-
- 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/locale/en.json b/locale/en.json
new file mode 100644
index 0000000..0aa2d36
--- /dev/null
+++ b/locale/en.json
@@ -0,0 +1,17 @@
+{
+ "hozonsite": "Hozon Site",
+ "desc": "It is the world's first FOSS web archiver.",
+ "logo": "Logo",
+ "top": "Top",
+ "langchange": "Language change",
+ "totop": "Return to toppage",
+ "tophe": "To toppage.",
+ "topwhatsave": "Which page will you archive?",
+ "hozon": "Archive",
+ "archwhozonsite": "Archived with Hozon Site.",
+ "areadyhozon": "Pages that already got archived:",
+ "willreallyhozon": "This page seems to have been already archived.
Do you really want to proceed?",
+ "yesreallyhozon": "Yes, please archive!!",
+ "errfuseiurl": "The URL should start with \"http://\" or \"https://\".",
+ "errfusei": "Unknown error."
+}
diff --git a/locale/ja.json b/locale/ja.json
new file mode 100644
index 0000000..a27b2c1
--- /dev/null
+++ b/locale/ja.json
@@ -0,0 +1,17 @@
+{
+ "hozonsite": "保存サイト",
+ "desc": "世界初FOSS系ウエブアーカイバーです。",
+ "logo": "ロゴ",
+ "top": "トップ",
+ "langchange": "言語変更",
+ "totop": "トップページに戻る",
+ "tophe": "トップページへ",
+ "topwhatsave": "どのページを保存しますか?",
+ "hozon": "保存",
+ "archwhozonsite": "保存サイトでアーカイブしました。",
+ "areadyhozon": "既に保存されたページ:",
+ "willreallyhozon": "このページが既に保存されているみたいです。
本当に手続きましょうか?",
+ "yesreallyhozon": "はい、保存して下さい!!",
+ "errfuseiurl": "URLは「http://」又は「https://」で始めます。",
+ "errfusei": "不正なエラー。"
+}
diff --git a/main.go b/main.go
index 44f3106..6731f24 100644
--- a/main.go
+++ b/main.go
@@ -7,7 +7,7 @@ import (
"strconv"
)
-var version = "1.0.0"
+var version = "1.1.0"
func help () {
fmt.Println("使い方:");
diff --git a/srv.go b/srv.go
index facd846..b4240fa 100644
--- a/srv.go
+++ b/srv.go
@@ -9,11 +9,13 @@ import (
"time"
"os"
"encoding/json"
+ "gitler.moe/suwako/goliblocale"
)
type (
Page struct {
- Tit, Err, Lan, Ver, Ves, Url, Body string
+ Err, Lan, Ver, Ves, Url, Body string
+ i18n map[string]string
Ext []Exist // 既に存在する場合
}
Stat struct { // APIのみ
@@ -24,6 +26,10 @@ type (
}
)
+func (p Page) T (key string) string {
+ return p.i18n[key]
+}
+
// 日本語か英語 TODO:複数言語対応
func initloc (r *http.Request) string {
cookie, err := r.Cookie("lang")
@@ -52,12 +58,17 @@ func siteHandler (cnf Config) func (http.ResponseWriter, *http.Request) {
data := &Page{Ver: version, Ves: strings.ReplaceAll(version, ".", "")}
lang := initloc(r)
-
data.Lan = lang
+
+ i18n, err := goliblocale.GetLocale("locale/" + lang)
+ if err != nil {
+ fmt.Println("liblocaleエラー:%v", err)
+ return
+ }
+ data.i18n = i18n
ftmpl[0] = cnf.webpath + "/view/index.html"
tmpl := template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
- data.Tit = getloc("top", lang)
if r.Method == "POST" {
err := r.ParseForm()
if err != nil {
@@ -66,14 +77,9 @@ func siteHandler (cnf Config) func (http.ResponseWriter, *http.Request) {
return
}
- // クッキー
- if r.PostForm.Get("langchange") != "" {
- cookie, err := r.Cookie("lang")
- if err != nil || cookie.Value == "ja" {
- http.SetCookie(w, &http.Cookie {Name: "lang", Value: "en", MaxAge: 31536000, Path: "/"})
- } else {
- http.SetCookie(w, &http.Cookie {Name: "lang", Value: "ja", MaxAge: 31536000, Path: "/"})
- }
+ // 言語変更
+ if lang := r.PostFormValue("lang"); lang != "" {
+ http.SetCookie(w, &http.Cookie{Name: "lang", Value: lang, MaxAge: 31536000, Path: "/"})
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
@@ -84,7 +90,7 @@ func siteHandler (cnf Config) func (http.ResponseWriter, *http.Request) {
url := r.PostForm.Get("hozonsite")
// HTTPかHTTPSじゃない場合
if !checkprefix(url) {
- data.Err = getloc("errfuseiurl", lang)
+ data.Err = i18n["errfuseiurl"]
ftmpl[0] = cnf.webpath + "/view/404.html"
} else {
eurl := stripurl(url)
@@ -114,7 +120,7 @@ func siteHandler (cnf Config) func (http.ResponseWriter, *http.Request) {
}
data.Ext = existing
} else {
- data.Err = getloc("errfusei", lang)
+ data.Err = i18n["errfusei"]
ftmpl[0] = cnf.webpath + "/view/404.html"
}
}
@@ -142,8 +148,14 @@ func archiveHandler (cnf Config) func (http.ResponseWriter, *http.Request) {
ftmpl := []string{cnf.webpath + "/view/index.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"}
data := &Page{Ver: version, Ves: strings.ReplaceAll(version, ".", "")}
lang := initloc(r)
-
data.Lan = lang
+
+ i18n, err := goliblocale.GetLocale("locale/" + lang)
+ if err != nil {
+ fmt.Println("liblocaleエラー:%v", err)
+ return
+ }
+ data.i18n = i18n
ftmpl[0] = cnf.webpath + "/view/index.html"
tmpl := template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
path := strings.TrimPrefix(r.URL.Path, "/archive/")
diff --git a/static/style.css b/static/style.css
index f73d05a..8e030e6 100644
--- a/static/style.css
+++ b/static/style.css
@@ -69,10 +69,30 @@ input[type="text"] {
border-color: #ea8181;
}
-.submit, .footer, h1, .switchlang {
+select {
+ background-color: #600e0e;
+ color: #ea8181;
+ font-size: 18px;
+ border-radius: 4px;
+ max-width: 700px;
+ border: 1px #ff3b3b groove;
+ padding: 8px;
+ width: 200px;
+}
+
+select, input.langchange {
+ height: 35px;
+ vertical-align: middle;
+}
+
+.submit, .footer, h1 {
text-align: center;
}
+input.langchange {
+ font-size: 24px;
+}
+
.submit, .footer, h1 {
margin-top: 32px;
}
diff --git a/view/404.html b/view/404.html
index 9e10b39..5ff36fc 100644
--- a/view/404.html
+++ b/view/404.html
@@ -1,4 +1,4 @@
{{template "header" .}}
{{ .Err }}
- {{if eq .Lan "ja"}}トップページに戻る{{else}}Return to toppage{{end}}
+ {{.T "totop"}}
{{template "footer" .}}
diff --git a/view/archive.html b/view/archive.html
index 4961989..eb8bc64 100644
--- a/view/archive.html
+++ b/view/archive.html
@@ -28,8 +28,8 @@
- {{if eq .Lan "en"}}
- This page seems to have been already archived.
Do you really want to proceed?
- {{else}}
- このページが既に保存されているみたいです。
本当に手続きましょうか?
- {{end}}
+ {{.T "willreallyhozon"}}