Skip to content

Commit d3c266c

Browse files
committed
add localizations
1 parent 3232ef9 commit d3c266c

File tree

16 files changed

+219
-117
lines changed

16 files changed

+219
-117
lines changed

server/activity.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,10 @@
11
package server
22

33
import (
4-
"net/http"
5-
"strings"
64
"time"
75
)
86

97
type Activity interface {
108
ActivityTime() time.Time
119
ActivityKey() uint32
1210
}
13-
14-
func ActivityFilterForRequest(r *http.Request) func(Activity) bool {
15-
subdomain := ""
16-
if r.Host != "" {
17-
subdomain = strings.Split(r.Host, ".")[0]
18-
}
19-
20-
includeReddit := false
21-
includeForumHost := map[string]bool{}
22-
23-
switch subdomain {
24-
case "br":
25-
includeForumHost["br.pathofexile.com"] = true
26-
case "ru":
27-
includeForumHost["ru.pathofexile.com"] = true
28-
case "th":
29-
includeForumHost["th.pathofexile.com"] = true
30-
case "de":
31-
includeForumHost["de.pathofexile.com"] = true
32-
case "fr":
33-
includeForumHost["fr.pathofexile.com"] = true
34-
case "es":
35-
includeForumHost["es.pathofexile.com"] = true
36-
default:
37-
includeReddit = true
38-
includeForumHost["www.pathofexile.com"] = true
39-
}
40-
41-
return func(a Activity) bool {
42-
switch a := a.(type) {
43-
case *ForumPost:
44-
return includeForumHost[a.Host]
45-
case *RedditComment:
46-
return includeReddit
47-
case *RedditPost:
48-
return includeReddit
49-
}
50-
return false
51-
}
52-
}

server/activity_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type jsonResponse struct {
1616

1717
func ActivityHandler(db *Database) echo.HandlerFunc {
1818
return func(c echo.Context) error {
19-
activity, next := db.Activity(c.QueryParam("next"), 50, ActivityFilterForRequest(c.Request()))
19+
activity, next := db.Activity(c.QueryParam("next"), 50, LocaleForRequest(c.Request()).ActivityFilter)
2020
response := jsonResponse{
2121
Next: next,
2222
}

server/common.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package server
22

33
import (
4+
"strings"
5+
46
"github.com/labstack/echo"
57
)
68

@@ -14,3 +16,16 @@ func AbsoluteURL(c echo.Context, resource string) string {
1416
}
1517
return scheme + "://" + c.Request().Host + resource
1618
}
19+
20+
func SubdomainURL(c echo.Context, subdomain string) string {
21+
scheme := c.Scheme()
22+
if c.Request().Header.Get(echo.HeaderXForwardedProto) == "https" {
23+
scheme = "https"
24+
}
25+
locale := LocaleForRequest(c.Request())
26+
host := strings.TrimPrefix(c.Request().Host, locale.Subdomain+".")
27+
if subdomain != "" {
28+
host = subdomain + "." + host
29+
}
30+
return scheme + "://" + host
31+
}

server/forum_indexer.go

Lines changed: 15 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,6 @@ func (indexer *ForumIndexer) Close() {
4343
func (indexer *ForumIndexer) run() {
4444
log.Info("starting forum indexer")
4545

46-
hosts := []string{
47-
"www.pathofexile.com",
48-
"br.pathofexile.com",
49-
"ru.pathofexile.com",
50-
"th.pathofexile.com",
51-
"de.pathofexile.com",
52-
"fr.pathofexile.com",
53-
"es.pathofexile.com",
54-
}
55-
5646
accounts := []string{
5747
"Chris", "Jonathan", "Erik", "Mark_GGG", "Samantha", "Rory", "Rhys", "Qarl", "Andrew_GGG",
5848
"Damien_GGG", "Joel_GGG", "Ari", "Thomas", "BrianWeissman", "Edwin_GGG", "Support", "Dylan",
@@ -84,18 +74,18 @@ func (indexer *ForumIndexer) run() {
8474
}
8575

8676
var wg sync.WaitGroup
87-
wg.Add(len(hosts))
77+
wg.Add(len(Locales))
8878

89-
for _, host := range hosts {
90-
host := host
79+
for _, l := range Locales {
80+
l := l
9181
go func() {
9282
for {
9383
for _, account := range accounts {
9484
select {
9585
case <-indexer.closeSignal:
9686
return
9787
default:
98-
indexer.index(host, account, timezone)
88+
indexer.index(l, account, timezone)
9989
time.Sleep(time.Second)
10090
}
10191
}
@@ -135,34 +125,7 @@ var postURLExpression = regexp.MustCompile("^/forum/view-thread/([0-9]+)/page/([
135125
var threadURLExpression = regexp.MustCompile("^/forum/view-thread/([0-9]+)")
136126
var forumURLExpression = regexp.MustCompile("^/forum/view-forum/([0-9]+)")
137127

138-
var monthReplacer = strings.NewReplacer(
139-
"ม.ค.", "Jan",
140-
"ก.พ.", "Feb",
141-
"มี.ค.", "Mar",
142-
"เม.ย.", "Apr",
143-
"พ.ค.", "May",
144-
"มิ.ย.", "Jun",
145-
"ก.ค.", "Jul",
146-
"ส.ค.", "Aug",
147-
"ก.ย.", "Sep",
148-
"ต.ค.", "Oct",
149-
"พ.ย.", "Nov",
150-
"ธ.ค.", "Dec",
151-
"janv.", "Jan",
152-
"févr.", "Feb",
153-
"mars", "Mar",
154-
"avril", "Apr",
155-
"mai", "May",
156-
"juin", "Jun",
157-
"juil.", "Jul",
158-
"août", "Aug",
159-
"sept.", "Sep",
160-
"oct.", "Oct",
161-
"nov.", "Nov",
162-
"déc.", "Dec",
163-
)
164-
165-
func ScrapeForumPosts(doc *goquery.Document, timezone *time.Location) ([]*ForumPost, error) {
128+
func ScrapeForumPosts(doc *goquery.Document, locale *Locale, timezone *time.Location) ([]*ForumPost, error) {
166129
posts := []*ForumPost(nil)
167130

168131
err := error(nil)
@@ -178,21 +141,9 @@ func ScrapeForumPosts(doc *goquery.Document, timezone *time.Location) ([]*ForumP
178141
}
179142
post.BodyHTML = body
180143

181-
timeText := monthReplacer.Replace(sel.Find(".post_date").Text())
182-
183-
for _, format := range []string{
184-
"Jan _2, 2006 3:04:05 PM",
185-
"2/1/2006 15:04:05",
186-
"2.1.2006 15:04:05",
187-
"_2 Jan 2006, 15:04:05",
188-
"_2 Jan 2006 15:04:05",
189-
} {
190-
if t, err := time.ParseInLocation(format, timeText, timezone); err == nil {
191-
post.Time = t
192-
break
193-
}
194-
}
195-
if post.Time.IsZero() {
144+
timeText := sel.Find(".post_date").Text()
145+
146+
if post.Time, err = locale.ParseTime(timeText, timezone); err != nil {
196147
log.WithField("text", timeText).Error("unable to parse time")
197148
return false
198149
}
@@ -226,32 +177,32 @@ func ScrapeForumPosts(doc *goquery.Document, timezone *time.Location) ([]*ForumP
226177
return posts, nil
227178
}
228179

229-
func (indexer *ForumIndexer) forumPosts(host, poster string, page int, timezone *time.Location) ([]*ForumPost, error) {
230-
doc, err := indexer.requestDocument(host, fmt.Sprintf("/account/view-posts/%v/page/%v", poster, page))
180+
func (indexer *ForumIndexer) forumPosts(locale *Locale, poster string, page int, timezone *time.Location) ([]*ForumPost, error) {
181+
doc, err := indexer.requestDocument(locale.ForumHost(), fmt.Sprintf("/account/view-posts/%v/page/%v", poster, page))
231182
if err != nil {
232183
return nil, err
233184
}
234-
posts, err := ScrapeForumPosts(doc, timezone)
185+
posts, err := ScrapeForumPosts(doc, locale, timezone)
235186
if err != nil {
236187
return nil, err
237188
}
238189
for _, post := range posts {
239-
post.Host = host
190+
post.Host = locale.ForumHost()
240191
}
241192
return posts, nil
242193
}
243194

244-
func (indexer *ForumIndexer) index(host, poster string, timezone *time.Location) {
195+
func (indexer *ForumIndexer) index(locale *Locale, poster string, timezone *time.Location) {
245196
logger := log.WithFields(log.Fields{
246-
"host": host,
197+
"host": locale.ForumHost(),
247198
"poster": poster,
248199
})
249200

250201
cutoff := time.Now().Add(time.Hour * -12)
251202
activity := []Activity(nil)
252203

253204
for page := 1; ; page++ {
254-
posts, err := indexer.forumPosts(host, poster, page, timezone)
205+
posts, err := indexer.forumPosts(locale, poster, page, timezone)
255206
if err != nil {
256207
logger.WithError(err).Error("error requesting forum posts")
257208
}

server/forum_indexer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestScrapeForumPosts(t *testing.T) {
2121
tz, err := time.LoadLocation("America/Los_Angeles")
2222
require.NoError(t, err)
2323

24-
posts, err := ScrapeForumPosts(doc, tz)
24+
posts, err := ScrapeForumPosts(doc, Locales[0], tz)
2525
require.NoError(t, err)
2626
assert.Equal(t, 10, len(posts))
2727

server/index_handler.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var index = `<!DOCTYPE html><html>
1111
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
1212
<title>GGG Tracker</title>
1313
<link rel="shortcut icon" href="static/favicon.ico" />
14-
<link rel="stylesheet" type="text/css" href="static/style.css?v4" />
14+
<link rel="stylesheet" type="text/css" href="static/style.css?v5" />
1515
<link rel="alternate" type="application/rss+xml" title="GGG Tracker Forum Feed" href="rss" />
1616
1717
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
@@ -34,19 +34,24 @@ var index = `<!DOCTYPE html><html>
3434
<div class="container">
3535
<header>
3636
<a href="/"><img src="static/images/ggg-dark.png" /></a>
37+
<ul id="locale-selection">
38+
{{range .Locales}}
39+
<li{{if eq .Subdomain $.Locale.Subdomain}} class="selected-locale"{{end}}><a href="{{call $.SubdomainURL .Subdomain}}"><img src="{{.Image}}" /></a></li>
40+
{{end}}
41+
</ul>
3742
</header>
3843
<div class="content-box">
39-
<h1>Activity</h1>
44+
<h1>{{call $.Translate "Activity"}}</h1>
4045
<a href="rss"><img src="static/images/rss-icon-28.png" class="rss-icon" /></a>
4146
<table id="activity-table" class="list">
4247
<thead>
4348
<tr>
4449
<th></th>
4550
<th></th>
46-
<th>Thread</th>
47-
<th>Poster</th>
48-
<th>Time</th>
49-
<th>Forum</th>
51+
<th>{{call $.Translate "Thread"}}</th>
52+
<th>{{call $.Translate "Poster"}}</th>
53+
<th>{{call $.Translate "Time"}}</th>
54+
<th>{{call $.Translate "Forum"}}</th>
5055
</tr>
5156
</thead>
5257
<tbody>
@@ -71,16 +76,33 @@ type IndexConfiguration struct {
7176
GoogleAnalytics string
7277
}
7378

74-
func IndexHandler(configuration IndexConfiguration) echo.HandlerFunc {
79+
var indexTemplate *template.Template
80+
81+
func init() {
7582
t, err := template.New("index").Parse(index)
7683
if err != nil {
7784
panic(err)
7885
}
86+
indexTemplate = t
87+
}
88+
89+
func IndexHandler(configuration IndexConfiguration) echo.HandlerFunc {
7990
return func(c echo.Context) error {
80-
return t.Execute(c.Response(), struct {
91+
locale := LocaleForRequest(c.Request())
92+
return indexTemplate.Execute(c.Response(), struct {
8193
Configuration IndexConfiguration
94+
Locales []*Locale
95+
Locale *Locale
96+
Translate func(string) string
97+
SubdomainURL func(string) string
8298
}{
8399
Configuration: configuration,
100+
Locales: Locales,
101+
Locale: locale,
102+
Translate: locale.Translate,
103+
SubdomainURL: func(subdomain string) string {
104+
return SubdomainURL(c, subdomain)
105+
},
84106
})
85107
}
86108
}

0 commit comments

Comments
 (0)