Skip to content

Commit 96be131

Browse files
committed
Symfony Translation assertions
1 parent 869fe17 commit 96be131

File tree

5 files changed

+92
-8
lines changed

5 files changed

+92
-8
lines changed

config/packages/framework.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@
3030
// Translation
3131
$framework->defaultLocale('en');
3232
$framework->translator()
33+
->enabled(true)
3334
->defaultPath('%kernel.project_dir%/resources/lang')
34-
->fallbacks('en');
35+
->fallbacks('es');
3536

3637
// Validator
3738
$framework->validation([
38-
'email_validation_mode' => 'html5'
39+
'email_validation_mode' => 'html5',
3940
]);
4041
};

resources/lang/messages.en.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
return [
4+
'register' => [
5+
'title' => 'Register',
6+
'heading' => 'Sign Up',
7+
'email_label' => 'Email Address',
8+
'password_label' => 'Password',
9+
'agree_terms_label' => 'I agree to the terms and conditions',
10+
'submit_button' => 'Sign Up',
11+
],
12+
];

resources/lang/messages.es.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
return [
4+
'register' => [
5+
'title' => 'Registro',
6+
'heading' => 'Registrarse',
7+
'email_label' => 'Correo Electrónico',
8+
'password_label' => 'Contraseña',
9+
'agree_terms_label' => 'Acepto los términos y condiciones',
10+
'submit_button' => 'Registrarse',
11+
],
12+
];
+6-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{% extends 'layout.html.twig' %}
22

3-
{% set page_title = 'Register' %}
3+
{% set page_title = 'register.title'|trans %}
44

55
{% block body %}
66
{% for flashError in app.flashes('verify_email_error') %}
77
<div role="alert">{{ flashError }}</div>
88
{% endfor %}
99

10-
<h1>Register</h1>
10+
<h1>{{ 'register.heading'|trans }}</h1>
1111

1212
{{ form_start(registrationForm) }}
13-
{{ form_row(registrationForm.email) }}
14-
{{ form_row(registrationForm.password) }}
15-
{{ form_row(registrationForm.agreeTerms) }}
13+
{{ form_row(registrationForm.email, { 'label': 'register.email_label'|trans }) }}
14+
{{ form_row(registrationForm.password, { 'label': 'register.password_label'|trans }) }}
15+
{{ form_row(registrationForm.agreeTerms, { 'label': 'register.agree_terms_label'|trans }) }}
1616

17-
<button type="submit">Register</button>
17+
<button type="submit">{{ 'register.submit_button'|trans }}</button>
1818
{{ form_end(registrationForm) }}
1919
{% endblock %}

tests/Functional/TranslationCest.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Functional;
6+
7+
use App\Tests\Support\FunctionalTester;
8+
9+
final class TranslationCest
10+
{
11+
public function dontSeeFallbackTranslations(FunctionalTester $I)
12+
{
13+
$I->amOnPage('/register');
14+
$I->dontSeeFallbackTranslations();
15+
}
16+
17+
public function dontSeeMissingTranslations(FunctionalTester $I)
18+
{
19+
$I->amOnPage('/');
20+
$I->dontSeeMissingTranslations();
21+
}
22+
23+
public function grabDefinedTranslationsCount(FunctionalTester $I)
24+
{
25+
$I->amOnPage('/register');
26+
$defined = $I->grabDefinedTranslationsCount();
27+
$I->assertSame($defined, 6);
28+
}
29+
30+
public function seeAllTranslationsDefined(FunctionalTester $I)
31+
{
32+
$I->amOnPage('/login');
33+
$I->seeAllTranslationsDefined();
34+
}
35+
36+
public function seeDefaultLocaleIs(FunctionalTester $I)
37+
{
38+
$I->amOnPage('/register');
39+
$I->seeDefaultLocaleIs('en');
40+
}
41+
42+
public function seeFallbackLocalesAre(FunctionalTester $I)
43+
{
44+
$I->amOnPage('/register');
45+
$I->seeFallbackLocalesAre(['es']);
46+
}
47+
48+
public function seeFallbackTranslationsCountLessThan(FunctionalTester $I)
49+
{
50+
$I->amOnPage('/register');
51+
$I->seeFallbackTranslationsCountLessThan(1);
52+
}
53+
54+
public function seeMissingTranslationsCountLessThan(FunctionalTester $I)
55+
{
56+
$I->amOnPage('/');
57+
$I->seeMissingTranslationsCountLessThan(1);
58+
}
59+
}

0 commit comments

Comments
 (0)