How can i get current language in jquery.ajax.cart.js file? Is it possible?
.module file:
drupal_add_js( drupal_get_path('module', 'uc_ajax_cart') .'/js/jquery.ajax.cart.js' );
Maybe parsing the url? I use: base_url() - default language, base_url().'/en' - English language.
Help please...

Comments

shashikant52004’s picture

Hi
"window.location" has what's in the address bar of your browser.
If that doesn't work, try window.location.href. you might want to do some parsing in javascript level to get exactly what you need

also you can pass the variable from module file and use it in js file.
e.g.

$js_variables = array(
'first_var' => arg(1),
'second_var' => $url_lang,
);
drupal_add_js(array('modulename' => $js_variables), "setting");

and access it using
var js_first_var = Drupal.settings.modulename.first_var;
var js_second_var = Drupal.settings.modulename.second_var;

thanks,
Shashikant.

l33roy’s picture

Thanks man
I think $js_variables is the best way.
Working now...

iamjon’s picture

I tried this and it worked:

//Get current language and send to js

global $language;
$thelang = $language->language;
$js_variables = array('thelang' => $thelang,);
drupal_add_js(array('mymodulename' => $js_variables), "setting");
Bensbury’s picture

I used the same method but to make the variable available to my theme.

global $language ;
	
$lang_name = $language->language ;
drupal_add_js(array('swflang' => $lang_name ), 'setting');

Then to pick it up in the javascript on the theme side:

var lang = Drupal.settings.swflang;
alert('lang');

Remove the alert when it works and do your stuff.

Also for non-php people who might want to know what language properties are available, drop

print_r($language);

under the global line.
This will print out all the properties in the global language object so you can see what is happening.
this might be useful if you want the full name instead of the language abbreviation.

I thought I'd mention this as knowing this might help a lot of people out who want something similar but a little different.

Nicolas Bouteille’s picture

fietserwin’s picture

Instead of passing in a setting or parsing the URL you can also use this code:

  var lang = $('html').attr('lang');

This works as all themes should render (at least) this info:

<html class="" lang="<?php print $language->language; ?>" dir="<?php print $language->dir; ?>">
Jaypan’s picture

Nice solution. Very clean as it will alway be the value from $language->language.

Jaypan’s picture

The big question here, is whether the language should even be needed. Generally, the current language is too specific, and will miss certain cases. For example, if it is text that the user wants to change by language, they should be using the Drupal.t() function for translations, or else they will bypass Drupal's multilingual API. If the user wants current language for layout, it may be something that covers more languages may be preferable. For example, targeting right-to-left (rtl) rather than targeting Arabic or Hebrew for example.

For future posters to this thread, you should include your purpose for wanting the current language, to determine if there is a better solution to what you are trying.

Bußmeyer’s picture

Try Drupal.settings.pathPrefix

The code could be something like this

$.ajax({
  url: Drupal.settings.basePath + Drupal.settings.pathPrefix + 'yoururl/ajax/' + $(this).attr('param'),
  dataType: 'json'
}).done(function(data) {
  $('regionBottom').html(data['content']);
});
Konstantin Komelin’s picture

Most efficient solution in this thread, IMO. Thanks, @Bußmeyer!

podarok’s picture

Thanks a lot.
This a really nice solution, because prefix not always set, so langcode can't be used here.

---------------
Andrii Podanenko
CEO, ITCare

cheans’s picture

Thanks, i'm trying to use it in my code. It works fine, nice share.

knalstaaf’s picture

This works quite well for me:

var currentlang = jQuery('html').attr('lang');
alert(currentlang);
pritam.tiwari’s picture

Hi,

Following setting variable will give you the language used in Drupal page.

Drupal.settings.currentlang seems not available in Drupal 8

Try using 

drupalSettings.langCode

Alternatively one can check the html tag with language attribute.

biblos’s picture

I can't find Drupal.settings.currentlang , but there is  Drupal.settings.pathPrefix,

which is empty for default language and

contains language code for other (like "uk/", "de/").