Newer
Older
Gábor Hojtsy
committed
<?php
// $Id$
/**
* @file
* Manages content translations.
*
* Translations are managed in sets of posts, which represent the same
* information in different languages. Only content types for which the
* administrator explicitly enabled translations could have translations
* associated. Translations are managed in sets with exactly one source
* post per set. The source post is used to translate to different
* languages, so if the source post is significantly updated, the
* editor can decide to mark all translations outdated.
*
* The node table stores the values used by this module:
* - 'tnid' is the translation set id, which equals the node id
* of the source post.
* - 'translate' is a flag, either indicating that the translation
* is up to date (0) or needs to be updated (1).
*/
/**
* Identifies a content type which has translation support enabled.
*/
Gábor Hojtsy
committed
define('TRANSLATION_ENABLED', 2);
Gábor Hojtsy
committed
/**
Dries Buytaert
committed
* Implement hook_help().
Gábor Hojtsy
committed
*/
Gábor Hojtsy
committed
function translation_help($path, $arg) {
switch ($path) {
Gábor Hojtsy
committed
case 'admin/help#translation':
$output = '<p>' . t('The content translation module allows content to be translated into different languages. Working with the <a href="@locale">locale module</a> (which manages enabled languages and provides translation for the site interface), the content translation module is key to creating and maintaining translated site content.', array('@locale' => url('admin/help/locale'))) . '</p>';
$output .= '<p>' . t('Configuring content translation and translation-enabled content types:') . '</p>';
Dries Buytaert
committed
$output .= '<ul><li>' . t('Assign the <em>translate content</em> permission to the appropriate user roles at the <a href="@permissions">Permissions configuration page</a>.', array('@permissions' => url('admin/config/people/permissions'))) . '</li>';
Dries Buytaert
committed
$output .= '<li>' . t('Add and enable desired languages at the <a href="@languages">Languages configuration page</a>.', array('@languages' => url('admin/config/regional/language'))) . '</li>';
Dries Buytaert
committed
$output .= '<li>' . t('Determine which <a href="@content-types">content types</a> should support translation features. To enable translation support for a content type, edit the type and at the <em>Multilingual support</em> drop down, select <em>Enabled, with translation</em>. (<em>Multilingual support</em> is located within <em>Publishing options</em>.) Be sure to save each content type after enabling multilingual support.', array('@content-types' => url('admin/structure/types'))) . '</li></ul>';
$output .= '<p>' . t('Working with translation-enabled content types:') . '</p>';
$output .= '<ul><li>' . t('Use the <em>Language</em> drop down to select the appropriate language when creating or editing posts.') . '</li>';
$output .= '<li>' . t('Provide new or edit current translations for existing posts via the <em>Translation</em> tab. Only visible while viewing a post as a user with the <em>translate content</em> permission, this tab allows translations to be added or edited using a specialized editing form that also displays the content being translated.') . '</li>';
$output .= '<li>' . t('Update translations as needed, so that they accurately reflect changes in the content of the original post. The translation status flag provides a simple method for tracking outdated translations. After editing a post, for example, select the <em>Flag translations as outdated</em> check box to mark all of its translations as outdated and in need of revision. Individual translations may be marked for revision by selecting the <em>This translation needs to be updated</em> check box on the translation editing form.') . '</li>';
Dries Buytaert
committed
$output .= '<li>' . t('The <a href="@content-node">Content management administration page</a> displays the language of each post, and also allows filtering by language or translation status.', array('@content-node' => url('admin/content'))) . '</li></ul>';
Dries Buytaert
committed
$output .= '<p>' . t('Use the <a href="@blocks">language switcher block</a> provided by locale module to allow users to select a language. If available, both the site interface and site content are presented in the language selected.', array('@blocks' => url('admin/structure/block'))) . '</p>';
$output .= '<p>' . t('For more information, see the online handbook entry for <a href="@translation">Translation module</a>.', array('@translation' => 'http://drupal.org/handbook/modules/translation/')) . '</p>';
Gábor Hojtsy
committed
return $output;
case 'node/%/translate':
$output = '<p>' . t('Translations of a piece of content are managed with translation sets. Each translation set has one source post and any number of translations in any of the <a href="!languages">enabled languages</a>. All translations are tracked to be up to date or outdated based on whether the source post was modified significantly.', array('!languages' => url('admin/settings/language'))) . '</p>';
Gábor Hojtsy
committed
return $output;
}
}
/**
Dries Buytaert
committed
* Implement hook_menu().
Gábor Hojtsy
committed
*/
function translation_menu() {
$items = array();
$items['node/%node/translate'] = array(
'title' => 'Translate',
'page callback' => 'translation_node_overview',
'page arguments' => array(1),
'access callback' => '_translation_tab_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'weight' => 2,
'file' => 'translation.pages.inc',
Gábor Hojtsy
committed
);
return $items;
}
/**
* Menu access callback.
*
* Only display translation tab for node types, which have translation enabled
* and where the current node is not language neutral (which should span
* all languages).
*/
function _translation_tab_access($node) {
if (!empty($node->language) && translation_supported_type($node->type)) {
return user_access('translate content');
}
return FALSE;
}
/**
* Implement hook_permission().
Gábor Hojtsy
committed
*/
function translation_permission() {
return array(
Angie Byron
committed
'translate content' => array(
'title' => t('Translate content'),
Angie Byron
committed
'description' => t('Translate website content.'),
),
);
Gábor Hojtsy
committed
}
Angie Byron
committed
/**
Dries Buytaert
committed
* Implement hook_form_FORM_ID_alter().
Angie Byron
committed
*/
function translation_form_node_type_form_alter(&$form, &$form_state) {
// Add translation option to content type form.
$form['workflow']['language_content_type']['#options'][TRANSLATION_ENABLED] = t('Enabled, with translation');
// Description based on text from locale.module.
$form['workflow']['language_content_type']['#description'] = t('Enable multilingual support for this content type. If enabled, a language selection field will be added to the editing form, allowing you to select from one of the <a href="!languages">enabled languages</a>. You can also turn on translation for this content type, which lets you have content translated to any of the enabled languages. If disabled, new posts are saved with the default language. Existing content will not be affected by changing this option.', array('!languages' => url('admin/settings/language')));
}
Gábor Hojtsy
committed
/**
Dries Buytaert
committed
* Implement hook_form_alter().
Gábor Hojtsy
committed
*
* - Add translation option to content type form.
* - Alters language fields on node forms when a translation
* is about to be created.
*/
Angie Byron
committed
function translation_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['#id']) && $form['#id'] == 'node-form' && translation_supported_type($form['#node']->type)) {
Gábor Hojtsy
committed
$node = $form['#node'];
if (!empty($node->translation_source)) {
// We are creating a translation. Add values and lock language field.
$form['translation_source'] = array('#type' => 'value', '#value' => $node->translation_source);
$form['language']['#disabled'] = TRUE;
}
elseif (!empty($node->nid) && !empty($node->tnid)) {
// Disable languages for existing translations, so it is not possible to switch this
// node to some language which is already in the translation set. Also remove the
// language neutral option.
unset($form['language']['#options']['']);
foreach (translation_node_get_translations($node->tnid) as $translation) {
if ($translation->nid != $node->nid) {
unset($form['language']['#options'][$translation->language]);
}
}
// Add translation values and workflow options.
Gábor Hojtsy
committed
$form['tnid'] = array('#type' => 'value', '#value' => $node->tnid);
Gábor Hojtsy
committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
$form['translation'] = array(
'#type' => 'fieldset',
'#title' => t('Translation settings'),
'#access' => user_access('translate content'),
'#collapsible' => TRUE,
'#collapsed' => !$node->translate,
'#tree' => TRUE,
'#weight' => 30,
);
if ($node->tnid == $node->nid) {
// This is the source node of the translation
$form['translation']['retranslate'] = array(
'#type' => 'checkbox',
'#title' => t('Flag translations as outdated'),
'#default_value' => 0,
'#description' => t('If you made a significant change, which means translations should be updated, you can flag all translations of this post as outdated. This will not change any other property of those posts, like whether they are published or not.'),
);
$form['translation']['status'] = array('#type' => 'value', '#value' => 0);
}
else {
$form['translation']['status'] = array(
'#type' => 'checkbox',
'#title' => t('This translation needs to be updated'),
'#default_value' => $node->translate,
'#description' => t('When this option is checked, this translation needs to be updated because the source post has changed. Uncheck when the translation is up to date again.'),
);
}
}
}
}
/**
Dries Buytaert
committed
* Implement hook_node_view().
Gábor Hojtsy
committed
*
* Display translation links with native language names, if this node
* is part of a translation set.
*/
function translation_node_view($node, $build_mode) {
if (isset($node->tnid) && $translations = translation_node_get_translations($node->tnid)) {
Gábor Hojtsy
committed
// Do not show link to the same node.
unset($translations[$node->language]);
Gábor Hojtsy
committed
$languages = language_list();
Dries Buytaert
committed
foreach ($languages as $langcode => $language) {
if (isset($translations[$langcode])) {
$links["node_translation_$langcode"] = array(
'title' => $language->native,
'href' => 'node/' . $translations[$langcode]->nid,
'language' => $language,
'attributes' => array('title' => $translations[$langcode]->title, 'class' => array('translation-link')),
Dries Buytaert
committed
);
$node->content['links']['translation'] = array(
'#theme' => 'links',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
Dries Buytaert
committed
}
Gábor Hojtsy
committed
}
}
}
/**
Dries Buytaert
committed
* Implement hook_node_prepare().
Gábor Hojtsy
committed
*/
function translation_node_prepare($node) {
Gábor Hojtsy
committed
// Only act if we are dealing with a content type supporting translations.
Dries Buytaert
committed
if (translation_supported_type($node->type)) {
if (empty($node->nid) && isset($_GET['translation']) && isset($_GET['language']) &&
($source_nid = $_GET['translation']) && ($language = $_GET['language']) &&
(user_access('translate content'))) {
// We are translating a node from a source node, so
// load the node to be translated and populate fields.
Angie Byron
committed
$source_node = node_load($source_nid);
// Ensure we don't have an existing translation in this language.
if (!empty($source_node->tnid)) {
$translations = translation_node_get_translations($source_node->tnid);
if (isset($translations[$language])) {
$languages = language_list();
drupal_set_message(t('A translation of %title in %language already exists, a new %type will be created instead of a translation.', array('%title' => $source_node->title, '%language' => $languages[$language]->name, '%type' => $node->type)), 'error');
return;
}
}
Dries Buytaert
committed
$node->language = $language;
Angie Byron
committed
$node->translation_source = $source_node;
Dries Buytaert
committed
$node->title = $node->translation_source->title;
// Let every module add custom translated fields.
Dries Buytaert
committed
module_invoke_all('node_prepare_translation', $node);
Dries Buytaert
committed
}
Gábor Hojtsy
committed
}
Dries Buytaert
committed
}
Gábor Hojtsy
committed
Dries Buytaert
committed
/**
Dries Buytaert
committed
* Implement hook_node_insert().
Dries Buytaert
committed
*/
function translation_node_insert($node) {
Dries Buytaert
committed
// Only act if we are dealing with a content type supporting translations.
if (translation_supported_type($node->type)) {
if (!empty($node->translation_source)) {
if ($node->translation_source->tnid) {
// Add node to existing translation set.
$tnid = $node->translation_source->tnid;
Gábor Hojtsy
committed
}
Dries Buytaert
committed
else {
// Create new translation set, using nid from the source node.
$tnid = $node->translation_source->nid;
Dries Buytaert
committed
db_update('node')
->fields(array(
'tnid' => $tnid,
'translate' => 0,
))
->condition('nid', $node->translation_source->nid)
->execute();
Gábor Hojtsy
committed
}
Dries Buytaert
committed
db_update('node')
->fields(array(
'tnid' => $tnid,
'translate' => 0,
))
->condition('nid', $node->nid)
->execute();
Dries Buytaert
committed
}
}
}
Gábor Hojtsy
committed
Dries Buytaert
committed
/**
Dries Buytaert
committed
* Implement hook_node_update().
Dries Buytaert
committed
*/
function translation_node_update($node) {
Dries Buytaert
committed
// Only act if we are dealing with a content type supporting translations.
if (translation_supported_type($node->type)) {
if (isset($node->translation) && $node->translation && !empty($node->language) && $node->tnid) {
// Update translation information.
Dries Buytaert
committed
db_update('node')
->fields(array(
'tnid' => $node->tnid,
'translate' => $node->translation['status'],
))
->condition('nid', $node->nid)
->execute();
Dries Buytaert
committed
if (!empty($node->translation['retranslate'])) {
// This is the source node, asking to mark all translations outdated.
Dries Buytaert
committed
db_update('node')
->fields(array('translate' => 1))
->condition('nid', $node->nid, '<>')
->condition('tnid', $node->tnid)
->execute();
Gábor Hojtsy
committed
}
Dries Buytaert
committed
}
Gábor Hojtsy
committed
}
}
Angie Byron
committed
/**
Dries Buytaert
committed
* Implement hook_node_validate().
Angie Byron
committed
*
* Ensure that duplicate translations can not be created for the same source.
*/
function translation_node_validate($node, $form) {
// Only act on translatable nodes with a tnid or translation_source.
if (translation_supported_type($node->type) && (!empty($node->tnid) || !empty($form['#node']->translation_source->nid))) {
$tnid = !empty($node->tnid) ? $node->tnid : $form['#node']->translation_source->nid;
$translations = translation_node_get_translations($tnid);
if (isset($translations[$node->language]) && $translations[$node->language]->nid != $node->nid ) {
form_set_error('language', t('There is already a translation in this language.'));
}
}
}
Dries Buytaert
committed
/**
Dries Buytaert
committed
* Implement hook_node_delete().
Dries Buytaert
committed
*/
function translation_node_delete($node) {
Dries Buytaert
committed
// Only act if we are dealing with a content type supporting translations.
if (translation_supported_type($node->type)) {
translation_remove_from_set($node);
}
}
Gábor Hojtsy
committed
/**
* Remove a node from its translation set (if any)
* and update the set accordingly.
*/
function translation_remove_from_set($node) {
if (isset($node->tnid)) {
Dries Buytaert
committed
$query = db_update('node')
->fields(array(
'tnid' => 0,
'translate' => 0,
));
if (db_query('SELECT COUNT(*) FROM {node} WHERE tnid = :tnid', array(':tnid' => $node->tnid))->fetchField() == 1) {
// There is only one node left in the set: remove the set altogether.
Dries Buytaert
committed
$query
->condition('tnid', $node->tnid)
->execute();
Gábor Hojtsy
committed
}
else {
Dries Buytaert
committed
$query
->condition('nid', $node->nid)
->execute();
Gábor Hojtsy
committed
// If the node being removed was the source of the translation set,
// we pick a new source - preferably one that is up to date.
if ($node->tnid == $node->nid) {
Dries Buytaert
committed
$new_tnid = db_query('SELECT nid FROM {node} WHERE tnid = :tnid ORDER BY translate ASC, nid ASC', array(':tnid' => $node->tnid))->fetchField();
db_update('node')
->fields(array('tnid' => $new_tnid))
->condition('tnid', $node->tnid)
->execute();
Gábor Hojtsy
committed
}
}
}
}
Gábor Hojtsy
committed
/**
* Get all nodes in a translation set, represented by $tnid.
*
* @param $tnid
* The translation source nid of the translation set, the identifier
* of the node used to derive all translations in the set.
* @return
* Array of partial node objects (nid, title, language) representing
* all nodes in the translation set, in effect all translations
* of node $tnid, including node $tnid itself. Because these are
* partial nodes, you need to node_load() the full node, if you
* need more properties. The array is indexed by language code.
Gábor Hojtsy
committed
*/
function translation_node_get_translations($tnid) {
if (is_numeric($tnid) && $tnid) {
$translations = &drupal_static(__FUNCTION__, array());
Gábor Hojtsy
committed
if (!isset($translations[$tnid])) {
$translations[$tnid] = array();
Dries Buytaert
committed
$result = db_select('node', 'n')
->fields('n', array('nid', 'title', 'language'))
->condition('n.tnid', $tnid)
->addTag('node_access')
->execute();
foreach ($result as $node) {
Gábor Hojtsy
committed
$translations[$tnid][$node->language] = $node;
}
}
return $translations[$tnid];
}
}
/**
* Returns whether the given content type has support for translations.
*
* @return
* Boolean value.
*/
function translation_supported_type($type) {
return variable_get('language_content_type_' . $type, 0) == TRANSLATION_ENABLED;
Gábor Hojtsy
committed
}
Gábor Hojtsy
committed
/**
* Return paths of all translations of a node, based on
Dries Buytaert
committed
* its Drupal path.
Gábor Hojtsy
committed
*
* @param $path
* A Drupal path, for example node/432.
* @return
Dries Buytaert
committed
* An array of paths of translations of the node accessible
Gábor Hojtsy
committed
* to the current user keyed with language codes.
*/
function translation_path_get_translations($path) {
$paths = array();
// Check for a node related path, and for its translations.
if ((preg_match("!^node/([0-9]+)(/.+|)$!", $path, $matches)) && ($node = node_load((int)$matches[1])) && !empty($node->tnid)) {
foreach (translation_node_get_translations($node->tnid) as $language => $translation_node) {
$paths[$language] = 'node/' . $translation_node->nid . $matches[2];
Gábor Hojtsy
committed
}
}
return $paths;
}
/**
Dries Buytaert
committed
* Implement hook_translation_link_alter().
Gábor Hojtsy
committed
*
* Replaces links with pointers to translated versions of the content.
*/
Dries Buytaert
committed
function translation_translation_link_alter(array &$links, $path) {
Gábor Hojtsy
committed
if ($paths = translation_path_get_translations($path)) {
foreach ($links as $langcode => $link) {
if (isset($paths[$langcode])) {
// Translation in a different node.
$links[$langcode]['href'] = $paths[$langcode];
}
else {
// No translation in this language, or no permission to view.
unset($links[$langcode]);
}
}
}
}