From 9933169c81dffadf646a7825aa721e8e297f772f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Tue, 3 Apr 2018 13:45:07 +0200 Subject: [PATCH] localization support --- http_utility.go | 108 +++++++++++++++++++++++++++++++++++++++++++----------- json_utility.go | 3 +- locale_utility.go | 33 +++++++++++++++++ 3 files changed, 121 insertions(+), 23 deletions(-) create mode 100644 locale_utility.go diff --git a/http_utility.go b/http_utility.go index f020d9d..16d7711 100644 --- a/http_utility.go +++ b/http_utility.go @@ -5,13 +5,98 @@ import ( "net/http" ) +type webError struct { + Request string `json:"request"` + Error string `json:"error"` +} + +// NotFoundHandler writes HTTP error 404 to w. +func NotFoundHandler(w http.ResponseWriter, req *http.Request) { + SetDefaultHeaders(w) + if req.Method == "OPTIONS" { + return + } + ErrorResponse(w, req, http.StatusNotFound, []HttpErrorDesc{ + {"en", "Not found."}, + {"rs", "Traženi resurs ne postoji."}, + }) +} + +// SetDefaultHeaders set's default headers for an HTTP response. +func SetDefaultHeaders(w http.ResponseWriter) { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS") + w.Header().Set("Access-Control-Allow-Headers", `Accept, Content-Type, + Content-Length, Accept-Encoding, X-CSRF-Token, Authorization`) + w.Header().Set("Content-Type", "application/json; charset=utf-8") +} + +// 2xx +func Success(w http.ResponseWriter, payload *Payload, code int) { + w.WriteHeader(code) + if payload != nil { + json.NewEncoder(w).Encode(*payload) + } +} + +// 200 +func OK(w http.ResponseWriter, payload *Payload) { + Success(w, payload, http.StatusOK) +} + +// 201 +func Created(w http.ResponseWriter, payload *Payload) { + Success(w, payload, http.StatusCreated) +} + +// 4xx; 5xx +func Error(w http.ResponseWriter, r *http.Request, code int, err string) { + werr := webError{Error: err, Request: r.Method + " " + r.RequestURI} + w.WriteHeader(code) + json.NewEncoder(w).Encode(werr) +} + +// 400 +func BadRequest(w http.ResponseWriter, r *http.Request, err string) { + Error(w, r, http.StatusBadRequest, err) +} + +// 404 +func NotFound(w http.ResponseWriter, r *http.Request, err string) { + Error(w, r, http.StatusNotFound, err) +} + +// 401 +func Unauthorized(w http.ResponseWriter, r *http.Request, err string) { + Error(w, r, http.StatusUnauthorized, err) +} + +// 403 +func Forbidden(w http.ResponseWriter, r *http.Request, err string) { + Error(w, r, http.StatusForbidden, err) +} + +// 403 +func Conflict(w http.ResponseWriter, r *http.Request, err string) { + Error(w, r, http.StatusConflict, err) +} + +// 500 +func InternalServerError(w http.ResponseWriter, r *http.Request, err string) { + Error(w, r, http.StatusInternalServerError, err) +} + +/// +/// Old API +/// + const ( templateHttpErr500_EN = "An internal server error has occurred." templateHttpErr500_RS = "Došlo je do greške na serveru." templateHttpErr400_EN = "Bad request." templateHttpErr400_RS = "Neispravan zahtev." templateHttpErr404_EN = "Resource not found." - templateHttpErr404_RS = "Objekat nije pronadjen." + templateHttpErr404_RS = "Resurs nije pronadjen." templateHttpErr401_EN = "Unauthorized request." templateHttpErr401_RS = "Neautorizovan zahtev." ) @@ -73,24 +158,3 @@ func UnauthorizedResponse(w http.ResponseWriter, req *http.Request) { {"rs", templateHttpErr401_RS}, }) } - -// NotFoundHandler writes HTTP error 404 to w. -func NotFoundHandler(w http.ResponseWriter, req *http.Request) { - SetDefaultHeaders(w) - if req.Method == "OPTIONS" { - return - } - ErrorResponse(w, req, http.StatusNotFound, []HttpErrorDesc{ - {"en", "Not found."}, - {"rs", "Traženi resurs ne postoji."}, - }) -} - -// SetDefaultHeaders set's default headers for an HTTP response. -func SetDefaultHeaders(w http.ResponseWriter) { - w.Header().Set("Access-Control-Allow-Origin", "*") - w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS") - w.Header().Set("Access-Control-Allow-Headers", `Accept, Content-Type, - Content-Length, Accept-Encoding, X-CSRF-Token, Authorization`) - w.Header().Set("Content-Type", "application/json; charset=utf-8") -} diff --git a/json_utility.go b/json_utility.go index ea283f6..bf150d9 100644 --- a/json_utility.go +++ b/json_utility.go @@ -61,7 +61,8 @@ type Payload struct { Correlations []CorrelationField `json:"correlationFields"` IdField string `json:"idField"` - // Data holds JSON payload. It can't be used for itteration. + // Data holds JSON payload. + // It can't be used for itteration. Data interface{} `json:"data"` } diff --git a/locale_utility.go b/locale_utility.go new file mode 100644 index 0000000..b08d530 --- /dev/null +++ b/locale_utility.go @@ -0,0 +1,33 @@ +package webutility + +import ( + "encoding/json" + "io/ioutil" +) + +var locales = map[string]map[string]string{} + +func LoadLocale(loc, filePath string) error { + file, err := ioutil.ReadFile(filePath) + if err != nil { + return err + } + + var data interface{} + err = json.Unmarshal(file, &data) + if err != nil { + return err + } + + l := map[string]string{} + for k, v := range data.(map[string]interface{}) { + l[k] = v.(string) + } + locales[loc] = l + + return nil +} + +func Translate(loc, key string) string { + return locales[loc][key] +} -- 1.8.1.2