add i18n selector

This commit is contained in:
Anya Tokugawa committed 2026-05-31 21:58:48 +09:00
1 parent 49a88460b4
commit e3a28c9f62
9 files changed
+291 -79

No files matched your search

+2 -1
View File
@@ -1,10 +1,11 @@
module pageswiki
go 1.22
go 1.22.12
require (
github.com/gomarkdown/markdown v0.0.0-20260417124207-7d523f7318df
github.com/google/uuid v1.6.0
golang.org/x/text v0.22.0
modernc.org/sqlite v1.32.0
)
+8 -4
View File
@@ -16,13 +16,17 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
modernc.org/cc/v4 v4.21.4 h1:3Be/Rdo1fpr8GrQ7IVw9OHtplU4gWbb+wNgeoBMmGLQ=
modernc.org/cc/v4 v4.21.4/go.mod h1:HM7VJTZbUCR3rV8EYBi9wxnJ0ZBRiGE5OeGXNA0IsLQ=
modernc.org/ccgo/v4 v4.19.2 h1:lwQZgvboKD0jBwdaeVCTouxhxAyN6iawF3STraAal8Y=
+13
View File
@@ -46,6 +46,18 @@ func (s *Server) handleConfig(
return
}
err = s.store.SetConfig(
"locale",
normalizeLocale(
strings.TrimSpace(r.FormValue("locale")),
),
)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
case "nav":
labels := r.Form["nav_label"]
@@ -149,6 +161,7 @@ func (s *Server) handleConfig(
map[string]any{
"HomePath": homePath,
"WikiName": wikiName,
"Locale": s.getLocale(),
"Objects": recent,
"NavItemsConfig": s.getNavItems(),
},
+55 -29
View File
@@ -3,6 +3,7 @@ package server
import (
"html/template"
"net/http"
"strings"
)
func (s *Server) handleHome(
@@ -20,49 +21,74 @@ func (s *Server) handleHome(
return
}
homePath =
strings.TrimSpace(
homePath,
)
if homePath != "" {
if !strings.HasPrefix(homePath, "/") {
homePath = "/" + homePath
}
objectUUID, err :=
s.store.ResolvePath(
homePath,
)
if err == nil {
if err != nil {
latest, err :=
s.store.GetLatestRevision(
objectUUID,
)
http.Error(
w,
"configured home_path is not resolvable: "+homePath,
500,
)
if err == nil {
rendered :=
s.expandMacros(
latest.Body,
homePath,
)
s.render(
w,
"page.gohtml",
map[string]any{
"Title": homePath,
"Path": homePath,
"ObjectUUID": objectUUID,
"RenderedHTML": template.HTML(
rendered,
),
},
)
return
}
return
}
latest, err :=
s.store.GetLatestRevision(
objectUUID,
)
if err != nil {
http.Error(
w,
err.Error(),
500,
)
return
}
rendered :=
s.expandMacros(
latest.Body,
homePath,
)
s.render(
w,
"page.gohtml",
map[string]any{
"Title": homePath,
"Path": homePath,
"ObjectUUID": objectUUID,
"RenderedHTML": template.HTML(
rendered,
),
},
)
return
}
rendered :=
s.expandMacros(
"{{children}}",
"{{children:/ exclude=/draft,/-,/_}}",
"/",
)
+104
View File
@@ -0,0 +1,104 @@
package server
import (
"golang.org/x/text/language"
)
var translations = map[string]map[string]string{
"en": {
"config.title": "Config",
"config.home": "Home",
"config.wiki_name": "Wiki name",
"config.locale": "Language",
"config.home_path": "Home semantic path",
"config.save": "Save",
"config.nav": "Navigation",
"config.objects": "Recent Objects",
"config.primary": "Primary Link",
"config.size": "Size",
"config.updated": "Updated",
"config.object": "Object",
"config.nav_label": "Label",
"config.nav_href": "Href",
"config.nav_order": "Order",
"config.nav_save": "Save Navigation",
},
"ja": {
"config.title": "設定",
"config.home": "ホーム",
"config.wiki_name": "Wiki名",
"config.locale": "言語",
"config.home_path": "トップページ",
"config.save": "保存",
"config.nav": "ナビゲーション",
"config.objects": "最近のオブジェクト",
"config.primary": "主なリンク",
"config.size": "サイズ",
"config.updated": "最終更新",
"config.object": "オブジェクト",
"config.nav_label": "表示名",
"config.nav_href": "リンク先",
"config.nav_order": "順序",
"config.nav_save": "ナビゲーションを保存",
},
}
func normalizeLocale(
raw string,
) string {
if raw == "" {
return "en"
}
tag, err :=
language.Parse(raw)
if err != nil {
return "en"
}
base, _ :=
tag.Base()
switch base.String() {
case "ja":
return "ja"
default:
return "en"
}
}
func (s *Server) getLocale() string {
raw, err :=
s.store.GetConfig(
"locale",
)
if err != nil {
return "en"
}
return normalizeLocale(raw)
}
func (s *Server) T(
key string,
) string {
locale :=
s.getLocale()
if v, ok := translations[locale][key]; ok {
return v
}
if v, ok := translations["en"][key]; ok {
return v
}
return key
}
+76 -34
View File
@@ -7,7 +7,65 @@ import (
"strings"
)
var childrenMacroRe = regexp.MustCompile(`\{\{children(?::([^}\s]+))?(?:\s+exclude=([^}]+))?\}\}`)
var childrenMacroRe = regexp.MustCompile(`\{\{children([^}]*)\}\}`)
func parseChildrenMacroArgs(
raw string,
currentPath string,
) (string, []string) {
targetPath := currentPath
excludeRaw := ""
raw = strings.TrimSpace(raw)
if strings.HasPrefix(raw, ":") {
raw = strings.TrimSpace(
strings.TrimPrefix(raw, ":"),
)
parts :=
strings.Fields(raw)
if len(parts) > 0 {
targetPath = parts[0]
for _, part := range parts[1:] {
if strings.HasPrefix(part, "exclude=") {
excludeRaw =
strings.TrimPrefix(
part,
"exclude=",
)
}
}
}
} else {
for _, part := range strings.Fields(raw) {
if strings.HasPrefix(part, "exclude=") {
excludeRaw =
strings.TrimPrefix(
part,
"exclude=",
)
}
}
}
if !strings.HasPrefix(targetPath, "/") {
targetPath = "/" + targetPath
}
return targetPath, splitExcludePatterns(excludeRaw)
}
func splitExcludePatterns(
raw string,
@@ -38,13 +96,18 @@ func isExcludedPath(
patterns []string,
) bool {
path = strings.TrimRight(path, "/")
for _, p := range patterns {
p = strings.TrimRight(p, "/")
if p == "" {
continue
}
if path == p ||
strings.HasPrefix(
path,
p+"/",
) {
strings.HasPrefix(path, p+"/") {
return true
}
@@ -65,34 +128,16 @@ func (s *Server) expandMacros(
matches :=
childrenMacroRe.FindStringSubmatch(m)
targetPath :=
currentPath
raw := ""
excludeRaw := ""
if len(matches) >= 2 &&
strings.TrimSpace(matches[1]) != "" {
targetPath =
strings.TrimSpace(
matches[1],
)
if len(matches) >= 2 {
raw = matches[1]
}
if len(matches) >= 3 {
excludeRaw =
strings.TrimSpace(
matches[2],
)
}
if !strings.HasPrefix(targetPath, "/") {
targetPath = "/" + targetPath
}
excludes :=
splitExcludePatterns(
excludeRaw,
targetPath, excludes :=
parseChildrenMacroArgs(
raw,
currentPath,
)
children, err :=
@@ -114,10 +159,7 @@ func (s *Server) expandMacros(
for _, child := range children {
if isExcludedPath(
child,
excludes,
) {
if isExcludedPath(child, excludes) {
continue
}
+1
View File
@@ -13,6 +13,7 @@ func mergeTemplateData(
out := map[string]any{
"NavItems": nav,
"WikiName": wikiName,
"Locale": "en",
}
if data == nil {
+2
View File
@@ -99,6 +99,8 @@ func (s *Server) mustLoadTemplates() {
"parentPath": parentPath,
"T": s.T,
"sub": func(a, b int) int {
return a - b
},
+30 -11
View File
@@ -80,7 +80,7 @@ Navigation
>
<label>
Wiki name
{{ T "config.wiki_name" }}
</label>
<input
@@ -95,7 +95,26 @@ Wiki name
<label>
Home semantic path
{{ T "config.locale" }}
</label>
<select
name="locale"
>
<option value="en" {{ if eq .Locale "en" }}selected{{ end }}>
English
</option>
<option value="ja" {{ if eq .Locale "ja" }}selected{{ end }}>
日本語
</option>
</select>
<br>
<br>
<label>
{{ T "config.home_path" }}
</label>
<input
@@ -122,16 +141,16 @@ Save
class="config-section config-panel"
>
<h2>Recent Objects</h2>
<h2>{{ T "config.objects" }}</h2>
<table class="recent-table">
<thead>
<tr>
<th>Primary Link</th>
<th>Size</th>
<th>Updated</th>
<th>Object</th>
<th>{{ T "config.primary" }}</th>
<th>{{ T "config.size" }}</th>
<th>{{ T "config.updated" }}</th>
<th>{{ T "config.object" }}</th>
</tr>
</thead>
@@ -176,7 +195,7 @@ Save
class="config-section config-panel"
>
<h2>Navigation</h2>
<h2>{{ T "config.nav" }}</h2>
<form
method="post"
@@ -193,9 +212,9 @@ Save
<thead>
<tr>
<th>Label</th>
<th>Href</th>
<th>Order</th>
<th>{{ T "config.nav_label" }}</th>
<th>{{ T "config.nav_href" }}</th>
<th>{{ T "config.nav_order" }}</th>
</tr>
</thead>