bug #32925 [Translation] Collect original locale in case of fallback translation (digilist)

This PR was squashed before being merged into the 3.4 branch (closes #32925).

Discussion
----------

[Translation] Collect original locale in case of fallback translation

Before, it collected the fallback locale that was used to translate a key. But this information is confusing, as it does not reveal which translation key is missing in the requested language.

So I'd like to propose to track the "requested" locale instead, so that the Symfony profiler gives me the information in which locale the key is missing instead of which locale was used as a fallback.

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | yes?
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

In principle, this change is a BC break, but imho also a bug. It's really confusing when the Profiler tells you that it uses a translation fallback for an ID and locale that is actually translated. Took some debugging so recognize that this fallback came from another locale. If you think it's better to target 5.0, I'll update the PR.

Commits
-------

5564e149cb [Translation] Collect original locale in case of fallback translation
This commit is contained in:
Fabien Potencier 2019-09-27 07:57:25 +02:00
commit 650f179c2f
3 changed files with 21 additions and 9 deletions

View File

@ -13,7 +13,7 @@
{% set text %}
<div class="sf-toolbar-info-piece">
<b>Locale</b>
<b>Default locale</b>
<span class="sf-toolbar-status">
{{ collector.locale|default('-') }}
</span>
@ -73,7 +73,7 @@
<div class="metrics">
<div class="metric">
<span class="value">{{ collector.locale|default('-') }}</span>
<span class="label">Locale</span>
<span class="label">Default locale</span>
</div>
<div class="metric">
<span class="value">{{ collector.fallbackLocales|join(', ')|default('-') }}</span>
@ -152,7 +152,7 @@
</div>
{% else %}
{% block fallback_messages %}
{{ helper.render_table(messages_fallback) }}
{{ helper.render_table(messages_fallback, true) }}
{% endblock %}
{% endif %}
</div>
@ -185,11 +185,14 @@
{% endblock %}
{% macro render_table(messages) %}
{% macro render_table(messages, is_fallback) %}
<table>
<thead>
<tr>
<th>Locale</th>
{% if is_fallback %}
<th>Fallback locale</th>
{% endif %}
<th>Domain</th>
<th>Times used</th>
<th>Message ID</th>
@ -200,6 +203,9 @@
{% for message in messages %}
<tr>
<td class="font-normal text-small nowrap">{{ message.locale }}</td>
{% if is_fallback %}
<td class="font-normal text-small nowrap">{{ message.fallbackLocale|default('-') }}</td>
{% endif %}
<td class="font-normal text-small text-bold nowrap">{{ message.domain }}</td>
<td class="font-normal text-small nowrap">{{ message.count }}</td>
<td>

View File

@ -145,6 +145,7 @@ class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInter
$id = (string) $id;
$catalogue = $this->translator->getCatalogue($locale);
$locale = $catalogue->getLocale();
$fallbackLocale = null;
if ($catalogue->defines($id, $domain)) {
$state = self::MESSAGE_DEFINED;
} elseif ($catalogue->has($id, $domain)) {
@ -153,10 +154,9 @@ class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInter
$fallbackCatalogue = $catalogue->getFallbackCatalogue();
while ($fallbackCatalogue) {
if ($fallbackCatalogue->defines($id, $domain)) {
$locale = $fallbackCatalogue->getLocale();
$fallbackLocale = $fallbackCatalogue->getLocale();
break;
}
$fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
}
} else {
@ -165,6 +165,7 @@ class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInter
$this->messages[] = [
'locale' => $locale,
'fallbackLocale' => $fallbackLocale,
'domain' => $domain,
'id' => $id,
'translation' => $translation,

View File

@ -34,6 +34,7 @@ class DataCollectorTranslatorTest extends TestCase
'id' => 'foo',
'translation' => 'foo (en)',
'locale' => 'en',
'fallbackLocale' => null,
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
'parameters' => [],
@ -42,7 +43,8 @@ class DataCollectorTranslatorTest extends TestCase
$expectedMessages[] = [
'id' => 'bar',
'translation' => 'bar (fr)',
'locale' => 'fr',
'locale' => 'en',
'fallbackLocale' => 'fr',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => [],
@ -52,6 +54,7 @@ class DataCollectorTranslatorTest extends TestCase
'id' => 'choice',
'translation' => 'choice',
'locale' => 'en',
'fallbackLocale' => null,
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_MISSING,
'parameters' => [],
@ -60,7 +63,8 @@ class DataCollectorTranslatorTest extends TestCase
$expectedMessages[] = [
'id' => 'bar_ru',
'translation' => 'bar (ru)',
'locale' => 'ru',
'locale' => 'en',
'fallbackLocale' => 'ru',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => [],
@ -69,7 +73,8 @@ class DataCollectorTranslatorTest extends TestCase
$expectedMessages[] = [
'id' => 'bar_ru',
'translation' => 'bar (ru)',
'locale' => 'ru',
'locale' => 'en',
'fallbackLocale' => 'ru',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => ['foo' => 'bar'],