A working prototype translation system that relies purely on constant variable, package namespace, maps and go generate.
A simple translation system that nicely suitable for practically embedded systems such as docker (IMO) and webassembly.
$ go get -u github.com/cjtoolkit/translate-gen
base.toml
[base]
language = "English"
createMap = true # Optional
[[base.value]]
const = "Hello"
value = "Hello World"
[[base.value]]
const = "Name"
value = "My name is %s"
french.toml
[[import]] # Optional
path = "net/http"
alias = "httpStuff" # Optional
[translation]
language = "French"
functionName = "French"
[[translation.value]]
const = "Hello"
value = "Bonjour le monde"
[[translation.value]]
const = "Name"
value = "Mon nom est %s"
[[translation.value]]
const = "httpStuff.StatusNotFound"
value = "Pas trouvé"
generate.go
//go:generate translate-gen example base.toml french.toml
package example
Than run go generate generate.go
base.go
// Code generated by translate-gen. DO NOT EDIT.
// Source: locale/base.toml
package example
// English
const (
Hello = `Hello World`
Name = `My name is %s`
)
// English Map
func BaseMap() map[interface{}]string {
return map[interface{}]string{
Hello: Hello,
Name: Name,
}
}
french.go
// Code generated by translate-gen. DO NOT EDIT.
// Source: locale/french.toml
package example
import (
httpStuff "net/http"
)
// French
func French() map[interface{}]string {
return map[interface{}]string{
Hello: `Bonjour le monde`,
Name: `Mon nom est %s`,
httpStuff.StatusNotFound: `Pas trouvé`,
}
}
It's basically generate go code from toml.
It's will replace backtick ( ` ) with a single quote ( ' ) for values.
There is no field validation, I omitted that for flexibility. Just be extra careful.