1

I have an ajax function in the javascript file that calls for the data in a custom module. But the problem is it always get the data for the default language. In my case it is 'en'. How do I get the data for another language.

$language = \Drupal::languageManager()->getCurrentLanguage()->getId(); 

This method always returns the default language for ajax requests. How do I get the data for the language that is mentioned in the url.

ajax:

$.ajax({
    url: "/gallery/view", 
    method :'POST',
    data: {
        category : category,
        propertyId : propertyId,
        isCorporate : isCorporate,
        brandId : brandId,
    },
    success: function(result){}
});

routing :

eme_gallery.view:
  path: '/gallery/view'
  defaults:
      _controller: 'Drupal\eme_gallery\Controller\ViewGalleryImage::view'
      _title: 'Image Gallery'
  requirements:
      _permission: 'access content'
2
  • How is the langauge for your site determined? URL? Prefix? Session?
    – Jaypan
    Aug 23, 2018 at 13:02
  • @Jaypan from the url prefix. eg: www.url.com/ja/html Aug 23, 2018 at 13:03

1 Answer 1

1

You will need to change the URL of your AJAX call to include the language prefix. So this:

url: "/gallery/view", 

Will be this:

url: "/ja/gallery/view", 
2
  • This doesn’t work for the english language. If I write /en/ it doesn’t work Aug 24, 2018 at 4:18
  • Do not include any prefix (en, ja etc) for the default language. You would only use /gallery/view
    – Jaypan
    Aug 24, 2018 at 4:57

Your Answer

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

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