Added basic i18n
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
i18n "github.com/tgckpg/golifehk/i18n"
|
||||
query "github.com/tgckpg/golifehk/query"
|
||||
utils "github.com/tgckpg/golifehk/utils"
|
||||
)
|
||||
@@ -20,6 +21,9 @@ type QueryResult struct {
|
||||
isConsumed bool
|
||||
dataType string
|
||||
tableData [][]query.TableCell
|
||||
|
||||
FallbackNearest int
|
||||
NearestRange float64
|
||||
}
|
||||
|
||||
func writeShortRoute(lang *string, sb *strings.Builder, b *BusStop) {
|
||||
@@ -52,6 +56,11 @@ func (this *QueryResult) Message() (string, error) {
|
||||
return "", this.Error
|
||||
}
|
||||
|
||||
langPack, err := i18n.LoadKeys(this.Lang)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sb := strings.Builder{}
|
||||
|
||||
if this.Schedules == nil {
|
||||
@@ -72,33 +81,83 @@ func (this *QueryResult) Message() (string, error) {
|
||||
if loc != nil {
|
||||
|
||||
this.dataType = "Table"
|
||||
sb.WriteString("K巴 100m")
|
||||
|
||||
table := [][]query.TableCell{}
|
||||
|
||||
// Group by Station Name first
|
||||
bGroups := map[string]*[]*BusStop{}
|
||||
|
||||
for _, entry := range *q.Results {
|
||||
busStop := any(entry).(*BusStop)
|
||||
|
||||
sb_i := strings.Builder{}
|
||||
sb_i.WriteString(fmt.Sprintf("%.2fm", busStop.Dist(loc.Lat(), loc.Lon())))
|
||||
sb_i.WriteString(" ")
|
||||
utils.WriteMDv2Text(&sb_i, busStop.RouteId)
|
||||
d := busStop.Direction
|
||||
if d == "O" {
|
||||
sb_i.WriteString("↑")
|
||||
} else if d == "I" {
|
||||
sb_i.WriteString("↓")
|
||||
} else {
|
||||
sb_i.WriteString("\\?")
|
||||
bName := (*busStop.Name)[this.Lang]
|
||||
bGroup, ok := bGroups[bName]
|
||||
if !ok {
|
||||
bGroup = &[]*BusStop{}
|
||||
bGroups[bName] = bGroup
|
||||
}
|
||||
sb_i.WriteString(" ")
|
||||
utils.WriteMDv2Text(&sb_i, (*busStop.Name)[this.Lang])
|
||||
*bGroup = append(*bGroup, busStop)
|
||||
}
|
||||
|
||||
for bName, bGroup := range bGroups {
|
||||
|
||||
row := []query.TableCell{
|
||||
query.TableCell{
|
||||
query.TableCell{Name: bName, Value: bName},
|
||||
}
|
||||
gRow := row
|
||||
|
||||
var minDist float64
|
||||
var maxDist float64
|
||||
|
||||
for colIndex, busStop := range *bGroup {
|
||||
if colIndex%6 == 0 {
|
||||
table = append(table, row)
|
||||
row = []query.TableCell{}
|
||||
}
|
||||
|
||||
sb_i := strings.Builder{}
|
||||
sb_i.WriteString(busStop.RouteId)
|
||||
d := busStop.Direction
|
||||
if d == "O" {
|
||||
sb_i.WriteString("↑")
|
||||
} else if d == "I" {
|
||||
sb_i.WriteString("↓")
|
||||
} else {
|
||||
sb_i.WriteString("\\?")
|
||||
}
|
||||
|
||||
cell := query.TableCell{
|
||||
Name: sb_i.String(),
|
||||
Value: fmt.Sprintf("%s %s", busStop.RouteId, (*busStop.Name)[this.Lang]),
|
||||
},
|
||||
Value: fmt.Sprintf("%s %s", busStop.RouteId, bName),
|
||||
}
|
||||
|
||||
// Data are already sorted by shortest dist
|
||||
// So the first one must be min dist
|
||||
if minDist == 0 {
|
||||
minDist = busStop.Dist(loc.Lat(), loc.Lon())
|
||||
}
|
||||
|
||||
if colIndex+1 == len(*bGroup) {
|
||||
maxDist = busStop.Dist(loc.Lat(), loc.Lon())
|
||||
}
|
||||
|
||||
row = append(row, cell)
|
||||
}
|
||||
|
||||
if minDist == maxDist {
|
||||
gRow[0].Name = fmt.Sprintf("%s (%s)", bName, i18n.FormatDistance(langPack, minDist))
|
||||
} else {
|
||||
gRow[0].Name = fmt.Sprintf(
|
||||
"%s (%s~%s)",
|
||||
bName, i18n.FormatDistance(langPack, minDist),
|
||||
bName, i18n.FormatDistance(langPack, maxDist),
|
||||
)
|
||||
}
|
||||
|
||||
rangeText := i18n.FormatDistance(langPack, this.NearestRange)
|
||||
if maxDist < this.NearestRange {
|
||||
utils.WriteMDv2Text(&sb, i18n.DS_MTR_NEAREST_STOPS.Text(langPack, rangeText))
|
||||
} else if this.NearestRange < minDist {
|
||||
utils.WriteMDv2Text(&sb, i18n.DS_MTR_NO_NEAREST_STOPS.Text(langPack, rangeText, this.FallbackNearest))
|
||||
}
|
||||
|
||||
table = append(table, row)
|
||||
@@ -235,7 +294,7 @@ func (this *QueryResult) Message() (string, error) {
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
} else {
|
||||
utils.WriteMDv2Text(&sb, "Schedules are empty...perhaps Out of Service Time?")
|
||||
utils.WriteMDv2Text(&sb, i18n.DS_MTR_NO_SCHEDULES.Text(langPack))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user