在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):bykovme/gotrans开源软件地址(OpenSource Url):https://github.com/bykovme/gotrans开源编程语言(OpenSource Language):Go 100.0%开源软件介绍(OpenSource Introduction):gotrans - localization package for golangLocalization package for GO, it uses JSON files to localize your GO application Getting startedInstallationInstall the package with the command go get github.com/bykovme/gotrans Prepare translation filesJSON 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:
Example of using gotrans packageSimple 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)
} BehaviourIf 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 documentationFUNCTIONSfunc 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.
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
func Tr(locale string, trKey string) string Tr - find translation for provided locale and translation key
Alex Bykov © 2015 - 2020 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论