'select',
'#title' => t('Language'),
'#default_value' => $default,
'#options' => array(LANGUAGE_NONE => '') + i18n_language_list(),
);
}
/**
* Get language field for hook_fields_extra_fields()
*/
function i18n_language_field_extra() {
return array(
'form' => array(
'language' => array(
'label' => t('Language'),
'description' => t('Language selection'),
'weight' => 0,
),
),
'display' => array(
'language' => array(
'label' => t('Language'),
'description' => t('Language'),
'weight' => 0,
),
),
);
}
/**
* Get full language list
*
* @todo See about creating a permission for seeing disabled languages
*/
function i18n_language_list($field = 'name', $mode = NULL) {
$mode = isset($mode) ? $mode : variable_get('i18n_language_list', I18N_LANGUAGE_ENABLED);
return locale_language_list($field, I18N_LANGUAGE_EXTENDED & $mode);
}
/**
* Get language name for any defined (enabled or not) language
*
* @see locale_language_list()
*/
function i18n_language_name($lang) {
$list = &drupal_static(__FUNCTION__);
if (!isset($list)) {
$list = locale_language_list('name', TRUE);
}
if (!$lang || $lang === LANGUAGE_NONE) {
return t('Undefined');
}
elseif (isset($list[$lang])) {
return check_plain($list[$lang]);
}
else {
return t('Unknown');
}
}
/**
* Get valid language code for current page
*/
function i18n_langcode($langcode = NULL) {
return $langcode && $langcode !== LANGUAGE_NONE ? $langcode : i18n_language()->language;
}
/**
* Implements hook_help().
*/
function i18n_help($path = 'admin/help#i18n', $arg) {
switch ($path) {
case 'admin/help#i18n' :
$output = '
'. t('This module improves support for multilingual content in Drupal sites:') .'
';
$output .= '';
$output .= '- '. t('Shows content depending on page language.') .'
';
$output .= '- '. t('Handles multilingual variables.') .'
';
$output .= '- '. t('Extended language option for chosen content types. For these content types transations will be allowed for all defined languages, not only for enabled ones.') .'
';
$output .= '- '. t('Provides a block for language selection and two theme functions: i18n_flags and i18n_links.') .'
';
$output .= '
';
$output .= ''. t('This is the base module for several others adding different features:') .'
';
$output .= '';
$output .= '- '. t('Multilingual menu items.') .'
';
$output .= '- '. t('Multilingual taxonomy adds a language field for taxonomy vocabularies and terms.') .'
';
$output .= '
';
$output .= ''. t('For more information, see the online handbook entry for Internationalization module.', array('@i18n' => 'http://drupal.org/node/133977')) .'
';
return $output;
case 'admin/config/language/i18n':
$output = '';
$output .= '- '. t('To enable multilingual support for specific content types go to configure content types.', array('@configure_content_types' => url('admin/structure/types'))) .'
';
$output .= '
';
return $output;
}
}
/**
* Implements hook_menu().
*/
function i18n_menu() {
$items['admin/config/regional/i18n'] = array(
'title' => 'Multilingual settings',
'description' => 'Configure extended options for multilingual content and translations.',
'page callback' => 'drupal_get_form',
'page arguments' => array('variable_module_form', 'i18n'),
'access arguments' => array('administer site configuration'),
'weight' => 10,
);
$items['admin/config/regional/i18n/configure'] = array(
'title' => 'Multilingual system',
'description' => 'Configure extended options for multilingual content and translations.',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
return $items;
}
/**
* Simple i18n API
*/
/**
* Switch select Mode on off if enabled
*/
function i18n_select($value = NULL) {
static $mode;
if (isset($value)) {
$mode = $value;
}
return $mode;
}
/**
* Get language properties.
*
* @param $code
* Language code.
* @param $property
* It may be 'name', 'native', 'ltr'...
*/
function i18n_language_property($code, $property) {
$languages = language_list();
return isset($languages[$code]->$property) ? $languages[$code]->$property : NULL;
}
/**
* Implements hook_preprocess_html().
*/
function i18n_preprocess_html(&$variables) {
global $language;
$variables['classes_array'][] = 'i18n-' . $language->language;
}
/**
* Translate or update user defined string. Entry point for i18n_string API if enabled.
*
* @param $name
* Textgroup and context glued with ':'.
* @param $default
* String in default language. Default language may or may not be English.
* @param $options
* An associative array of additional options, with the following keys:
* - 'langcode' (defaults to the current language) The language code to translate to a language other than what is used to display the page.
* - 'filter' Formatting or filtering callback to apply to the translated string only
* - 'callback' Callback to apply to the result (both to translated or untranslated string
* - 'update' (defaults to FALSE) Whether to update source table
* - 'translate' (defaults to TRUE) Whether to return a translation
*
* @return $string
* Translated string, $string if not found
*/
function i18n_string($name, $string, $options = array()) {
$options += array('translate' => TRUE, 'update' => FALSE);
if ($options['update']) {
$result = function_exists('i18n_string_update') ? i18n_string_update($name, $string, $options) : FALSE;
}
if ($options['translate']) {
$result = function_exists('i18n_string_translate') ? i18n_string_translate($name, $string, $options) : $string;
}
return $result;
}
/**
* Get language from context.
*
* Depending on the page content we may need to use a different language for some operations.
*/
function i18n_context_language() {
// Get language from the first module that provides it
foreach (module_implements('i18n_context_language') as $module) {
if ($language = module_invoke($module, 'i18n_context_language')) {
return $language;
}
}
return i18n_language();
}
/**
* Get object language code
*
* @param $object
* Object or array having language field or plain language field
* @param $default
* What value to return if the object doesn't have a valid language
*/
function i18n_object_langcode($object, $default = FALSE, $field = 'language') {
$value = i18n_object_field($object, $field, $default);
return $value && $value != LANGUAGE_NONE ? $value : $default;
}
/**
* Get translation information for objects
*/
function i18n_object_info($type = NULL) {
$info = &drupal_static(__FUNCTION__);
if (!$info) {
$info = module_invoke_all('i18n_object_info');
drupal_alter('i18n_object_info', $info);
}
if ($type) {
return isset($info[$type]) ? $info[$type] : array();
}
else {
return $info;
}
}
/**
* Get field value from object/array
*/
function i18n_object_field($object, $field, $default = NULL) {
if (is_object($object)) {
return isset($object->$field) ? $object->$field : $default;
}
elseif (is_array($object)) {
return isset($object[$field]) ? $object[$field] : $default;
}
else {
return $default;
}
}
/**
* Build translation link
*/
function i18n_translation_link($path, $langcode, $link = array()) {
$language = i18n_language($langcode);
$link += array(
'href' => $path,
'title' => $language->native,
'language' => $language,
'i18n_translation' => TRUE,
);
$link['attributes']['class'] = array('language-link');
// @todo Fix languageicons weight, but until that
if (function_exists('languageicons_link_add')) {
languageicons_link_add($link);
}
return $link;
}