• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

bykovme/gotrans: Localization package for golang

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

bykovme/gotrans

开源软件地址(OpenSource Url):

https://github.com/bykovme/gotrans

开源编程语言(OpenSource Language):

Go 100.0%

开源软件介绍(OpenSource Introduction):

Build Status codecov Go Report Card

gotrans - localization package for golang

Localization package for GO, it uses JSON files to localize your GO application

Getting started

Installation

Install the package with the command

go get github.com/bykovme/gotrans

Prepare translation files

JSON files should use following format:

{
    "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. At least one file "en.json" should be in the localization folder. Initiate the library using the folder with localization files with the function gotrans.InitLocales(), see detailed documentation below

The folder content should look like that:

    en.json
    de.json
    ru.json

Example of using gotrans package

Simple usage example (retrieving translation with a locale and translation key)

package main

import (
	"fmt"

	"github.com/bykovme/gotrans"
)

func main() {
    _ := gotrans.InitLocales("/home/user/project/languages")  //  Path to the folder with localization files
    fmt.Println(gotrans.Tr("en", "hello_world"))  // Using english translation from the file 'en.json'
    fmt.Println(gotrans.Tr("ru", "hello_world"))  // Using russian translation from the file 'ru.json'
}

Using default language and shorter function call (retrieving translation without a locale)

package main

import (
	"fmt"

	"github.com/bykovme/gotrans"
)

func main() {
	_ = gotrans.InitLocales("/home/user/project/languages")  //  Path to the folder with localization files     
	_ = gotrans.SetDefaultLocale("ru") // Setting default locale
    
	fmt.Println(gotrans.T("hello_world"))  // Using russian translation from the file 'ru.json'
}

More complicated usage example for dynamic usage on the webserver. The same example is located within the package here

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/bykovme/gotrans"
)

const cHtmlTemplate = `
<html>
	<head>
		<title>%s</title>
	</head>
	<body>
		<h2> %s </h2>
		%s 
	</body>
</html>
`
const cGitHubLink = "https://github.com/bykovme/gotrans"
const cLink = `<a href="%s">%s</a>`

func handler(w http.ResponseWriter, r *http.Request) {
	lang := gotrans.DetectLanguage(r.Header.Get("Accept-Language"))
	helloWorld := gotrans.Tr(lang, "hello_world")
	link := fmt.Sprintf(cLink, cGitHubLink, cGitHubLink)
	findMore := fmt.Sprintf(gotrans.Tr(lang, "find_more"), link)
	_, err := fmt.Fprintf(w, cHtmlTemplate,
		helloWorld,
		helloWorld,
		findMore)
	if err != nil {
		log.Println(err.Error())
	}
}

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.

Quick documentation

FUNCTIONS

func DetectLanguage(acceptLanguage string) string DetectLanguage - parse to find the most preferable language

func GetDefaultLocale() string GetDefaultLocale - return current default locale

func GetLocales() []string GetLocales - get available locales

func InitLocales(trPath string) error InitLocales - initiate locales from the folder.

Parameters:

'trPath' - path to the folder with translations files

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".

Examples:

err := gotrans.InitLocales("/home/user/project/languages") // absolute path

err := gotrans.InitLocales("languages") // relative path

func SetDefaultLocale(newLocale string) error SetDefaultLocale - set new default locale

func T(trKey string) string T - find translation for default locale and provided translation key

Parameters

'trKey' - translation key from json file

IMPORTANT! Call gotrans.InitLocale() to initiate translations before calling
this function and gotrans.SetDefaultLocale() to set up proper translation
lacale (if it is not set then the library will use locale "en")

func Tr(locale string, trKey string) string Tr - find translation for provided locale and translation key

Parameters

'locale' - locale value, for example: "en", "jp", "de", "ru"

'trKey' - translation key from json file

IMPORTANT! Call gotrans.InitLocale() to initiate translations before calling
this function

Alex Bykov © 2015 - 2020




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap