From a1f4690e72f6d8258a2b02816f2d1327565f2f49 Mon Sep 17 00:00:00 2001 From: zu1k Date: Thu, 21 Dec 2023 21:46:02 +0800 Subject: [PATCH] feat: geoip use default lang when selected not exist --- pkg/geoip/geoip.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/geoip/geoip.go b/pkg/geoip/geoip.go index 7b61450..c1d0c74 100644 --- a/pkg/geoip/geoip.go +++ b/pkg/geoip/geoip.go @@ -48,9 +48,9 @@ func (g GeoIP) Find(query string, params ...string) (result fmt.Stringer, err er } result = Result{ - Country: record.Country.Names[lang], + Country: getMapLang(record.Country.Names, lang), CountryCode: record.Country.IsoCode, - Area: record.City.Names[lang], + Area: getMapLang(record.City.Names, lang), } return } @@ -72,3 +72,13 @@ func (r Result) String() string { return fmt.Sprintf("%s %s", r.Country, r.Area) } } + +const DefaultLang = "en" + +func getMapLang(data map[string]string, lang string) string { + res, found := data[lang] + if found { + return res + } + return data[DefaultLang] +}