array( 'arguments' => array('langcode' => array()), ), ); } /** * Language block * * This is a simple language switcher which knows nothing about translations */ function i18n_block($op = 'list', $delta = 0) { if ($op == 'list') { $blocks[0]['info'] = t('Language switcher'); } elseif($op == 'view') { $blocks['subject'] = t('Languages'); $query = drupal_query_string_encode($_GET, array('q')); $blocks['content'] = theme('item_list', i18n_get_links($_GET['q'], empty($query) ? NULL : $query)); } return $blocks; } /** * Implementation of hook_init() * * Will initialize language dependent variables * Modify rewriting conditions when viewing specific nodes */ function i18n_init(){ // If not in bootstrap, variable init if(!_i18n_is_bootstrap()){ //include drupal_get_path('module', 'i18n').'/i18n.inc'; i18n_variable_init(); } if (arg(0) == 'node') { if(isset($_POST['language']) && $_POST['language']) { $pagelang = $_POST['language']; } elseif( is_numeric(arg(1)) && $node = node_load(arg(1)) ) { // Node language when loading specific nodes $pagelang = $node->language; } if(isset($pagelang)) i18n_selection_mode('node', db_escape_string($pagelang)); } elseif(arg(0) == 'admin' && arg(0) == 'content' && user_access('administer all languages')) { // No restrictions for administration pages i18n_selection_mode('off'); } } /** * Implementation of hook_help(). */ function i18n_help($section = 'admin/help#i18n' ) { switch ($section) { case 'admin/help#i18n' : $output = '

'.t('This module provides support for multilingual content in Drupal sites:').'

'; $output .= ''; $output .= '

'. t('For more information please read the on-line help pages.', array('@i18n' =>'http://drupal.org/node/31631')) .'

'; return $output; case 'admin/settings/i18n': $output .= '

'.t('To enable multilingual support for specific content types go to !configure_content_types.', array('!configure_content_types' => l(t('configure content types'), 'admin/content/types'), )).'

'; return $output; } } /** * Implementation of hook_menu(). */ function i18n_menu() { $items[] = array( 'path' => 'admin/settings/i18n', 'title' => 'Multilingual system', 'description' => 'Configure multilingual content and translation.', 'callback' => 'drupal_get_form', 'callback arguments' => array('i18n_admin_settings'), 'access' => user_access('administer site configuration'), ); $items[] = array( 'path' => 'admin/settings/i18n/main', 'title' => 'Internationalization', 'type' => MENU_DEFAULT_LOCAL_TASK ); return $items; } /** * Implementation of hook_nodeapi(). */ /* function i18n_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { if (variable_get("i18n_node_$node->type", 0)) { switch ($op) { case 'load': return db_fetch_array(db_query("SELECT trid, language, status AS i18n_status FROM {i18n_node} WHERE nid=%d", $node->nid)); case 'insert': case 'update': db_query("DELETE FROM {i18n_node} WHERE nid=%d",$node->nid); if ($node->language){ // Assign a trid from the beginning db_query("INSERT INTO {i18n_node} (nid, trid, language, status) VALUES(%d, '%d', '%s', '%d')", $node->nid, $node->trid, $node->language, $node->i18n_status); } // Handle menu items. Fixes duplication issue and language for menu items which happens when editing nodes in languages other than current. if (isset($node->menu) && !$node->menu['delete'] && $node->menu['title']) { $item = $node->menu; $item['path'] = ($item['path']) ? $item['path'] : "node/$node->nid"; $item['type'] = $item['type'] | MENU_MODIFIED_BY_ADMIN; if ($item['mid']) { // Update menu item db_query("UPDATE {menu} SET pid = %d, path = '%s', title = '%s', description = '%s', weight = %d, type = %d, language = '%s' WHERE mid = %d", $item['pid'], $item['path'], $item['title'], $item['description'], $item['weight'], $item['type'], $node->language, $item['mid']); drupal_set_message(t('The menu item %title has been updated with node language.', array('%title' => $item['title']))); } elseif(SAVED_NEW == menu_save_item($item)) { // Creating new menu item with node language db_query("UPDATE {menu} SET language = '%s' WHERE mid = %d", $node->language, $item['mid']); drupal_set_message(t('The menu item %title has been added with node language.', array('%title' => $item['title']))); } menu_rebuild(); unset($node->menu); // Avoid further processing by menu module } // Pathauto integration. Dynamic replacement of variables to allow different patterns per language if (module_exists('path') && module_exists('pathauto')) { // Language for pathauto variables is either node language or default language $language = $node->language ? $node->language : i18n_default_language(); if ($language != i18n_get_lang()) { i18n_variable_init($language, 'pathauto_node'); } } break; case 'delete': db_query('DELETE FROM {i18n_node} WHERE nid=%d', $node->nid); break; case 'prepare': // Book pages, set the right language nodes and outlines if (arg(3) == 'parent' && is_numeric(arg(4)) && ($parent = node_load(arg(4))) && $parent->language) { $node->language = $parent->language; i18n_selection_mode('node', $parent->language); } break; } } } */ /** * Implementation of hook_user() * * Switch to user's language after login */ function i18n_user($op, &$edit, &$account, $category = NULL) { if($op == 'login' && $account->language) { $_SESSION['language'] = $account->language; i18n_get_lang($account->language); } } /** * Form builder function. * * TO DO: Add exclude paths for content selection * Some options have been removed from previous versions: * - Languages are now taken from locale module unless defined in settings file * - Language dependent tables are authomatically used if defined in settings file */ function i18n_admin_settings() { // Content selection options $form['selection'] = array( '#type' => 'fieldset', '#title' => t('Content selection'), //'#collapsible' => TRUE, //'#collapsed' => TRUE, ); $form['selection']['i18n_selection_mode'] = array( '#type' => 'radios', '#title' => t('Content selection mode'), '#default_value' => variable_get('i18n_selection_mode', 'simple'), '#options' => _i18n_selection_mode(), '#description' => t('Determines which content to show depending on language.'), ); return system_settings_form($form); } /** * Simple i18n API */ /** * Get language properties * * @param $code * Language code * @param $property * It may be 'name', 'native', 'ltr'... */ function i18n_language_property($code, $property = 'native') { $languages = language_list($property); return $languages[$code]; } /** * Get locale languages plus i18n language settings * * @param $key * Data to be returned, defaults to 'active' * 'active' => array of enabled languages with native name * 'enabled' => array of enabled languages with english name * 'name' => all languages defined in locale module with english name * 'site_default' => code of default site language */ function i18n_languages($key = 'active') { static $languages; if (!$languages) { if($languages = variable_get('i18n_languages', 0)) { foreach($languages['name'] as $code => $name) { if($languages['enabled'][$code]) { $languages['active'][$code] = $name; } } } else { // It is possible that languages are not initialized if (function_exists('locale_supported_languages')) { $languages = locale_supported_languages(); } else { // Worst case scenario: locale module not loaded, for cached pages, at least this won't break everything unset($languages); // There's some PHP bug: http://www.zend.com/zend/week/week98.php $languages['name'] = array('en' => 'English'); } $languages['site_default'] = key($languages['name']); $languages['active'] = $languages['name']; $languages['native'] = $languages['name']; } // Sort everything alphabetically asort($languages['name']); asort($languages['active']); asort($languages['native']); } return $key ? $languages[$key] : $languages; } /** * More i18n API */ /** * Get node language */ function i18n_node_get_lang($nid, $default = '') { $lang = db_result(db_query('SELECT language FROM {node} WHERE nid=%d',$nid)); return $lang ? $lang : $default ; } /** * Get allowed languages for node * * This allows node types to define its own language list implementing hook 'language_list' */ function i18n_node_language_list($node) { if ($languages = node_invoke($node, 'language_list')) { return $languages; // The node module manages its own language list } elseif(variable_get('i18n_node_'.$node->type, 0) == LANGUAGE_SUPPORT_EXTENDED) { return i18n_language_list(TRUE); // All defined languages } else { return i18n_language_list(); // All enabled languages } } /** * Function i18n_get_links * * Returns an array of links for all languages, with or without names/flags * * @param $path * Drupal internal path * @param $query * Query string * @param $names * Names to use for the links. Defaults to native language names */ function i18n_get_links($path = '', $query = NULL, $names = NULL) { if($path == i18n_frontpage()) { $path = ''; } $names = $names ? $names : i18n_languages('native'); foreach (array_keys(i18n_supported_languages()) as $lang){ $links[$lang]= theme('i18n_link', $names[$lang], i18n_path($path, $lang), $lang, $query); } return $links; } /** * i18n_selection_mode * * Allows several modes for query rewriting and to change them programatically * off = No language conditions inserted * simple = Only current language and no language * mixed = Only current and default languages * strict = Only current language * default = Only default language * user = User defined, in the module's settings page * params = Gets the stored params * reset = Returns to previous * custom = add custom where clause, like "%alias.language = 'en'" */ function i18n_selection_mode($mode= NULL, $params= NULL){ static $current_mode = 'simple'; static $current_value = ''; static $store = array(); if(!$mode) { return $current_mode; } elseif($mode == 'params'){ return $current_value; } elseif($mode == 'reset'){ list($current_mode, $current_value) = array_pop($store); //drupal_set_message("i18n mode reset mode=$current_mode value=$current_value"); } else { array_push($store, array($current_mode, $current_value)); $current_mode = $mode; $current_value = $params; } } // List of selection modes function _i18n_selection_mode(){ return array( 'simple' => t('Only current language and no language'), 'mixed' => t('Only current and default languages and no language'), 'default' => t('Only default language and no language'), 'strict' => t('Only current language'), 'off' => t('All content. No language conditions apply'), ); } // List of language support modes for content function _i18n_content_languages() { return array( LANGUAGE_SUPPORT_NONE => t('Disabled'), LANGUAGE_SUPPORT_NORMAL => t('Normal - All enabled languages will be allowed.'), LANGUAGE_SUPPORT_EXTENDED => t('Extended - All defined languages will be allowed.') ); } /** * @name Themeable functions * @{ */ /** * Produces a language link with the right flag */ function theme_i18n_link($text, $target, $lang, $query= NULL, $fragment = NULL){ $output = ''; $attributes = ($lang == i18n_get_lang()) ? array('class' => 'active') : NULL; $output .= l(theme('i18n_language_icon', $lang), $target, $attributes, $query, $fragment, FALSE, TRUE); $output .= " "; $output .= l($text, $target, $attributes, $query, $fragment, FALSE, TRUE); $output .= ''; return $output; } /* @} */ /** * Implementation of hook_db_rewrite_sql() */ function i18n_db_rewrite_sql($query, $primary_table, $primary_key){ switch ($primary_table) { case 'n': case 'node': // Node queries. // No rewrite for translation module queries if (preg_match("/.*FROM {node} WHERE.* tnid/", $query)) return; // When loading specific nodes, language conditions shouldn't apply if (preg_match("/WHERE.*\s$primary_table.nid\s*=\s*(\d|%d)/", $query)) return; // If language conditions already there, get out if (preg_match("/i18n/", $query)) return; //$result['join'] = "LEFT JOIN {i18n_node} i18n ON $primary_table.nid = i18n.nid"; $result['where'] = i18n_db_rewrite_where($primary_table, 'node'); return $result; } } /** * Rewrites queries depending on rewriting mode */ function i18n_db_rewrite_where($alias, $type, $mode = NULL){ if (!$mode) { // Some exceptions for query rewrites $mode = i18n_selection_mode(); // drupal_set_message("i18n_db_rewrite mode=$mode query=$query"); } // Special case. Selection mode is 'strict' but this should be only for node queries if ($mode == 'strict' && $type != 'node') { $mode = 'simple'; } switch($mode){ case 'off': return ''; case 'simple': return "$alias.language ='".i18n_get_lang()."' OR $alias.language ='' OR $alias.language IS NULL" ; case 'mixed': return "$alias.language ='".i18n_get_lang()."' OR $alias.language ='".i18n_default_language()."' OR $alias.language ='' OR $alias.language IS NULL" ; case 'strict': return "$alias.language ='".i18n_get_lang()."'" ; case 'node': case 'translation': return "$alias.language ='".i18n_selection_mode('params')."' OR $alias.language ='' OR $alias.language IS NULL" ; case 'default': return "$alias.language ='".i18n_default_language()."' OR $alias.language ='' OR $alias.language IS NULL" ; case 'custom': return str_replace('%alias', $alias, i18n_selection_mode('params')); } } /** * Implementation of hook_exit */ function i18n_exit(){ _i18n_variable_exit(); } /** * Implementation of hook_form_alter * * This is the place to add language fields to all forms */ function i18n_form_alter(&$form, $form_state, $form_id) { //drupal_set_message("DEBUG: i18n_form_alter form_id=$form_id "); switch($form_id){ case 'node_type_form': $node_type = $form['old_type']['#value']; // Build a fieldset to allow some more options here $form['workflow']['i18n'] = array( '#type' => 'fieldset', '#title' => t('Multilingual options'), ); $form['workflow']['i18n']['i18n_node'] = array( '#type' => 'radios', '#title' => t('Multilingual content'), '#default_value' => variable_get('i18n_node_'.$node_type, 0), '#options' => _i18n_content_languages(), '#description' => t('Enables language field and translations for this content type.'), ); break; default: // Node edit form /** @ TO DO Upgrade if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id && $node = $form['#node']) { // Language field if(variable_get('i18n_node_'.$form['type']['#value'], 0) && !isset($form['i18n']['language'])) { // Language field $form['i18n'] = array('#type' => 'fieldset', '#title' => t('Multilingual settings'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#weight' => -4); // Language will default to current only when creating a node $language = isset($form['#node']->language) ? $form['#node']->language : (arg(1)=='add' ? i18n_get_lang() : ''); $form['i18n']['language'] = _i18n_language_select($language, t('If you change the Language, you must click on Preview to get the right Categories & Terms for that language.'), -4, i18n_node_language_list($node)); $form['i18n']['trid'] = array('#type' => 'value', '#value' => $form['#node']->trid); } // Correction for lang/node/nid aliases generated by path module // if($form['#node']->path && $form['#node']->path == i18n_get_lang().'/node/'.$form['#node']->nid){ if($node->path) { $alias = drupal_lookup_path('alias', 'node/'.$node->nid); if($alias && $alias != 'node/'.$node->nid){ $form['#node']->path = $alias; } else { unset($form['#node']->path); } } } */ // Multilingual variables in settings form if (isset($form['#theme']) && $form['#theme'] == 'system_settings_form' && $variables = variable_get('i18n_variables', 0)) { if (i18n_form_alter_settings($form, $variables)) { $form['#submit'][] = 'i18n_variable_form_submit'; } } } } /** * Implementation of hook_perm(). * * Permissions defined * - administer all languages * Disables language conditions for administration pages, so the user can view objects for all languages at the same time. * This applies for: menu items, taxonomy */ function i18n_perm() { return array('administer all languages'); } /** * Process menu and menu item add/edit form submissions. */ function i18n_menu_edit_item_form_submit($form_id, $form_values) { $mid = menu_edit_item_save($form_values); db_query("UPDATE {menu} SET language = '%s' WHERE mid = %d", $form_values['language'], $mid); return 'admin/build/menu'; } /** * Theme the locale admin manager form. */ function theme_i18n_admin_manage_screen($form) { foreach ($form['name'] as $key => $element) { // Do not take form control structures. if (is_array($element) && element_child($key)) { $rows[] = array(check_plain($key), drupal_render($form['name'][$key]), drupal_render($form['native'][$key]), drupal_render($form['enabled'][$key]), drupal_render($form['site_default'][$key]), drupal_render($form['rtl'][$key]),($key != 'en' ? drupal_render($form['translation'][$key]) : t('n/a')), ($key != 'en' ? l(t('delete'), 'admin/settings/locale/language/delete/'. $key) : '')); } } $header = array(array('data' => t('Code')), array('data' => t('English name')), array('data' => t('Native name')), array('data' => t('Enabled')), array('data' => t('Default')), array('data' => t('RTL')), array('data' => t('Translated')), array('data' => t('Operations'))); $output = theme('table', $header, $rows); $output .= drupal_render($form); return $output; } /** * Save language settings in variable */ function i18n_admin_manage_screen_submit($form_id, $form_values) { // Save changes to existing languages. foreach(array('site_default', 'name', 'rtl', 'native', 'enabled') as $key) { $save[$key] = $form_values[$key]; } $save['name']['en'] = 'English'; $languages = locale_supported_languages(FALSE, TRUE); foreach($languages['name'] as $key => $lang) { // Nothing } variable_set('i18n_languages', $save); } /** * Check for multilingual variables in form */ function i18n_form_alter_settings(&$form, &$variables) { $result = 0; foreach (element_children($form) as $field) { if(isset($form[$field]['#type']) && $form[$field]['#type'] == 'fieldset') { $result += i18n_form_alter_settings($form[$field], $variables); } elseif (in_array($field, $variables)) { $form[$field]['#description'] .= ' '.t('This is a multilingual variable.').''; $result++; } } return $result; } /** * Save multilingual variables and remove them from form */ function i18n_variable_form_submit($form, &$form_state) { $form_values = $form_state['values']; $op = isset($form_values['op']) ? $form_values['op'] : ''; $variables = variable_get('i18n_variables', array()); $language = i18n_get_lang(); foreach ($form_values as $key => $value) { if (in_array($key, $variables)) { if ($op == t('Reset to defaults')) { i18n_variable_del($key, $language); } else { if (is_array($value) && isset($form_values['array_filter'])) { $value = array_keys(array_filter($value)); } i18n_variable_set($key, $value, $language); } unset($form_values[$key]); } } // Re-submit form // system_settings_form_submit($form_id, $form_values); } /** * Initialization of multilingual variables * * @param $language * Language to retrieve variables. Defaults to current language * @param $prefix * Variable name prefix to load just a selected group of variables */ function i18n_variable_init($language = NULL, $prefix = ''){ global $conf; global $i18n_conf; $language = $language ? $language : i18n_get_lang(); if ($i18n_variables = variable_get('i18n_variables', '')){ if (!$i18n_conf) { $i18n_conf = array(); } $variables = _i18n_variable_init($language, $prefix); foreach($i18n_variables as $name){ $i18n_conf[$name] = isset($variables[$name]) ? $variables[$name] : (isset($conf[$name]) ? $conf[$name] : ''); } $conf = array_merge($conf, $i18n_conf); } } /** * Helper function to create language selector */ function _i18n_language_select($value ='', $description ='', $weight = -20, $languages = NULL){ $languages = $languages ? $languages : i18n_language_list(); return array( '#type' => 'select', '#title' => t('Language'), '#default_value' => $value, '#options' => array_merge(array('' => ''), $languages), '#description' => $description, '#weight' => $weight, ); } /** * Load language variables into array */ function _i18n_variable_init($language, $prefix = ''){ $variables = array(); $cacheid = 'variables:'.$language.($prefix ? ':'.$prefix : ''); if ($cached = cache_get($cacheid)) { $variables = $cached->data; } else { $result = db_query("SELECT * FROM {i18n_variable} WHERE language='%s' AND name LIKE '%s%'", $language, $prefix); while ($variable = db_fetch_object($result)) { $variables[$variable->name] = unserialize($variable->value); } cache_set($cacheid, $variables); } return $variables; } /** * Save multilingual variables that may have been changed by other methods than settings pages */ function _i18n_variable_exit(){ global $i18n_conf; global $conf; if($i18n_conf){ $lang = i18n_get_lang(); $refresh = FALSE; // Rewritten because array_diff_assoc may fail with array variables foreach($i18n_conf as $name => $value){ if($value != $conf[$name]) { $refresh = TRUE; $i18n_conf[$name] = $conf[$name]; db_query("DELETE FROM {i18n_variable} WHERE name='%s' AND language='%s'", $name, $lang ); db_query("INSERT INTO {i18n_variable} (language, name, value) VALUES('%s', '%s', '%s')", $lang, $name, serialize($conf[$name])); } } if($refresh) { cache_set('variables:'.$lang, 'cache', serialize($i18n_conf)); } } } /** * Check whether we are in bootstrap mode */ function _i18n_is_bootstrap(){ return !function_exists('drupal_get_headers'); } /** * Sets db_prefix to given language */ function _i18n_set_db_prefix($lang) { global $db_prefix, $db_prefix_i18n; if (is_array($db_prefix_i18n)) { $db_prefix = array_merge($db_prefix, str_replace('**', $lang, $db_prefix_i18n)); } } /** * To get the original path. * Cannot use $_GET["q"] cause it may have been already changed */ function _i18n_get_original_path() { return isset($_REQUEST["q"]) ? trim($_REQUEST["q"],"/") : ''; } /** * Drupal 6, backwards compatibility layer * @ TO DO Fully upgrade all the modules and remove */ /** * This one expects to be called first from common.inc */ function i18n_get_lang($setlanguage = NULL) { global $language; return $language->language; } /** * Returns default language */ function i18n_default_language(){ return language_default('language'); } /** * Language dependent front page * This function will search for aliases like 'en/home', 'es/home'... */ function i18n_frontpage($lang = NULL) { $lang = $lang ? $lang : i18n_get_lang(); return i18n_get_normal_path($lang.'/'.variable_get('site_frontpage','node')); } /** * Get localized language list, sort alphabetically * * @param $all * TRUE for all languages, not only enabled */ function i18n_language_list($field = 'name', $all = FALSE) { if ($all) { $languages = language_list(); } else { $languages = language_list('enabled'); $languages = $languages[1]; } $list = array(); foreach ($languages as $language) { $list[$language->language] = ($field == 'name') ? t($language->name) : $language->$field; } return $list; } /** * Get list of supported languages * @param $all * TRUE to get all defined languages */ function i18n_supported_languages($all = FALSE) { return i18n_language_list('native', $all); } /** * @defgroup i18n_api Extended language API * @{ * This is an extended language API to be used by modules in i18n package. */ /** * Set a persistent language dependent variable. * * @param $name * The name of the variable to set. * @param $value * The value to set. This can be any PHP data type; these functions take care * of serialization as necessary. * @param $langcode * Language code */ function i18n_variable_set($name, $value, $langcode) { global $conf, $i18n_conf; db_lock_table('i18n_variable'); db_query("DELETE FROM {i18n_variable} WHERE name = '%s' AND language='%s'", $name, $langcode); db_query("INSERT INTO {i18n_variable} (name, language, value) VALUES ('%s', '%s', '%s')", $name, $langcode, serialize($value)); db_unlock_tables(); cache_clear_all('variables:'.$langcode, 'cache'); $conf[$name] = $value; $i18n_conf[$name] = $value; } /** * Unset a persistent multilingual variable. * * @param $name * The name of the variable to undefine. * @param $langcode * Language code */ function i18n_variable_del($name, $langcode) { global $conf, $i18n_conf; db_query("DELETE FROM {i18n_variable} WHERE name = '%s' AND language='%s'", $name, $langcode); cache_clear_all('variables:'.$langcode, 'cache'); unset($conf[$name]); unset($i18n_conf[$name]); } /** * Utility. Get part of array variable */ function i18n_array_variable_get($name, $element, $default = NULL) { if (($values = variable_get($name, array())) && isset($values[$element])) { return $values[$element]; } else { return $default; } } /** * Utility. Set part of array variable */ function i18n_array_variable_set($name, $element, $value) { $values = variable_get($name, array()); $values[$element] = $value; variable_set($name, $values); } /** * @} End of "defgroup i18n_api". */