Drupal: how to set theme language programmatically?

10,987

Solution 1

PS at last I did it in template.php like this:

// get the list of availabel languages
$langs = language_list();
// now switch global $language back to 'en'
$vars['language'] = $langs['en'];

it solved some problems but there are still some problems alive. because of i am doing this in theme function, before the code some modules have used drupal defined language and it rises some problems like css directions and so.

If anyone has a better solution to do the code above somewhere perior to template.php i will thank.

Solution 2

The global $language determines the language. So if you change it you'll change the language. But that will be a language switch and not just for a page. It might be possible to switch back and forth. Haven't experimented with it though.

Solution 3

In Drupal 7, paste the following into template.php:

//Get the list of languages
$languages = language_list();

//Overwrite the global language object
global $language;
$language = $languages['en'];

More info here

Share:
10,987
Alexar
Author by

Alexar

Updated on June 05, 2022

Comments

  • Alexar
    Alexar almost 2 years

    How can i change drupal default language programmatically somewhere in code (like template.php)? (i need to overwrite default language set by admin in some cases.) i'm using drupal 6.

    PS: please read my own answer for more detail. and if you may help solve that

    PS: later i saw a module that was what i wanted. make sure take a look at it:

    Administration Language Drupal Module