When creating functional tests (extending BrowserTestBase) for a D8 site, how would I go about changing the default language of the site being temporarily created by BrowserTestBase?

I'm trying to run tests on a site in a non-English language, and I'd like to avoid confusion by using English terms etc. in test cases.

As far as I see, Drupal/Core/Test/FunctionalTestSetupTrait.php sets the default langcode to be 'en' (in the installParameters method) and there's no option to specify custom installation parameters. Besides hacking this manually, is there a more elegant, "proper" way of changing this behavior?

If that's not possible, then would it be possible to change the site's language after it's created, maybe in the setUp method of the test class?


I think there are a few ways. The simplest might be to override the installParameters() method to specify your desired language:

protected function installParameters() {
  // This will come back with the langcode set to `en`.
  $parameters = parent::installParameters();

  // Update to desired langcode:
  $parameters['parameters']['langcode'] = 'es';

  return $parameters;

I didn't find any tests in core that did it this way, but there are a few that change it during setup via the UI. (This example from LanguagePathMonolingualTest)

// Make French the default language.
$edit = [
  'site_default_language' => 'fr',
$this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.