4

I know this question may get the dafs-adrenalin of some of you viewers going. But I'm just recently getting my head in there with php and drupal and have been searching the api and googled a possible function only to find functions that did all sorts of other stuff.

It's that I have this custom logic where I want to change the language object after I've set a $_COOKIE['lang']. The value of the cookie lang would be a language code like en or fr. And going $language->language -> $_COOKIE['lang']; is pretty rediculous.

So the question maybe then is: I Know the value of $language->language, now I want to use the function that Drupal uses in core to construct the entire language object. This way the language object actually changes after the cookies is set and the locale module can to it's work to load the site in the new language?

  • Sorry what's the question? Are you looking for a Drupal API function that will assign the language set by you in the $_COOKIE back to the global $language object automatically? – Clive Feb 27 '12 at 10:28
  • Yes that is correct. I was thinking i18n API but like you say it must be the Drupal API that will provide me this function to assign the language set by the $_COOKIE back to the global $language automatically, yes indeed. – Immers Feb 27 '12 at 10:43
  • I very much doubt there will be...why not just write one? function reassign_language() { global $language; $language->language = $_COOKIE['lang']; } There's no reason that Drupal would include a function to handle/reverse the logic in custom code that you've written – Clive Feb 27 '12 at 10:45
  • Yes but that would keep the global $language in it's original language and just change the value of that $language->language attribute to the new one provided by that cookie. So after I would have done this assignment like I posed in my question and would set in your function, I would go global $language; var_dump($language); In my browserwindow, this var_dump would then return something like object(stdClass)#3 (11) { ["language"]=> string(2) "fr" ["name"]=> string(5) "Dutch"...and so on, with all the values of the rest of the keys in the array still set to dutch. – Immers Feb 27 '12 at 10:51
  • OK I get you, I'll add an answer below – Clive Feb 27 '12 at 11:32
6

The list of language objects is built up by the core function language_list(), which you can use to overwrite the global $language object if you need to:

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

// Set up the new language code
$new_lang_code = 'fr';

// Make sure the required language object is actually set
if (isset($languages[$new_lang_code])) {
  // Overwrite the global language object
  global $language;
  $language = $languages[$new_lang_code];
}
|improve this answer|||||

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.