Posts Tagged github.com

Localization for GOLANG

I created and published small package to be used for localization of go applications, you can find it here https://github.com/bykovme/gotrans

Installation

Install the package with the command

go get github.com/bykovme/gotrans

Prepare translation files

JSON files should use following format as in the example below:

{
    "hello_world":"Hello World",
    "find_more":"Find more information about the project on the website %s"
}

JSON file name should use standard language code or language-country code supported by browsers, if you are developing web app. At least one file with English locale should be in the localization folder with the name “en.json”.

Quick documentation

There are just 3 functions in the package

InitLocales(path string)

Use the relative or absolute path to set the folder where all the JSON files with translations are located. Make sure that all the files with translations have extension “.json”

Tr(lang string, key string) string

Get translation value by the language & key

DetectLanguage(acceptLanguage string) string

This function will be useful when you are creating web application, it detects the language from HTTP header Accept-Language, check the usage of the function in the example below

Example of using gotrans package

The same example is located within the package here

package main

import (
    "fmt"
    "net/http"

    "github.com/bykovme/gotrans"
)

func handler(w http.ResponseWriter, r *http.Request) {
    lang := gotrans.DetectLanguage(r.Header.Get("Accept-Language"))
    fmt.Fprintf(w, "<html><head><title> %s </title></head><body>", gotrans.Tr(lang, "hello_world"))
    fmt.Fprintf(w, "<h2> %s </h2>", gotrans.Tr(lang, "hello_world"))
    githubLink := "https://github.com/bykovme/gotrans"
    link := fmt.Sprintf(`<a href="%s">%s</a>`, githubLink, githubLink)
    fmt.Fprintf(w, gotrans.Tr(lang, "find_more"), link)
    fmt.Fprint(w, "</body></html>")
}

func main() {
    err := gotrans.InitLocales("langs")
    if err != nil {
        panic(err)
    }

    http.HandleFunc("/", handler)
    http.ListenAndServe(":3000", nil)
}

Behaviour

If the key is not found in the localization file, it will try to find the same key in English localization (“en.json”), if the key is not found there as well, the key will be returned instead of value.

, , , , , ,

Leave a comment

Using standard command “go get” for private golang packages on gitlab.com

I’m keeping my projects privately on gitlab.com and want to use for some of my golang packages standard “go get” command instead of using git clone/pull. When I’m trying to use standard commands I’m getting the error

go get gitlab.com/bykovme/my_package_here
# cd .; git clone https://gitlab.com/bykovme/my_package_here.git /home/bykov/gopath/src/gitlab.com/bykovme/my_package_here
Cloning into '/home/bykov/gopath/src/gitlab.com/bykovme/my_package_here'...
fatal: could not read Username for 'https://gitlab.com': terminal prompts disabled

As I’m using SSH keys to access the git repository, the following command solves the issue giving possibility to use standard go packages commands

git config --global url."git@gitlab.com:".insteadOf "https://gitlab.com/"

You can check if it was applied using the following command

cat ~/.gitconfig

The output of the command above should be like the text below

[url "git@gitlab.com:"]
    insteadOf = https://gitlab.com/

The command

go get gitlab.com/bykovme/my_package_here

is working now.

PS. The same solution is working for github.com

, , , , , , , ,

Leave a comment