我正在使用symfony 3.4.19,并且有2种语言的翻译:英语en Dutch。我正在使用this symfony指南来翻译验证消息
这是我的UserDTO包含断言和消息翻译的部分:
class UserDTO
{
/**
* @var string
*
* @Assert\NotNull(message="security.registration.firstname.required", groups={"createUser", "updateUser"})
* @Assert\NotBlank(message="security.registration.firstname.required", groups={"createUser", "updateUser"})
*/
private $firstname;
/**
* @var string
*
* @Assert\NotNull(message="security.registration.lastname.required", groups={"createUser", "updateUser"})
* @Assert\NotBlank(message="security.registration.lastname.required", groups={"createUser", "updateUser"})
*/
private $lastname;
/**
* @var string
*
* @Assert\NotNull(message="security.registration.email.required", groups={"createUser", "updateUser"})
* @Assert\NotBlank(message="security.registration.email.required", groups={"createUser", "updateUser"})
* @Assert\Email(message="security.registration.email.format", groups={"createUser", "updateUser"})
*/
private $email;
/**
* @var string
*
* @Assert\NotNull(message="security.registration.password.required", groups={"createUser", "updateUser"})
* @Assert\NotBlank(message="security.registration.password.required", groups={"createUser", "updateUser"})
*/
private $plainPassword;
}
所以我有2个翻译文件:security.en.yml
"Username could not be found.": "Wrong email or password!"
"Invalid credentials.": "Wrong email or password!"
security:
login:
email: 'Email'
password: 'Password'
registration:
firstname:
title: 'Firstname'
required: 'A firstname is required'
lastname:
title: 'Lastname'
required: 'A lastname is required'
email:
title: 'Email'
required: 'An email is required'
format: 'Wrong email format'
password:
title: 'Password'
required: 'A password is required'
和security.nl.yml:
"Username could not be found.": "Verkeerd email of wachtwoord!"
"Invalid credentials.": "Verkeerd email of wachtwoord!"
security:
login:
email: 'Email'
password: 'Wachtwoord'
registration:
firstname:
title: 'Voornaam'
required: 'Voornaam is verplicht'
lastname:
title: 'Achternaam'
required: 'Achternaam is verplicht'
email:
title: 'Email'
required: 'Email is verplicht'
format: 'Verkeerd email formaat'
password:
title: 'Wachtwoord'
required: 'Wachtwoord is verplicht'
我也不使用symfony表单构建器,所以我只提交在树枝模板中创建的表单:
{% trans_default_domain 'security' %}
{% extends 'base.html.twig' %}
{% block stylesheets %}
{{ parent() }}
<link href="{{ asset('css/project/registration.css') }}" rel="stylesheet"/>
{% endblock %}
{% block javascripts %}
{{ parent() }}
{% endblock %}
{% block body %}
{{ include('component/main-navbar.html.twig') }}
<div>
<h2>Create new account</h2>
{% if errors is defined %}
<div class="alert alert-danger">
<ul>
{% for error in errors %}
<li>{{ error|trans }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
<form action="/register" method="post">
<input type="text" name="firstname" placeholder="{{ 'security.registration.firstname.title'|trans }}">
<input type="text" name="lastname" placeholder="{{ 'security.registration.lastname.title'|trans }}">
<input type="text" name="email" placeholder="{{ 'security.registration.email.title'|trans }}">
<input type="text" name="plainPassword" placeholder="{{ 'security.registration.password.title'|trans }}">
<button type="submit" class="btn btn-success btn-sm float-right">Login <span class="fa fa-lock"></span>
</button>
</form>
</div>
{{ include('component/footer.html.twig') }}
{% endblock %}
如您所见,在模板顶部,我明确设置了翻译域,即。 “安全”。
在控制器中,我有2种方法。第一个是呈现视图,另一个是提交表单。
public function registrationPageAction(Request $request)
{
return new Response(
$this->twigService->render(
'security/register.html.twig',
[
'error' => []
]
),
200
);
}
提交表格:
public function registrationSubmitAction(Request $request)
{
$submittedFormData = $request->request->all();
$userDTO = new UserDTO();
$userDTO->setFirstname($submittedFormData['firstname'])
->setLastname($submittedFormData['lastname'])
->setEmail($submittedFormData['email'])
->setPlainPassword($submittedFormData['plainPassword']);
$fieldErrors = $this->validator->validate($userDTO, null, ['createUser']);
$errors = [];
if (count($fieldErrors) > 0)
{
foreach ($fieldErrors as $error)
{
$errors[] = $error->getMessage();
}
return new Response(
$this->twigService->render(
'security/register.html.twig',
[
'errors' => $errors
]
),
200
);
}
}
目前,我不在任何地方存储提交的数据,我只是想确保翻译工作正常。
因此,当我转到/ register时,可以看到带有TRANSLATED字段的表单。并且在调试栏中没有关于缺少翻译的通知:
提交表单后,我还获得了包含翻译字段的表单,但是这次在调试栏中,我还收到了缺少翻译的通知:
请注意,所有翻译(字段和消息)都可以正常工作。在config.yml中,我将后备语言环境设置为'en',而当前语言环境设置为'nl'。
我真的不明白为什么我一直看到这些通知。有什么想法吗?
更新:
现在,我摆脱了security.en.yml和security.nl.yml,仅出于测试目的创建了app / Resources / translations / validators.en.yml,内容如下:
firstname: 'Firstname is needed'
lastname: 'Lastname is needed'
email: 'Email is needed'
emailformat: 'Wrong email format'
password: 'Password is needed'
我还修改了UserDTO类中的验证消息:
class UserDTO
{
/**
* @var string
*
* @Assert\NotNull(message="firstname", groups={"createUser", "updateUser"})
* @Assert\NotBlank(message="firstname", groups={"createUser", "updateUser"})
*/
private $firstname;
/**
* @var string
*
* @Assert\NotNull(message="lastname", groups={"createUser", "updateUser"})
* @Assert\NotBlank(message="lastname", groups={"createUser", "updateUser"})
*/
private $lastname;
/**
* @var string
*
* @Assert\NotNull(message="email", groups={"createUser", "updateUser"})
* @Assert\NotBlank(message="email", groups={"createUser", "updateUser"})
* @Assert\Email(message="emailformat", groups={"createUser", "updateUser"})
*/
private $email;
/**
* @var string
*
* @Assert\NotNull(message="password", groups={"createUser", "updateUser"})
* @Assert\NotBlank(message="password", groups={"createUser", "updateUser"})
*/
private $plainPassword;
}
在register.html.twig中,我删除了{%trans_default_domain'security'%},并制作了没有字段翻译的表格:
<form action="/register" method="post">
<input type="text" name="firstname" placeholder="Firstname">
<input type="text" name="lastname" placeholder="Lastname">
<input type="text" name="email" placeholder="Email">
<input type="text" name="plainPassword" placeholder="Password">
<button type="submit" class="btn btn-success btn-sm float-right">Login <span class="fa fa-lock"></span>
</button>
</form>
我在做什么错?我只是一步一步地遵循官方的symfony指南:(
答案 0 :(得分:2)
问题在您的上一个屏幕截图中有点隐藏。验证约束将不使用模板中的默认翻译域。默认情况下,这些邮件是从validators
域中提取的。
一个选项可能是将消息密钥复制到验证者域的新文件中,您可以更改验证消息的默认域。据我所知,这是通过覆盖参数%validator.translation_domain%
例如在您的services.yaml或config.yaml中:
# app/config/config.yaml
parameters:
validator.translation_domain: security
framework:
...
由于这些确实是验证错误,我认为将这些消息保留在验证者域内并仅在模板中有选择地使用此域会更有意义:
<input type="text" name="firstname" placeholder="{{ 'security.registration.firstname.title'|trans(domain="validators") }}">
或将占位符和验证消息拆分到单独的翻译域(和文件)中。
编辑以进行澄清:首先,验证者将翻译您的消息,而不是查找翻译,因此翻译是原始密钥。然后,此“翻译”将传递到模板中的错误处理中,并在其中与当前域一起翻译。因此,基本上,您的问题是对验证消息进行双重翻译的某种隐藏尝试。