Added basic i18n
This commit is contained in:
@@ -2,10 +2,12 @@ package kmb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
i18n "github.com/tgckpg/golifehk/i18n"
|
||||
query "github.com/tgckpg/golifehk/query"
|
||||
utils "github.com/tgckpg/golifehk/utils"
|
||||
)
|
||||
@@ -19,6 +21,9 @@ type QueryResult struct {
|
||||
isConsumed bool
|
||||
dataType string
|
||||
tableData [][]query.TableCell
|
||||
|
||||
FallbackNearest int
|
||||
NearestRange float64
|
||||
}
|
||||
|
||||
func writeRouteHead(sb *strings.Builder, r *RouteStop) {
|
||||
@@ -64,6 +69,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 0 < len(*this.Query.Results) {
|
||||
@@ -72,23 +82,38 @@ func (this *QueryResult) Message() (string, error) {
|
||||
if this.Query.Key == "" {
|
||||
loc := this.Query.Message.Location
|
||||
if loc != nil {
|
||||
sb.WriteString("九巴 100m")
|
||||
|
||||
this.dataType = "Table"
|
||||
|
||||
table := [][]query.TableCell{}
|
||||
minDist := math.MaxFloat64
|
||||
maxDist := -1.0
|
||||
|
||||
for _, item := range *this.Query.Results {
|
||||
b := any(item).(*BusStop)
|
||||
|
||||
row := []query.TableCell{}
|
||||
|
||||
bDist := b.Dist(loc.Lat(), loc.Lon())
|
||||
if bDist < minDist {
|
||||
minDist = bDist
|
||||
}
|
||||
|
||||
if maxDist < bDist {
|
||||
maxDist = bDist
|
||||
}
|
||||
|
||||
cell := query.TableCell{
|
||||
Name: fmt.Sprintf("%.2fm %s", b.Dist(loc.Lat(), loc.Lon()), (*b.Name)[this.Lang]),
|
||||
Name: fmt.Sprintf("%s (%s)", (*b.Name)[this.Lang], i18n.FormatDistance(langPack, bDist)),
|
||||
Value: fmt.Sprintf("%s", b.BusStopId),
|
||||
}
|
||||
row = append(row, cell)
|
||||
|
||||
for _, r := range *b.Routes {
|
||||
for colIndex, r := range *b.Routes {
|
||||
if colIndex%6 == 0 {
|
||||
table = append(table, row)
|
||||
row = []query.TableCell{}
|
||||
}
|
||||
sb_i := strings.Builder{}
|
||||
writeRouteHead(&sb_i, r)
|
||||
cell := query.TableCell{
|
||||
@@ -102,6 +127,13 @@ func (this *QueryResult) Message() (string, error) {
|
||||
}
|
||||
this.tableData = table
|
||||
|
||||
rangeText := i18n.FormatDistance(langPack, this.NearestRange)
|
||||
if maxDist < this.NearestRange {
|
||||
utils.WriteMDv2Text(&sb, i18n.DS_KMB_NEAREST_STOPS.Text(langPack, rangeText))
|
||||
} else if this.NearestRange < minDist {
|
||||
utils.WriteMDv2Text(&sb, i18n.DS_KMB_NO_NEAREST_STOPS.Text(langPack, rangeText, this.FallbackNearest))
|
||||
}
|
||||
|
||||
} else {
|
||||
busStops := map[string]*BusStop{}
|
||||
for _, item := range *this.Query.Results {
|
||||
@@ -149,16 +181,13 @@ func (this *QueryResult) Message() (string, error) {
|
||||
_m := schedule.ETA.Sub(now).Minutes()
|
||||
|
||||
sb.WriteString(" \\* ")
|
||||
txt := "%.0f min(s)"
|
||||
|
||||
if this.Lang == "zh-Hant" {
|
||||
txt = "%.0f 分鐘"
|
||||
}
|
||||
|
||||
utils.WriteMDv2Text(&sb, fmt.Sprintf(txt, _m))
|
||||
eta := i18n.UNITS_MINUTE.Text(langPack, _m)
|
||||
|
||||
if _m < 0 {
|
||||
sb.WriteString(" 走左了?")
|
||||
utils.WriteMDv2Text(&sb, i18n.DS_KMB_ETA_DEPARTED.Text(langPack, eta))
|
||||
} else {
|
||||
utils.WriteMDv2Text(&sb, eta)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user