Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Translation] Provide TranslatorInterface implementation to facilitate testing? #45472

Closed
MalteWunsch opened this issue Feb 18, 2022 · 3 comments

Comments

@MalteWunsch
Copy link
Contributor

Description

When adding translations to an existing project, I'd like to write tests for them somehow. How?

I start at the translator documentation, but there are no tipps on that (yet), so I have to think about that by myself. I know I don't want to check for the translation messages, as they are prone to change. So the next best thing I currently see is using the IdentityTranslator, which is a TranslatorInterface implementation that returns the message key as it's translation. With that, I can check for the translation keys in my tests.

But this approach falls a bit short when I want

  • to assert that the translation actually happened (the test cannot differentiate between a message key that was rendered plainly and a translated message, because they are equal)
  • I need the translation to follow specific rules - e.g. to be an email address that is used as a sender or receiver email address of a contact form and hence checked with the email constraint.

So usually come up with something like

$translator = new class() implements TranslatorInterface
  public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string
  {
    return ($id === 'sender.emailAddress')
      ? 'sender@example.com'
      : 'translator-was-called-with-id:' . $id;
  }
};

So I wonder if it would be a good idead to work on something like a TestTranslator, e.g. as a generalization of the inline class above?

And if so, in which package should it be placed? As a test tool, I wouldn't place it in symfony/translation, but the names of the usually dev-required composer packages (symfony/browser-kit, /css-selector, /debug-bundle, /maker-bundle, /phpunit-bridge, /stopwatch, /web-profiler-bundle) don't sound any better to me.

WDYT?

Example

No response

@ro0NL
Copy link
Contributor

ro0NL commented Feb 18, 2022

Would using an ArrayLoader work?

@carsonbot
Copy link

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@MalteWunsch
Copy link
Contributor Author

Thanks for the suggestion @ro0NL! After giving it some thought (not for the whole 6+ months though - it slipped through, sorry!): yes, you're right, that's an easy solution.

For reference:

use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Translator;

$translator = new Translator(null);
$translator->addLoader('array', new ArrayLoader());
$translator->addResource(
  'array',
  [
    'sender.emailAddress' => 'sender@example.com',
  ],
  'de_DE'
);

This does not feature the dynamic 'translator-was-called-with-id:' . $id translations like in my suggestion above, but they weren't really necessary. I can easily add the few relevant messages and I'm fine with the rest defaulting to their message key. Checking that the relevant messages are translated is enough to ensure that the translator is actually used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants