0

I'm a bit hesitant to ask this, but I have a drupal 8 site with 2 languages enabled - English as default and German. I have URL prefixes 'en' and 'de' to detect the chosen language. When setting German as default, the menus are translated, fields etc. It all works reasonably well. However, none of strings I use in my custom modules are translated. A very simple example

class testController extends ControllerBase {

...
drupal_set_message(t("Sorry, that does not work"));
....
}

The string is not translated when using german as the site's default language. Furthermore in my twig templates {% trans %} Hello {% endtrans %} have absolutely no effect.

Is there something I am missing here or I have to specifically set up ? I have used google for hours but do not find any solution to this weird problem. I've also tried all kinds of language detection methods in the Drupal settings - no effect. I have modules: Configuration Translation, Content Translation, Interface Translation and Language enabled.

2
  • When you search for "Sorry, that" at /admin/config/regional/translate do you see your string? Dec 6, 2017 at 13:15
  • No I don't see it
    – theuni
    Dec 6, 2017 at 13:16

1 Answer 1

4

If the strings in your custom module are in English but the site's default language is in German, you will need to translate the strings in the admin UI:

/admin/config/regional/translate

If you search for the string on this page but don't see it, you will need to first visit the page/path on the site that causes the string to be displayed so that the string will be created for translation purposes. Then you can return to the translation page and translate the string.

To translate strings in a twig template, do it like this:

{{ 'This is a string to translate.'|trans }}
3
  • Thanks, I'll try that. The thing is, even if I keep english as the site's default language, but choose the path prefix 'de' in my URL, the string is not translated. I always thought that t() does translation on the fly.
    – theuni
    Dec 6, 2017 at 13:27
  • 1
    t() does translation on the fly, but you have to translate the string first. In a custom module, you can either 1) translate the string in the UI or 2) prepare a .po file and import that, but either way, you have to translate the string first before it will show up in another language. Dec 6, 2017 at 13:29
  • I wonder how I did miss that. Usually you only read about how easy using t() is. Anyway - that makes it clear. Thanks !
    – theuni
    Dec 6, 2017 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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