我必须根据用户想要的语言动态更改语言环境.
我可以像这样设置Application/Module.php中的语言环境:
public function onBootstrap(MvcEvent $e)
{
$translator = $e->getApplication()->getServiceManager()->get('translator');
$translator->setLocale('hu_HU');
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我想更改语言,我怎么能在控制器中执行此操作?我尝试了这个,但在此之后,我可以只为这一个请求而不是全局更改语言环境.
$translator = $this->getServiceLocator()->get('translator');
$translator->setLocale('srb_SRB');
Run Code Online (Sandbox Code Playgroud) 我已经在Grails中使用继承对我的域类建模,如下所示.
abstract class Profile{
}
class Team extends Profile{
}
class User extends Profile{
}
class B{
static hasMany = [profiles: Profile]
}
Run Code Online (Sandbox Code Playgroud)
稍后在控制器中,当我在某些情况下从B类获取所有配置文件时,我想将一些配置文件转换为Team或User,但我不能,因为我得到了java.lang.ClassCastException或GroovyCastException,尽管它们保存为团队或用户(在数据库中具有属性类).以下是我尝试过的方法:
def team1 = b.profiles.toList()[0] as Team
def team1 = (Team)b.profiles.toList()[0]
Run Code Online (Sandbox Code Playgroud)
当我不写任何类型时它正在工作,因为它在动态语言中是正常的.
def team1 = b.profiles.toList()[0]
Run Code Online (Sandbox Code Playgroud)
但后来我才知道我正在使用哪个班级.无论如何在groovy或gorm中将父类投射给孩子?