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.


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

routing :

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

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", 
| improve this answer | |
  • This doesn’t work for the english language. If I write /en/ it doesn’t work – i am batman Aug 24 '18 at 4:18
  • Do not include any prefix (en, ja etc) for the default language. You would only use /gallery/view – Jaypan Aug 24 '18 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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