Posts Tagged package

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

Reference ‘Xamarin.Android.Support.v4’ not resolved

I killed some days to get rid from the error in the topic, it happened when I tried to compile the app for Android, finally I found a way how to solve it, quick manual:

  1. Select Build -> Clean All in your Xamarin project where you have the issue
  2. Close your Xamarin project
  3. Exit from Xamarin Studio
  4. Go to your project in the file system, find the the folder “packages” delete the folder “Xamarin.Android.Support.v4.*” from there and everything what is inside
  5. Go to your Android project, find there the file “package.config”
  6. Find the line where “Xamarin.Android.Support.v4.*” is mentioned and delete this line
  7. Start Xamarin Studio, load your project
  8. Go to Android part of the project, right click on the “Packages” folder and select menu item “Add packages..”
  9. Find in the repository the package “Xamarin.Android.Support.v4” and install it again
  10. Try to build Android project again
  11. Enjoy, now everything should work again.

, , , ,

1 Comment