Newer
Older
Gábor Hojtsy
committed
<?php
/**
* @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.
*/
const TRANSLATION_ENABLED = 2;
Gábor Hojtsy
committed
/**
Dries Buytaert
committed
* Implements 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':
Angie Byron
committed
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$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. For more information, see the online handbook entry for <a href="@translation">Content translation module</a>.', array('@locale' => url('admin/help/locale'), '@translation' => 'http://drupal.org/handbook/modules/translation/')) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Configuring content types for translation') . '</dt>';
$output .= '<dd>' . t('To configure a particular content type for translation, visit the <a href="@content-types">Content types</a> page, and click the <em>edit</em> link for the content type. In the <em>Publishing options</em> section, select <em>Enabled, with translation</em> under <em>Multilingual support</em>.', array('@content-types' => url('admin/structure/types'))) . '</li></ul><dd>';
$output .= '<dt>' . t('Assigning a language to content') . '</dt>';
$output .= '<dd>' . t('Use the <em>Language</em> drop down to select the appropriate language when creating or editing content.') . '</dd>';
$output .= '<dt>' . t('Translating content') . '</dt>';
$output .= '<dd>' . t('Users with the <em>translate content</em> permission can translate content, if the content type has been configured to allow translations. To translate content, select the <em>Translation</em> tab when viewing the content, select the language for which you wish to provide content, and then enter the content.') . '</dd>';
$output .= '<dt>' . t('Maintaining translations') . '</dt>';
$output .= '<dd>' . t('If editing content in one language requires that translated versions also be updated to reflect the change, use the <em>Flag translations as outdated</em> check box to mark the translations as outdated and in need of revision. Individual translations may also be marked for revision by selecting the <em>This translation needs to be updated</em> check box on the translation editing form.') . '</dd>';
$output .= '</dl>';
Gábor Hojtsy
committed
return $output;
case 'node/%/translate':
Angie Byron
committed
$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/config/regional/language'))) . '</p>';
Gábor Hojtsy
committed
return $output;
}
}
/**
Dries Buytaert
committed
* Implements 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 ($node->language != LANGUAGE_NONE && translation_supported_type($node->type) && node_access('view', $node)) {
Gábor Hojtsy
committed
return user_access('translate content');
}
return FALSE;
}
Angie Byron
committed
/**
* Implements hook_admin_paths().
*/
function translation_admin_paths() {
Dries Buytaert
committed
if (variable_get('node_admin_theme')) {
$paths = array(
'node/*/translate' => TRUE,
);
return $paths;
}
Angie Byron
committed
}
Gábor Hojtsy
committed
/**
Dries Buytaert
committed
* Implements hook_permission().
Gábor Hojtsy
committed
*/
function translation_permission() {
return array(
Angie Byron
committed
'translate content' => array(
'title' => t('Translate content'),
Angie Byron
committed
),
);
Gábor Hojtsy
committed
}
Angie Byron
committed
/**
Dries Buytaert
committed
* Implements 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.
Dries Buytaert
committed
$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 installed 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/config/regional/language')));
Angie Byron
committed
}
Gábor Hojtsy
committed
/**
Dries Buytaert
committed
* Implements hook_form_BASE_FORM_ID_alter().
Gábor Hojtsy
committed
*
Dries Buytaert
committed
* This function alters language fields on node edit forms when a translation is
* about to be created.
Gábor Hojtsy
committed
*/
Dries Buytaert
committed
function translation_form_node_form_alter(&$form, &$form_state) {
if (translation_supported_type($form['#node']->type)) {
Gábor Hojtsy
committed
$node = $form['#node'];
Dries Buytaert
committed
// Build two lists with the disabled and enabled languages.
$languages = language_list();
$grouped_languages = array();
foreach ($languages as $langcode => $language) {
$grouped_languages[(int) $language->enabled][$langcode] = $language;
}
$translator_widget = !empty($grouped_languages[0]) && user_access('translate content');
$groups = array(t('Disabled'), t('Enabled'));
// Allow translators to enter content in disabled languages. Translators
// might need to distinguish between enabled and disabled languages, hence
// we divide them in two option groups.
if ($translator_widget) {
Dries Buytaert
committed
$options = array($groups[1] => array(LANGUAGE_NONE => t('Language neutral')));
foreach (array(1, 0) as $status) {
$group = $groups[$status];
Dries Buytaert
committed
foreach ($grouped_languages[$status] as $langcode => $language) {
$options[$group][$langcode] = $language->name;
}
}
$form['language']['#options'] = $options;
}
Gábor Hojtsy
committed
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'][LANGUAGE_NONE]);
foreach (translation_node_get_translations($node->tnid) as $langcode => $translation) {
Gábor Hojtsy
committed
if ($translation->nid != $node->nid) {
if ($translator_widget) {
$group = $groups[(int)!isset($disabled_languages[$langcode])];
unset($form['language']['#options'][$group][$langcode]);
}
else {
unset($form['language']['#options'][$langcode]);
}
Gábor Hojtsy
committed
}
}
// Add translation values and workflow options.
Gábor Hojtsy
committed
$form['tnid'] = array('#type' => 'value', '#value' => $node->tnid);
Gábor Hojtsy
committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
$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
* Implements hook_node_view().
Gábor Hojtsy
committed
*
catch
committed
* Display translation links with language names, if this node is part of
Angie Byron
committed
* a translation set. If no language provider is enabled "fall back" to the
* simple links built through the result of translation_node_get_translations().
Gábor Hojtsy
committed
*/
function translation_node_view($node, $view_mode) {
Angie Byron
committed
// If the site has no translations or is not multilingual we have no content
// translation links to display.
catch
committed
if (isset($node->tnid) && language_multilingual() && $translations = translation_node_get_translations($node->tnid)) {
Dries Buytaert
committed
$languages = language_list(TRUE);
Angie Byron
committed
// There might be a language provider enabled defining custom language
// switch links which need to be taken into account while generating the
// content translation links. As custom language switch links are available
// only for configurable language types and interface language is the only
// configurable language type in core, we use it as default. Contributed
// modules can change this behavior by setting the system variable below.
$type = variable_get('translation_language_type', LANGUAGE_TYPE_INTERFACE);
$custom_links = language_negotiation_get_switch_links($type, "node/$node->nid");
$links = array();
foreach ($translations as $langcode => $translation) {
// Do not show links to the same node, to unpublished translations or to
// translations in disabled languages.
if ($translation->status && isset($languages[$langcode]) && $langcode != $node->language) {
$language = $languages[$langcode];
$key = "translation_$langcode";
if (isset($custom_links->links[$langcode])) {
$links[$key] = $custom_links->links[$langcode];
}
else {
$links[$key] = array(
'href' => "node/{$translation->nid}",
catch
committed
'title' => $language->name,
Angie Byron
committed
'language' => $language,
);
}
// Custom switch links are more generic than content translation links,
// hence we override existing attributes with the ones below.
$links[$key] += array('attributes' => array());
$attributes = array(
'title' => $translation->title,
'class' => array('translation-link'),
);
$links[$key]['attributes'] = $attributes + $links[$key]['attributes'];
}
Gábor Hojtsy
committed
}
Angie Byron
committed
$node->content['links']['translation'] = array(
'#theme' => 'links__node__translation',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
);
Gábor Hojtsy
committed
}
}
/**
Dries Buytaert
committed
* Implements 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.
if (translation_supported_type($node->type) &&
// And it's a new node.
empty($node->nid) &&
// And the user has permission to translate content.
user_access('translate content') &&
// And the $_GET variables are set properly.
isset($_GET['translation']) &&
Dries Buytaert
committed
isset($_GET['target']) &&
is_numeric($_GET['translation'])) {
Dries Buytaert
committed
$source_node = node_load($_GET['translation']);
if (empty($source_node) || !node_access('view', $source_node)) {
// Source node not found or no access to view. We should not check
// for edit access, since the translator might not have permissions
// to edit the source node but should still be able to translate.
return;
}
Dries Buytaert
committed
$language_list = language_list();
Dries Buytaert
committed
$langcode = $_GET['target'];
if (!isset($language_list[$langcode]) || ($source_node->language == $langcode)) {
// If not supported language, or same language as source node, break.
return;
}
Dries Buytaert
committed
// Ensure we don't have an existing translation in this language.
if (!empty($source_node->tnid)) {
$translations = translation_node_get_translations($source_node->tnid);
Dries Buytaert
committed
if (isset($translations[$langcode])) {
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' => $language_list[$langcode]->name, '%type' => $node->type)), 'error');
return;
Angie Byron
committed
}
Dries Buytaert
committed
}
Dries Buytaert
committed
// Populate fields based on source node.
Dries Buytaert
committed
$node->language = $langcode;
$node->translation_source = $source_node;
$node->title = $source_node->title;
Angie Byron
committed
// Add field translations and let other modules module add custom translated
// fields.
field_attach_prepare_translation('node', $node, $node->language, $source_node, $source_node->language);
Gábor Hojtsy
committed
}
Dries Buytaert
committed
}
Gábor Hojtsy
committed
Dries Buytaert
committed
/**
Dries Buytaert
committed
* Implements 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();
Angie Byron
committed
// Save tnid to avoid loss in case of resave.
$node->tnid = $tnid;
Dries Buytaert
committed
}
}
}
Gábor Hojtsy
committed
Dries Buytaert
committed
/**
Dries Buytaert
committed
* Implements 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
* Implements hook_node_validate().
Angie Byron
committed
*
* Ensure that duplicate translations can not be created for the same source.
*/
function translation_node_validate($node, $form) {
Angie Byron
committed
// 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
/**
catch
committed
* Implements hook_node_predelete().
Dries Buytaert
committed
*/
catch
committed
function translation_node_predelete($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) {
Gábor Hojtsy
committed
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')
Dries Buytaert
committed
->fields('n', array('nid', 'type', 'uid', 'status', 'title', 'language'))
Dries Buytaert
committed
->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.
Angie Byron
committed
if ((preg_match("!^node/(\d+)(/.+|)$!", $path, $matches)) && ($node = node_load((int) $matches[1])) && !empty($node->tnid)) {
Gábor Hojtsy
committed
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;
}
/**
Angie Byron
committed
* Implements hook_language_switch_links_alter().
Gábor Hojtsy
committed
*
* Replaces links with pointers to translated versions of the content.
*/
Angie Byron
committed
function translation_language_switch_links_alter(array &$links, $type, $path) {
Angie Byron
committed
$language_type = variable_get('translation_language_type', LANGUAGE_TYPE_INTERFACE);
Dries Buytaert
committed
if ($type == $language_type && preg_match("!^node/(\d+)(/.+|)!", $path, $matches)) {
$node = node_load((int) $matches[1]);
if (empty($node->tnid)) {
// If the node cannot be found nothing needs to be done. If it does not
// have translations it might be a language neutral node, in which case we
// must leave the language switch links unaltered. This is true also for
// nodes not having translation support enabled.
if (empty($node) || $node->language == LANGUAGE_NONE || !translation_supported_type($node->type)) {
return;
}
$translations = array($node->language => $node);
}
else {
$translations = translation_node_get_translations($node->tnid);
}
Angie Byron
committed
Gábor Hojtsy
committed
foreach ($links as $langcode => $link) {
Angie Byron
committed
if (isset($translations[$langcode]) && $translations[$langcode]->status) {
Gábor Hojtsy
committed
// Translation in a different node.
Angie Byron
committed
$links[$langcode]['href'] = 'node/' . $translations[$langcode]->nid . $matches[2];
Gábor Hojtsy
committed
}
else {
// No translation in this language, or no permission to view.
Angie Byron
committed
unset($links[$langcode]['href']);
Dries Buytaert
committed
$links[$langcode]['attributes']['class'][] = 'locale-untranslated';