Dict is a Go package for translating terms
- By running below go command you can install it, if you use 'go mod' you can skip this step
go get github.com/syronz/dict
- Import it in your code:
import "github.com/syronz/dict"
Dict support JSON and TOML, we recommend toml because it prevent duplication. In version 1.1 this package just support English, Kurdish and Arabic. The style of terms.toml should be like below
terms.toml
["route not found"]
en = 'route not found'
ku = 'route booni nie'
ar = 'الطريق غير موجود'
[unauthorized]
en = 'unauthorized'
ku = 'daxl naboo'
ar = 'غير مصرح'
["%v shouldn't be more than %v"]
en = "%v shouldn't be more than %v"
ku = "%v nabe la %v ziatr be"
ar = "يجب ألا يكون٪ v أكثر من٪ v"
For loading terms just one time run the below method
dict.Init("./terms.toml", true)
For tranlate simple sentence/word without params it be like below
translated := dict.T("route not found", dict.Ku)
fmt.Println("route not found: ", translated)
Output:
route not found: route booni nie
For translate sentences with params, dynamic terms. you can pass arbitrary params as arguments
In case you want translate params should cast them as dict.R
phrase := "%v shouldn't be more than %v"
translated = dict.T(phrase, dict.Ku, dict.R("age"), 18)
fmt.Println(phrase+": ", translated)
Output:
%v shouldn't be more than %v: taman nabe la 18 ziatr be
If the word not exist inside the TOML or JSON file, around the output '!!!' would be added. For example
translated = dict.T("term not exist", dict.Ku)
fmt.Println("term not exist: ", translated)
Output:
term not exist: !!! term not exist !!!
For prevent adding '!!!' around not existed terms we can use SafeTranslate, in this case empty and false will be returned
var ok bool
translated, ok = dict.SafeTranslate("term not exist", dict.Ku)
fmt.Println("term not exist: ", translated, ok)
Output:
term not exist: false