Thomashdk
Thomashdk

Reputation: 13

Drupal translation jquery

I'm trying to use Drupals translation. '/admin/config/regional/translate'. When I use the code in the bottom, the word are showing in the list and I can specify the translation.

But when I use the variable in may jQuery code i'm getting an error.

Cannot read property 't' of undefined

Maybe I need to define t ? or include it? I'm using Drupal 8

Doc: https://www.drupal.org/docs/7/api/localization-api/translating-strings-in-javascript

;(function ($, Drupal) {
    $(function () {

    function addLeadForm(){
        var title = Drupal.t("Fill to get demoversion");
        var firstName = Drupal.t("firstname");
        var lastName = Drupal.t("lastname");
        var email = Drupal.t("email");

   });
})(jQuery);

Thanks

Upvotes: 1

Views: 2695

Answers (1)

norman.lol
norman.lol

Reputation: 5374

I guess you forgot to include the right dependencies in your *.libraries.yml.

Here comes an example from menu_ui.js:

(function ($, Drupal) {
  Drupal.behaviors.menuUiDetailsSummaries = {
    attach: function attach(context) {
      $(context).find('.menu-link-form').drupalSetSummary(function (context) {
        var $context = $(context);
        if ($context.find('.js-form-item-menu-enabled input').is(':checked')) {
          return Drupal.checkPlain($context.find('.js-form-item-menu-title input').val());
        }

        return Drupal.t('Not in menu');
      });
    }
  };

And the corresponding menu_ui.libraries.yml:

drupal.menu_ui:
  version: VERSION
  js:
    menu_ui.js: {}
  dependencies:
    - core/jquery
    - core/drupal
    - core/drupal.form

Upvotes: 3

Related Questions