How do you translate custom theme settings in Drupal 8?

I use hook_form_system_theme_settings_alter to add custom settings to a theme. One setting is the text string (e.g. 'Menu') to display beside the menu icon for a responsive menu. How can I integrate with the configuration translation system to allow the text to be translated?

All of the examples I have found describe how to translate a custom module's settings. I have not found any examples about how to translate a themes's settings, but there are a few hints in the Drupal code base that suggest it should be possible to create a THEME.config_translation.yml file.

I have taken a lead from this article on Gábor Hojtsy's blog and created the following files:

  • config/install/mytheme.settings.yml
  • config/schema/mytheme.schema.yml
  • mytheme.config_translation.yml

The schema is valid (tested using Configuration Inspector) and I have managed to get a Translate button for my theme's settings to show up on the Configuration translation admin page (/admin/config/regional/config-translation). That's as far as I've got.

I don't know what to use for base_route_name in mytheme.config_translation.yml. Here's what I have:

  title: 'My Theme Settings'
  base_route_name: system.theme_settings

For base_route_name I used system.theme_settings which results in a Translate button that links to /admin/appearance/settings/translate, but clicking on it leads to an Access denied, I assume because there is no translate page for theme settings. Also, I think the base_route_name should be system.theme_settings_theme and the Translate button should map to /admin/appearance/settings/mytheme/translate, but I get an error when I try that.

Am I on the right track? How would I go about creating a /admin/appearance/settings/mytheme/translate page and integrate it with the config translation system?

  • 1
    – user21641
    Mar 8 '16 at 14:13
  • I'm getting the same error as above. How to enable translation for theme settings?
    – wilNev
    Jul 19 '19 at 11:43
  • 4
    check my answer here - it helped me to translate theme settings drupal.stackexchange.com/a/283930/50320 Jul 29 '19 at 16:44
  • 1
    Also, important to understand the difference between string and text. A string does not translate, while text can be translated. I haven't experimented much with this other than fixing a custom module where my setting was being translated every time I enabled/disabled a module and translations were updated. I do believe that the addition of translatable: true is also important. Mar 10 '20 at 4:40

Browse other questions tagged or ask your own question.