Internationalization
QOR Admin provides an interface to support Internationalization
type I18n interface {
Scope(scope string) I18n
Default(value string) I18n
T(locale string, key string, args ...interface{}) template.HTML
}
If you specified an i18n backend that implemented this interface when initializing QOR Admin, it will get internationalization supports immediately.
Admin := admin.New(&admin.AdminConfig{
I18n: yourI18nBackend,
})
QOR I18n
QOR I18n implements the I18n
interface, let me show you how to use it with Qor Admin
.
Firstly, initialize i18n with storages. You can use multiple storages together, the earlier one has higher priority. So in the example, I18n will look up the translation in the database first, then continue finding it in the YAML file if not found.
import (
"github.com/jinzhu/gorm"
"github.com/qor/i18n"
"github.com/qor/i18n/backends/database"
"github.com/qor/i18n/backends/yaml"
)
func main() {
db, _ := gorm.Open("mysql", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local")
I18n := i18n.New(
database.New(&db), // load translations from the database
yaml.New(filepath.Join(config.Root, "config/locales")), // load translations from the YAML files in directory `config/locales`
)
}
Then initialize QOR Admin with I18n
Admin := admin.New(&admin.AdminConfig{
I18n: I18n,
})
Done it! All translations will be powered by the I18n backend.
Manage I18n translations via QOR Admin Interface
No one would like to update translations by changing the database or update YAML directly, so i18n provides a friendly web interface for managing translations via QOR Admin, to make it works, just add it as a resource to QOR Admin, like:
Admin.AddResource(I18n)
If you registered I18n to QOR Admin as Resource, then you can skip configuring QOR Admin when initializing it, i18n will do it automatically