Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By l33roy on
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
Hi "window.location" has
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.
Thanks man I think
Thanks man
I think $js_variables is the best way.
Working now...
Another take
I tried this and it worked:
//Get current language and send to js
I did it this way too - it's goood.
I used the same method but to make the variable available to my theme.
Then to pick it up in the javascript on the theme side:
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.
Thanks it worked great for me
Thanks it worked great for me !
Développeur Drupal Freelance — Design Web & Intégration
Use lang attribute of html tag
Instead of passing in a setting or parsing the URL you can also use this code:
This works as all themes should render (at least) this info:
Nice solution. Very clean as
Nice solution. Very clean as it will alway be the value from $language->language.
The big question here, is
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.
Try
Try Drupal.settings.pathPrefix
The code could be something like this
Most efficient solution in
Most efficient solution in this thread, IMO. Thanks, @Bußmeyer!
Thanks a lot.
Thanks a lot.
This a really nice solution, because prefix not always set, so langcode can't be used here.
---------------
Andrii Podanenko
CEO, ITCare
Yeah, it works fine.
Thanks, i'm trying to use it in my code. It works fine, nice share.
jQuery
This works quite well for me:
Drupal.settings.currentlang
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.
I can't find Drupal.settings
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/").