From 0219a6302b65365f1d86ff9c166334b58e89d661 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Wed, 26 May 2021 17:51:31 +0200 Subject: [PATCH] Geo title based on blog language --- reverseGeo.go | 19 ++++++++++++++----- templates/postmeta.gohtml | 3 ++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/reverseGeo.go b/reverseGeo.go index 48a6311..579e29f 100644 --- a/reverseGeo.go +++ b/reverseGeo.go @@ -5,14 +5,15 @@ import ( "io" "io/ioutil" "net/http" + "net/url" "strings" geojson "github.com/paulmach/go.geojson" "github.com/thoas/go-funk" ) -func geoTitle(lat, lon float64) string { - ba, err := photonReverse(lat, lon) +func geoTitle(lat, lon float64, lang string) string { + ba, err := photonReverse(lat, lon, lang) if err != nil { return "" } @@ -28,13 +29,21 @@ func geoTitle(lat, lon float64) string { return strings.Join(funk.FilterString([]string{name, city, state, country}, func(s string) bool { return s != "" }), ", ") } -func photonReverse(lat, lon float64) ([]byte, error) { - cacheKey := fmt.Sprintf("photon-%v-%v", lat, lon) +func photonReverse(lat, lon float64, lang string) ([]byte, error) { + cacheKey := fmt.Sprintf("photon-%v-%v-%v", lat, lon, lang) cache, _ := retrievePersistentCache(cacheKey) if cache != nil { return cache, nil } - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://photon.komoot.io/reverse?lat=%v&lon=%v", lat, lon), nil) + uv := url.Values{} + uv.Set("lat", fmt.Sprintf("%v", lat)) + uv.Set("lon", fmt.Sprintf("%v", lon)) + if lang == "de" || lang == "fr" || lang == "it" { + uv.Set("lang", lang) + } else { + uv.Set("lang", "en") + } + req, err := http.NewRequest(http.MethodGet, "https://photon.komoot.io/reverse?"+uv.Encode(), nil) if err != nil { return nil, err } diff --git a/templates/postmeta.gohtml b/templates/postmeta.gohtml index 96beb07..57665ef 100644 --- a/templates/postmeta.gohtml +++ b/templates/postmeta.gohtml @@ -1,11 +1,12 @@ {{ define "postmeta" }}
{{ include "summaryandpostmeta" . }} + {{ $bloglang := .Blog.Lang }} {{ $loc := ( p .Data "location" ) }} {{ if $loc }} {{ with geouri $loc }}
📍 - {{ with ( geourip . "name" ) }}{{ . }}{{ else }}{{ with ( geotitle .Latitude .Longitude ) }}{{ . }}{{ else }}{{ .Latitude }}, {{ .Longitude }}{{ end }}{{ end }} + {{ with ( geourip . "name" ) }}{{ . }}{{ else }}{{ with ( geotitle .Latitude .Longitude $bloglang ) }}{{ . }}{{ else }}{{ .Latitude }}, {{ .Longitude }}{{ end }}{{ end }}