Testing localization with Playwright
If your application uses some kind of localization, it might be good to create a test for it that acts as a smoke test. These tests can cover the translation logic, and how certain fields are formatted, for example, dates, currency, decimals, and so on.
There might already be a unit test that covers this behavior, but thanks to Playwright it's just a tiny effort to write a simple end-to-end test. In my opinion, testing all the translations is an overkill and a time waster (this might not be true for your use case), but testing the basics provides a lot of value. With it, we become confident that our localization strategy works.
The Default Behavior link
By default, Playwright uses the locale and time zone of the host machine. This is not always desirable. When your locale and/or time zone is different from the one that is used on the server, or when your team is working in different time zones, this can be the cause of some conflicts.
To resolve this issue, we can set the default locale and time zone that is used by Playwright across your test suite(s).
You can do this by setting the locale
and timezoneId
options in the shared settings (use
) within the playwright config.
With the current setup, the locale that is used in the tests is nl-BE
, and the time zone that's used is Europe/Brussels
.
Overriding the Default Behavior link
This is already a good start, but we can do even better.
As mentioned earlier, we want to test the localization of the application. But with the current setup, we've set the locale to a fixed value and are thus we're only testing for one language.
Luckily, Playwright provides an easy way to set the locale per test block.
To do this, override the locale
and timezoneId
options with the use
method.
When you do this, all other tests within the same test block use the same locale and time zone.
Example link
When we put this all together, we can write a simple test that verifies if:
- the logic to implement the translation works
- the dates, decimals, and whatnot are formatted as expected
Feel free to update this blog post on GitHub, thanks in advance!
Join My Newsletter (WIP)
Join my weekly newsletter to receive my latest blog posts and bits, directly in your inbox.
Support me
I appreciate it if you would support me if have you enjoyed this post and found it useful, thank you in advance.