6

As in question, ofcourse I am asking about Drupal function for this job.

So I need function that get language string as param (e.g 'en','pl' etc) and returns me standard Drupal language object. Similar to global $language.

2 Answers 2

-1

I would use the following code; with mymodule_get_language_obj($name, FALSE) you get a language object only if it has been enabled.

function mymodule_get_language_obj($name, $all = TRUE) {
  if ($all) {
    $languages = language_list();
  }
  else {
    $languages = language_list('enabled');
    $languages = $languages[1];
  }

  return isset($languages[$name]) ? $languages[$name] : NULL;
}

Notice that on Drupal 7.43 (and possibly, on previous Drupal 7 versions), language_list() returns an array of objects, contrary to what reported in a comment posted in the documentation page.

When calling:

<?php
  $languages = language_list('enabled');
?>

You can expect the return value to be:

<?php
$languages = array(
    '1' => 
        array( 
            'en' => array('language'=>'en', 'name'=>'English', ... ), 
            'fr' => array('language'=> 'fr', 'name' => 'Francais', ... ),
        )
    );
?>

The index being 1 because it's the value of the 'enabled' column.

With Drupal 7.43, I obtained the following:

Locale module not installed

  • Output of print_r(language_list('enabled'));
    Array(
      [1] => Array(
        [en] => stdClass Object(
          [language] => en
          [name] => English
          [native] => English
          [direction] => 0
          [enabled] => 1
          [plurals] => 0
          [formula] =>
          [domain] =>
          [prefix] =>
          [weight] => 0
          [javascript] =>
        )
      )
    )
  • Output of print_r(language_list());
    Array(
      [en] => stdClass Object(
        [language] => en
        [name] => English
        [native] => English
        [direction] => 0
        [enabled] => 1
        [plurals] => 0
        [formula] =>
        [domain] =>
        [prefix] =>
        [weight] => 0
        [javascript] =>
      )
    )

Locale module installed, and Esperanto enabled as additional language

  • Output of print_r(language_list('enabled'));
    Array(
      [1] => Array(
        [en] => stdClass Object(
          [language] => en
          [name] => English
          [native] => English
          [direction] => 0
          [enabled] => 1
          [plurals] => 0
          [formula] =>
          [domain] =>
          [prefix] =>
          [weight] => 0
          [javascript] =>
        )
        [eo] => stdClass Object(
          [language] => eo
          [name] => Esperanto
          [native] => Esperanto
          [direction] => 0
          [enabled] => 1
          [plurals] => 0
          [formula] =>
          [domain] =>
          [prefix] => eo
          [weight] => 0
          [javascript] =>
        )
      )
    )
  • Output of print_r(language_list());
    Array(
      [en] => stdClass Object(
        [language] => en
        [name] => English
        [native] => English
        [direction] => 0
        [enabled] => 1
        [plurals] => 0
        [formula] =>
        [domain] =>
        [prefix] =>
        [weight] => 0
        [javascript] =>
      )
      [eo] => stdClass Object(
        [language] => eo
        [name] => Esperanto
        [native] => Esperanto
        [direction] => 0
        [enabled] => 1
        [plurals] => 0
        [formula] =>
        [domain] =>
        [prefix] => eo
        [weight] => 0
        [javascript] =>
      )
    )

 

The equivalent code for Drupal 8 would be the following one.

function mymodule_get_language_obj($name) {
  $languages = \Drupal::languageManager()->getLanguages();

  return isset($languages[$name]) ? $languages[$name] : NULL;
}

Note that Drupal 8 version of the code returns instances of the Locale class, not the stdClass class.

0
9

Use language_list() function:

<?php

function get_langugage_object($language) {
  $installed_languages = language_list();
  return $installed_languages[$language];
}

?>
1
  • 1
    this should be the correct answer because it actually gives you an object, the accepted answer however returns a string of the language....
    – Alex
    Oct 27, 2014 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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