13.11.2025 | Michael Ebert

How to alter the langcode of a field for accessibility reasons

my_module.module
function my_module_preprocess_field(&$variables): void {
  if ($variables['element']['#bundle'] == 'my_bundle') {
    if (in_array($variables['element']['#field_name'], ['my_field'])) {
      $variables['attributes']['lang'] = 'en';
    }
  }
}

Issue:

You want to assign a language to a certain part of your node which differs from the set content language. That could be the case if you need to make it more readable for disabled persons who use a screenreader.

Solution:

Use a preprocess_field hook and set the lang attribute of that field to e.g. "en" for English. We only want this to work on nodes from the "my_bundle" type.

Similar results may also be achieved using the CKEditor Text part language plugin.