From 2c705e614270cfcf3e800c4c193a14a13fb3336a Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Wed, 8 Nov 2023 03:16:12 +0600 Subject: [PATCH] :globe_with_meridians: Added language support for pages --- main.go | 1 + web/template/baseof.qtpl | 9 +- web/template/baseof.qtpl.go | 195 ++++++++++++++++++++++-------------- web/template/page.qtpl | 18 +++- web/template/page.qtpl.go | 182 +++++++++++++++++++++------------ 5 files changed, 263 insertions(+), 142 deletions(-) diff --git a/main.go b/main.go index f4355b7..37b44e6 100644 --- a/main.go +++ b/main.go @@ -62,6 +62,7 @@ func main() { w.Header().Set(common.HeaderContentType, common.MIMETextHTMLCharsetUTF8) template.WriteTemplate(w, template.NewPage( template.NewBaseOf(lang), + language.English, []byte(`hello, world!`), `toby3d`, )) diff --git a/web/template/baseof.qtpl b/web/template/baseof.qtpl index b920ff6..0d14367 100644 --- a/web/template/baseof.qtpl +++ b/web/template/baseof.qtpl @@ -7,6 +7,7 @@ Body() T(format message.Reference, v ...any) Title() + Lang() } %} {% code @@ -34,9 +35,15 @@ func NewBaseOf(lang language.Tag) BaseOf { {% func (b BaseOf) Body() %}{% endfunc %} +{% func (b BaseOf) Lang() %} + {% if b.language != language.Und %} + {%s b.language.String() %} + {% endif %} +{% endfunc %} + {% func Template(p Pager) %} - + diff --git a/web/template/baseof.qtpl.go b/web/template/baseof.qtpl.go index 7a71bd6..e5ff60e 100644 --- a/web/template/baseof.qtpl.go +++ b/web/template/baseof.qtpl.go @@ -43,10 +43,16 @@ type Pager interface { StreamTitle(qw422016 *qt422016.Writer) //line web/template/baseof.qtpl:6 WriteTitle(qq422016 qtio422016.Writer) +//line web/template/baseof.qtpl:6 + Lang() string +//line web/template/baseof.qtpl:6 + StreamLang(qw422016 *qt422016.Writer) +//line web/template/baseof.qtpl:6 + WriteLang(qq422016 qtio422016.Writer) //line web/template/baseof.qtpl:6 } -//line web/template/baseof.qtpl:13 +//line web/template/baseof.qtpl:14 type BaseOf struct { language language.Tag printer *message.Printer @@ -59,140 +65,181 @@ func NewBaseOf(lang language.Tag) BaseOf { } } -//line web/template/baseof.qtpl:27 +//line web/template/baseof.qtpl:28 func (b BaseOf) StreamTitle(qw422016 *qt422016.Writer) { -//line web/template/baseof.qtpl:27 +//line web/template/baseof.qtpl:28 qw422016.N().S(`toby3d`) -//line web/template/baseof.qtpl:29 +//line web/template/baseof.qtpl:30 } -//line web/template/baseof.qtpl:29 +//line web/template/baseof.qtpl:30 func (b BaseOf) WriteTitle(qq422016 qtio422016.Writer) { -//line web/template/baseof.qtpl:29 +//line web/template/baseof.qtpl:30 qw422016 := qt422016.AcquireWriter(qq422016) -//line web/template/baseof.qtpl:29 +//line web/template/baseof.qtpl:30 b.StreamTitle(qw422016) -//line web/template/baseof.qtpl:29 +//line web/template/baseof.qtpl:30 qt422016.ReleaseWriter(qw422016) -//line web/template/baseof.qtpl:29 +//line web/template/baseof.qtpl:30 } -//line web/template/baseof.qtpl:29 +//line web/template/baseof.qtpl:30 func (b BaseOf) Title() string { -//line web/template/baseof.qtpl:29 +//line web/template/baseof.qtpl:30 qb422016 := qt422016.AcquireByteBuffer() -//line web/template/baseof.qtpl:29 +//line web/template/baseof.qtpl:30 b.WriteTitle(qb422016) -//line web/template/baseof.qtpl:29 +//line web/template/baseof.qtpl:30 qs422016 := string(qb422016.B) -//line web/template/baseof.qtpl:29 +//line web/template/baseof.qtpl:30 qt422016.ReleaseByteBuffer(qb422016) -//line web/template/baseof.qtpl:29 +//line web/template/baseof.qtpl:30 return qs422016 -//line web/template/baseof.qtpl:29 +//line web/template/baseof.qtpl:30 } -//line web/template/baseof.qtpl:31 -func (b BaseOf) StreamT(qw422016 *qt422016.Writer, format message.Reference, v ...any) { //line web/template/baseof.qtpl:32 +func (b BaseOf) StreamT(qw422016 *qt422016.Writer, format message.Reference, v ...any) { +//line web/template/baseof.qtpl:33 qw422016.E().S(b.printer.Sprintf(format, v...)) -//line web/template/baseof.qtpl:33 +//line web/template/baseof.qtpl:34 } -//line web/template/baseof.qtpl:33 +//line web/template/baseof.qtpl:34 func (b BaseOf) WriteT(qq422016 qtio422016.Writer, format message.Reference, v ...any) { -//line web/template/baseof.qtpl:33 +//line web/template/baseof.qtpl:34 qw422016 := qt422016.AcquireWriter(qq422016) -//line web/template/baseof.qtpl:33 +//line web/template/baseof.qtpl:34 b.StreamT(qw422016, format, v...) -//line web/template/baseof.qtpl:33 +//line web/template/baseof.qtpl:34 qt422016.ReleaseWriter(qw422016) -//line web/template/baseof.qtpl:33 +//line web/template/baseof.qtpl:34 } -//line web/template/baseof.qtpl:33 +//line web/template/baseof.qtpl:34 func (b BaseOf) T(format message.Reference, v ...any) string { -//line web/template/baseof.qtpl:33 +//line web/template/baseof.qtpl:34 qb422016 := qt422016.AcquireByteBuffer() -//line web/template/baseof.qtpl:33 +//line web/template/baseof.qtpl:34 b.WriteT(qb422016, format, v...) -//line web/template/baseof.qtpl:33 +//line web/template/baseof.qtpl:34 qs422016 := string(qb422016.B) -//line web/template/baseof.qtpl:33 +//line web/template/baseof.qtpl:34 qt422016.ReleaseByteBuffer(qb422016) -//line web/template/baseof.qtpl:33 +//line web/template/baseof.qtpl:34 return qs422016 -//line web/template/baseof.qtpl:33 +//line web/template/baseof.qtpl:34 } -//line web/template/baseof.qtpl:35 +//line web/template/baseof.qtpl:36 func (b BaseOf) StreamBody(qw422016 *qt422016.Writer) { -//line web/template/baseof.qtpl:35 +//line web/template/baseof.qtpl:36 } -//line web/template/baseof.qtpl:35 +//line web/template/baseof.qtpl:36 func (b BaseOf) WriteBody(qq422016 qtio422016.Writer) { -//line web/template/baseof.qtpl:35 +//line web/template/baseof.qtpl:36 qw422016 := qt422016.AcquireWriter(qq422016) -//line web/template/baseof.qtpl:35 +//line web/template/baseof.qtpl:36 b.StreamBody(qw422016) -//line web/template/baseof.qtpl:35 +//line web/template/baseof.qtpl:36 qt422016.ReleaseWriter(qw422016) -//line web/template/baseof.qtpl:35 +//line web/template/baseof.qtpl:36 } -//line web/template/baseof.qtpl:35 +//line web/template/baseof.qtpl:36 func (b BaseOf) Body() string { -//line web/template/baseof.qtpl:35 +//line web/template/baseof.qtpl:36 qb422016 := qt422016.AcquireByteBuffer() -//line web/template/baseof.qtpl:35 +//line web/template/baseof.qtpl:36 b.WriteBody(qb422016) -//line web/template/baseof.qtpl:35 +//line web/template/baseof.qtpl:36 qs422016 := string(qb422016.B) -//line web/template/baseof.qtpl:35 +//line web/template/baseof.qtpl:36 qt422016.ReleaseByteBuffer(qb422016) -//line web/template/baseof.qtpl:35 +//line web/template/baseof.qtpl:36 return qs422016 -//line web/template/baseof.qtpl:35 +//line web/template/baseof.qtpl:36 } -//line web/template/baseof.qtpl:37 -func StreamTemplate(qw422016 *qt422016.Writer, p Pager) { -//line web/template/baseof.qtpl:37 - qw422016.N().S(``) -//line web/template/baseof.qtpl:44 - p.StreamTitle(qw422016) -//line web/template/baseof.qtpl:44 - qw422016.N().S(``) -//line web/template/baseof.qtpl:48 - p.StreamBody(qw422016) -//line web/template/baseof.qtpl:48 - qw422016.N().S(``) -//line web/template/baseof.qtpl:51 +//line web/template/baseof.qtpl:38 +func (b BaseOf) StreamLang(qw422016 *qt422016.Writer) { +//line web/template/baseof.qtpl:39 + if b.language != language.Und { +//line web/template/baseof.qtpl:40 + qw422016.E().S(b.language.String()) +//line web/template/baseof.qtpl:41 + } +//line web/template/baseof.qtpl:42 } -//line web/template/baseof.qtpl:51 -func WriteTemplate(qq422016 qtio422016.Writer, p Pager) { -//line web/template/baseof.qtpl:51 +//line web/template/baseof.qtpl:42 +func (b BaseOf) WriteLang(qq422016 qtio422016.Writer) { +//line web/template/baseof.qtpl:42 qw422016 := qt422016.AcquireWriter(qq422016) -//line web/template/baseof.qtpl:51 - StreamTemplate(qw422016, p) -//line web/template/baseof.qtpl:51 +//line web/template/baseof.qtpl:42 + b.StreamLang(qw422016) +//line web/template/baseof.qtpl:42 qt422016.ReleaseWriter(qw422016) -//line web/template/baseof.qtpl:51 +//line web/template/baseof.qtpl:42 } -//line web/template/baseof.qtpl:51 -func Template(p Pager) string { -//line web/template/baseof.qtpl:51 +//line web/template/baseof.qtpl:42 +func (b BaseOf) Lang() string { +//line web/template/baseof.qtpl:42 qb422016 := qt422016.AcquireByteBuffer() -//line web/template/baseof.qtpl:51 - WriteTemplate(qb422016, p) -//line web/template/baseof.qtpl:51 +//line web/template/baseof.qtpl:42 + b.WriteLang(qb422016) +//line web/template/baseof.qtpl:42 qs422016 := string(qb422016.B) -//line web/template/baseof.qtpl:51 +//line web/template/baseof.qtpl:42 qt422016.ReleaseByteBuffer(qb422016) -//line web/template/baseof.qtpl:51 +//line web/template/baseof.qtpl:42 return qs422016 -//line web/template/baseof.qtpl:51 +//line web/template/baseof.qtpl:42 +} + +//line web/template/baseof.qtpl:44 +func StreamTemplate(qw422016 *qt422016.Writer, p Pager) { +//line web/template/baseof.qtpl:44 + qw422016.N().S(``) +//line web/template/baseof.qtpl:51 + p.StreamTitle(qw422016) +//line web/template/baseof.qtpl:51 + qw422016.N().S(``) +//line web/template/baseof.qtpl:55 + p.StreamBody(qw422016) +//line web/template/baseof.qtpl:55 + qw422016.N().S(``) +//line web/template/baseof.qtpl:58 +} + +//line web/template/baseof.qtpl:58 +func WriteTemplate(qq422016 qtio422016.Writer, p Pager) { +//line web/template/baseof.qtpl:58 + qw422016 := qt422016.AcquireWriter(qq422016) +//line web/template/baseof.qtpl:58 + StreamTemplate(qw422016, p) +//line web/template/baseof.qtpl:58 + qt422016.ReleaseWriter(qw422016) +//line web/template/baseof.qtpl:58 +} + +//line web/template/baseof.qtpl:58 +func Template(p Pager) string { +//line web/template/baseof.qtpl:58 + qb422016 := qt422016.AcquireByteBuffer() +//line web/template/baseof.qtpl:58 + WriteTemplate(qb422016, p) +//line web/template/baseof.qtpl:58 + qs422016 := string(qb422016.B) +//line web/template/baseof.qtpl:58 + qt422016.ReleaseByteBuffer(qb422016) +//line web/template/baseof.qtpl:58 + return qs422016 +//line web/template/baseof.qtpl:58 } diff --git a/web/template/page.qtpl b/web/template/page.qtpl index 60edbb8..c95e62f 100644 --- a/web/template/page.qtpl +++ b/web/template/page.qtpl @@ -1,13 +1,19 @@ +{% import ( + "golang.org/x/text/language" +) %} + {% code type Page struct { BaseOf + language language.Tag title string content []byte } -func NewPage(base BaseOf, content []byte, title string) Page { +func NewPage(base BaseOf, lang language.Tag, content []byte, title string) Page { return Page{ BaseOf: base, + language: lang, title: title, content: content, } @@ -23,7 +29,15 @@ func NewPage(base BaseOf, content []byte, title string) Page { {% endif %} {% endfunc %} +{% func (p Page) Lang() %} + {% if p.language != language.Und %} + {%s p.language.String() %} + {% else %} + {%= p.BaseOf.Lang() %} + {% endif %} +{% endfunc %} + {% func (p Page) Body() %} - {%z p.content %} +

{%z p.content %}

{% endfunc %} {% endstripspace %} diff --git a/web/template/page.qtpl.go b/web/template/page.qtpl.go index 8e7ef67..c802e16 100644 --- a/web/template/page.qtpl.go +++ b/web/template/page.qtpl.go @@ -5,103 +5,155 @@ package template //line web/template/page.qtpl:1 +import ( + "golang.org/x/text/language" +) + +//line web/template/page.qtpl:5 import ( qtio422016 "io" qt422016 "github.com/valyala/quicktemplate" ) -//line web/template/page.qtpl:1 +//line web/template/page.qtpl:5 var ( _ = qtio422016.Copy _ = qt422016.AcquireByteBuffer ) -//line web/template/page.qtpl:2 +//line web/template/page.qtpl:6 type Page struct { BaseOf - title string - content []byte + language language.Tag + title string + content []byte } -func NewPage(base BaseOf, content []byte, title string) Page { +func NewPage(base BaseOf, lang language.Tag, content []byte, title string) Page { return Page{ - BaseOf: base, - title: title, - content: content, + BaseOf: base, + language: lang, + title: title, + content: content, } } -//line web/template/page.qtpl:18 +//line web/template/page.qtpl:24 func (p Page) StreamTitle(qw422016 *qt422016.Writer) { -//line web/template/page.qtpl:19 +//line web/template/page.qtpl:25 if p.title != "" { -//line web/template/page.qtpl:20 - qw422016.E().S(p.title) -//line web/template/page.qtpl:21 - } else { -//line web/template/page.qtpl:22 - p.BaseOf.StreamTitle(qw422016) -//line web/template/page.qtpl:23 - } -//line web/template/page.qtpl:24 -} - -//line web/template/page.qtpl:24 -func (p Page) WriteTitle(qq422016 qtio422016.Writer) { -//line web/template/page.qtpl:24 - qw422016 := qt422016.AcquireWriter(qq422016) -//line web/template/page.qtpl:24 - p.StreamTitle(qw422016) -//line web/template/page.qtpl:24 - qt422016.ReleaseWriter(qw422016) -//line web/template/page.qtpl:24 -} - -//line web/template/page.qtpl:24 -func (p Page) Title() string { -//line web/template/page.qtpl:24 - qb422016 := qt422016.AcquireByteBuffer() -//line web/template/page.qtpl:24 - p.WriteTitle(qb422016) -//line web/template/page.qtpl:24 - qs422016 := string(qb422016.B) -//line web/template/page.qtpl:24 - qt422016.ReleaseByteBuffer(qb422016) -//line web/template/page.qtpl:24 - return qs422016 -//line web/template/page.qtpl:24 -} - //line web/template/page.qtpl:26 -func (p Page) StreamBody(qw422016 *qt422016.Writer) { + qw422016.E().S(p.title) //line web/template/page.qtpl:27 - qw422016.E().Z(p.content) + } else { //line web/template/page.qtpl:28 + p.BaseOf.StreamTitle(qw422016) +//line web/template/page.qtpl:29 + } +//line web/template/page.qtpl:30 } -//line web/template/page.qtpl:28 -func (p Page) WriteBody(qq422016 qtio422016.Writer) { -//line web/template/page.qtpl:28 +//line web/template/page.qtpl:30 +func (p Page) WriteTitle(qq422016 qtio422016.Writer) { +//line web/template/page.qtpl:30 qw422016 := qt422016.AcquireWriter(qq422016) -//line web/template/page.qtpl:28 - p.StreamBody(qw422016) -//line web/template/page.qtpl:28 +//line web/template/page.qtpl:30 + p.StreamTitle(qw422016) +//line web/template/page.qtpl:30 qt422016.ReleaseWriter(qw422016) -//line web/template/page.qtpl:28 +//line web/template/page.qtpl:30 } -//line web/template/page.qtpl:28 -func (p Page) Body() string { -//line web/template/page.qtpl:28 +//line web/template/page.qtpl:30 +func (p Page) Title() string { +//line web/template/page.qtpl:30 qb422016 := qt422016.AcquireByteBuffer() -//line web/template/page.qtpl:28 - p.WriteBody(qb422016) -//line web/template/page.qtpl:28 +//line web/template/page.qtpl:30 + p.WriteTitle(qb422016) +//line web/template/page.qtpl:30 qs422016 := string(qb422016.B) -//line web/template/page.qtpl:28 +//line web/template/page.qtpl:30 qt422016.ReleaseByteBuffer(qb422016) -//line web/template/page.qtpl:28 +//line web/template/page.qtpl:30 return qs422016 -//line web/template/page.qtpl:28 +//line web/template/page.qtpl:30 +} + +//line web/template/page.qtpl:32 +func (p Page) StreamLang(qw422016 *qt422016.Writer) { +//line web/template/page.qtpl:33 + if p.language != language.Und { +//line web/template/page.qtpl:34 + qw422016.E().S(p.language.String()) +//line web/template/page.qtpl:35 + } else { +//line web/template/page.qtpl:36 + p.BaseOf.StreamLang(qw422016) +//line web/template/page.qtpl:37 + } +//line web/template/page.qtpl:38 +} + +//line web/template/page.qtpl:38 +func (p Page) WriteLang(qq422016 qtio422016.Writer) { +//line web/template/page.qtpl:38 + qw422016 := qt422016.AcquireWriter(qq422016) +//line web/template/page.qtpl:38 + p.StreamLang(qw422016) +//line web/template/page.qtpl:38 + qt422016.ReleaseWriter(qw422016) +//line web/template/page.qtpl:38 +} + +//line web/template/page.qtpl:38 +func (p Page) Lang() string { +//line web/template/page.qtpl:38 + qb422016 := qt422016.AcquireByteBuffer() +//line web/template/page.qtpl:38 + p.WriteLang(qb422016) +//line web/template/page.qtpl:38 + qs422016 := string(qb422016.B) +//line web/template/page.qtpl:38 + qt422016.ReleaseByteBuffer(qb422016) +//line web/template/page.qtpl:38 + return qs422016 +//line web/template/page.qtpl:38 +} + +//line web/template/page.qtpl:40 +func (p Page) StreamBody(qw422016 *qt422016.Writer) { +//line web/template/page.qtpl:40 + qw422016.N().S(`

`) +//line web/template/page.qtpl:41 + qw422016.E().Z(p.content) +//line web/template/page.qtpl:41 + qw422016.N().S(`

`) +//line web/template/page.qtpl:42 +} + +//line web/template/page.qtpl:42 +func (p Page) WriteBody(qq422016 qtio422016.Writer) { +//line web/template/page.qtpl:42 + qw422016 := qt422016.AcquireWriter(qq422016) +//line web/template/page.qtpl:42 + p.StreamBody(qw422016) +//line web/template/page.qtpl:42 + qt422016.ReleaseWriter(qw422016) +//line web/template/page.qtpl:42 +} + +//line web/template/page.qtpl:42 +func (p Page) Body() string { +//line web/template/page.qtpl:42 + qb422016 := qt422016.AcquireByteBuffer() +//line web/template/page.qtpl:42 + p.WriteBody(qb422016) +//line web/template/page.qtpl:42 + qs422016 := string(qb422016.B) +//line web/template/page.qtpl:42 + qt422016.ReleaseByteBuffer(qb422016) +//line web/template/page.qtpl:42 + return qs422016 +//line web/template/page.qtpl:42 }