简体   繁体   English

有没有办法在模型中使用gobuffalo i18n转换器?

[英]Is there a way to use gobuffalo i18n translator in models?

gobuffalo i18n translator is accessible in actions but how can I access it on my models? gobuffalo i18n转换器可在操作中访问,但如何在模型中访问它? It requires the buffalo.Context as a param in T *i18n.Translator . 它需要buffalo.Context作为T *i18n.Translator一个参数。

Thank you in advance! 先感谢您!

This is a workaround solution. 这是一种解决方法。

1.In models.go create the following function 1.在models.go中创建以下功能

import "github.com/nicksnyder/go-i18n/i18n"
var Lang = "en"

func t(translationID string, args ...interface{}) string{


   T, _ := i18n.Tfunc(Lang, fmt.Sprintf("locales/models.%s.yaml", Lang))
   return T(translationID,args... ) 

}

In your init() function add 在您的init()函数中添加

    i18n.LoadTranslationFile(fmt.Sprintf("locales/models.fr.yaml"))
    i18n.LoadTranslationFile(fmt.Sprintf("locales/models.en.yaml"))
    ... other locale files if needed....

In your models you can now use the translate function t("translationID") 现在,您可以在模型中使用翻译功能t("translationID")

You can change the models locale by simple setting the Lang variable models.Lang="fr" 您可以通过简单地设置Lang变量模型来更改模型区域设置models.Lang="fr"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM