language;
}
/**
* Implements hook_init().
*
* Special fix for site_frontpage, that may have been used before the language variables are loaded.
*/
/*
function i18n_init() {
// If not in bootstrap, variable init. Otherwise we are serving a cached page so we don't need anything else.
if (!_i18n_is_bootstrap()) {
_i18n_init_mode();
}
}
*/
/**
* Initialize selection mode
*/
function _i18n_init_mode() {
if (i18n_selection_mode() != 'off') {
// Node language when loading specific nodes or creating translations.
if (arg(0) == 'node' ) {
if (($node = menu_get_object('node')) && $node->language) {
i18n_selection_mode('node', $node->language);
}
elseif (arg(1) == 'add' && !empty($_GET['translation']) && !empty($_GET['language'])) {
i18n_selection_mode('translation', db_escape_string($_GET['language']));
}
}
elseif (arg(0) == 'admin') {
// There are some exceptions for admin pages.
if (arg(1) == 'content' && user_access('administer all languages')) {
// No restrictions for administration pages.
i18n_selection_mode('off');
}
elseif (arg(1) == 'build' && (arg(2) == 'menu-customize' || arg(2) == 'menu')) {
// All nodes available when editing custom menu items.
i18n_selection_mode('off');
}
}
}
}
/**
* 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('i18n_admin_settings'),
'access arguments' => array('administer site configuration'),
'file' => 'i18n.admin.inc',
'weight' => 10,
);
$items['admin/config/regional/i18n/configure'] = array(
'title' => 'Multilingual system',
'description' => 'Configure extended options for multilingual content and translations.',
//'page callback' => 'drupal_get_form',
//'page arguments' => array('i18n_admin_settings'),
//'access arguments' => array('administer site configuration'),
'file' => 'i18n.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_modules_installed()
*
* Invoke hook_i18n_update_drupal6()
*/
function i18n_modules_installed($modules) {
if (variable_get('i18n_drupal6_update')) {
foreach ($modules as $module) {
if (strpos($module, 'i18n') === 0) {
module_load_install($module);
module_invoke($module, 'i18n_update_drupal6');
}
}
}
}
/**
* Implements hook_user_login().
*
* Switch to user's language after login.
*/
function i18n_user_login(&$edit, &$account) {
if ($account->language) {
$_SESSION['language'] = $account->language;
i18n_get_lang($account->language);
}
}
/**
* Simple i18n API
*/
/**
* 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;
}
/**
* Selection mode for content.
*
* Warning: when used with params they need to be escaped, as some values are thrown directly in queries.
*
* 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'".
*
* @param $reset
* Whether to reset the internal cache for the selection mode.
*/
function i18n_selection_mode($mode = NULL, $params = NULL, $reset = FALSE) {
$current_mode = &drupal_static(__FUNCTION__ . '_mode', NULL, $reset);
$current_mode = &drupal_static(__FUNCTION__ . '_value', '', $reset);
$store = &drupal_static(__FUNCTION__ . '_store', '', $reset);
// Initialization, first time this runs
if (!isset($current_mode)) {
$current_mode = variable_get('i18n_selection_mode', 'simple');
}
if (!$mode) {
return $current_mode;
}
elseif ($mode == 'params') {
return $current_value;
}
elseif ($mode == 'reset') {
list($current_mode, $current_value) = array_pop($store);
}
else {
array_push($store, array($current_mode, $current_value));
$current_mode = $mode;
$current_value = $params;
}
}
/**
* Rewrites queries depending on rewriting mode.
*
* @return DatabaseCondition
*/
function i18n_db_rewrite_where($alias, $type, $mode = NULL) {
if (!$mode) {
// Some exceptions for query rewrites.
$mode = i18n_selection_mode();
}
// Get languages to simplify query building.
$current = i18n_language()->language;
$default = language_default('language');
if ($mode == 'strict' && $type != 'node') {
// Special case. Selection mode is 'strict' but this should be only for node queries.
$mode = 'simple';
}
elseif ($mode == 'mixed' && $current == $default) {
// If mode is mixed but current = default, is the same as 'simple'.
$mode = 'simple';
}
switch ($mode) {
case 'off':
return '';
case 'simple':
return db_condition('OR')
->condition($alias . '.language', $current)
->condition($alias . '.language', '')
->condition($alias . '.language', NULL);
case 'mixed':
return db_condition('OR')
->condition($alias . '.language', $current)
->condition($alias . '.language', $default)
->condition($alias . '.language', '')
->condition($alias . '.language', NULL);
case 'strict':
return db_condition('AND')->condition($alias . '.language', $current);
case 'node':
case 'translation':
return db_condition('OR')
->condition($alias . '.language', i18n_selection_mode('params'))
->condition($alias . '.language', '')
->condition($alias . '.language', NULL);
case 'default':
return db_condition('OR')
->condition($alias . '.language', $default)
->condition($alias . '.language', '')
->condition($alias . '.language', NULL);
case 'custom':
return str_replace('%alias', $alias, i18n_selection_mode('params'));
}
}
/**
* Implements hook_preprocess_page().
*
* Add the language code to the classes for the tag. Unfortunately, some
* themes will not respect the variable we're modifying to achieve this - in
* particular, Garland and Minelli do not.
*/
function i18n_preprocess_page(&$variables) {
if (isset($variables['body_classes'])) {
global $language;
$variables['body_classes'] .= ' i18n-' . $language->language;
}
}
/**
* Implements hook_permission().
*
* 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
* - administer translations
* Will allow to add/remove existing nodes to/from translation sets.
*/
function i18n_permission() {
return array(
'administer all languages' => array(
'title' => t('Administer all languages'),
'description' => t('Administer all languages.'),
),
'administer translations' => array(
'title' => t('Administer translations'),
'description' => t('Administer translations.'),
),
);
}
/**
* 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.
*/
function _i18n_get_context_lang() {
// Node language when loading specific nodes or creating translations.
if (arg(0) == 'node' ) {
if (($node = menu_get_object('node')) && $node->language) {
return $node->language;
}
elseif (arg(1) == 'add' && !empty($_GET['translation']) && !empty($_GET['language'])) {
return $_GET['language'];
}
}
}