Search code examples
golocalizationlang

How to get actual localization/lang in golang?


There is function who return localize/lang in golang ? Something like HTTP_ACCEPT_LANGUAGE in PHP

I wanted to make a dynamically changing language page but i can't find this.


Solution

  • r.Header.Get("Accept-Language") is what you are looking for.

    The function will return something like: en-US,en;q=0.8,ro;q=0.6

    func main() {
        http.HandleFunc("/", index) 
        err := http.ListenAndServe(":8080", nil)
        if err != nil {
            log.Fatal("Error happend", err)
        }
    }
    
    func index(w http.ResponseWriter, r *http.Request) {        
        if r.Method == "GET" {
            fmt.Println(r.Header.Get("Accept-Language"))
        }
    }
    

    And the corresponding $_SERVER['HTTP_ACCEPT_LANGUAGE'] php function will return the same information: en-US,en;q=0.5.