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.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI 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:
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] =>
)
)
)
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] =>
)
)
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] =>
)
)
)
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.
Use language_list()
function:
<?php
function get_langugage_object($language) {
$installed_languages = language_list();
return $installed_languages[$language];
}
?>