123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576 |
- <?php
- define('TRANSLATION_ENABLED', 2);
- function translation_help($path, $arg) {
- switch ($path) {
- case 'admin/help#translation':
- $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/documentation/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'))) . '</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>';
- 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/config/regional/language'))) . '</p>';
- return $output;
- }
- }
- 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',
- );
- return $items;
- }
- function _translation_tab_access($node) {
- if (entity_language('node', $node) != LANGUAGE_NONE && translation_supported_type($node->type) && node_access('view', $node)) {
- return user_access('translate content');
- }
- return FALSE;
- }
- function translation_admin_paths() {
- if (variable_get('node_admin_theme')) {
- $paths = array(
- 'node/*/translate' => TRUE,
- );
- return $paths;
- }
- }
- function translation_permission() {
- return array(
- 'translate content' => array(
- 'title' => t('Translate content'),
- ),
- );
- }
- function translation_form_node_type_form_alter(&$form, &$form_state) {
-
- $form['workflow']['language_content_type']['#options'][TRANSLATION_ENABLED] = t('Enabled, with translation');
-
- $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')));
- }
- function translation_form_node_form_alter(&$form, &$form_state) {
- if (translation_supported_type($form['#node']->type)) {
- $node = $form['#node'];
- $languages = language_list('enabled');
- $disabled_languages = isset($languages[0]) ? $languages[0] : FALSE;
- $translator_widget = $disabled_languages && user_access('translate content');
- $groups = array(t('Disabled'), t('Enabled'));
-
-
-
- if ($translator_widget) {
- $options = array($groups[1] => array(LANGUAGE_NONE => t('Language neutral')));
- $language_list = locale_language_list('name', TRUE);
- foreach (array(1, 0) as $status) {
- $group = $groups[$status];
- foreach ($languages[$status] as $langcode => $language) {
- $options[$group][$langcode] = $language_list[$langcode];
- }
- }
- $form['language']['#options'] = $options;
- }
- if (!empty($node->translation_source)) {
-
- $form['translation_source'] = array('#type' => 'value', '#value' => $node->translation_source);
- $form['language']['#disabled'] = TRUE;
- }
- elseif (!empty($node->nid) && !empty($node->tnid)) {
-
-
-
- unset($form['language']['#options'][LANGUAGE_NONE]);
- foreach (translation_node_get_translations($node->tnid) as $langcode => $translation) {
- 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]);
- }
- }
- }
-
- $form['tnid'] = array('#type' => 'value', '#value' => $node->tnid);
- $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) {
-
- $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.'),
- );
- }
- }
- }
- }
- function translation_node_view($node, $view_mode) {
-
-
- if (isset($node->tnid) && drupal_multilingual() && $translations = translation_node_get_translations($node->tnid)) {
- $languages = language_list('enabled');
- $languages = $languages[1];
-
-
-
-
-
-
- $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) {
-
-
- if ($translation->status && isset($languages[$langcode]) && $langcode != entity_language('node', $node)) {
- $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}",
- 'title' => $language->native,
- 'language' => $language,
- );
- }
-
-
- $links[$key] += array('attributes' => array());
- $attributes = array(
- 'title' => $translation->title,
- 'class' => array('translation-link'),
- );
- $links[$key]['attributes'] = $attributes + $links[$key]['attributes'];
- }
- }
- $node->content['links']['translation'] = array(
- '#theme' => 'links__node__translation',
- '#links' => $links,
- '#attributes' => array('class' => array('links', 'inline')),
- );
- }
- }
- function translation_node_prepare($node) {
-
- if (translation_supported_type($node->type) &&
-
- empty($node->nid) &&
-
- user_access('translate content') &&
-
- isset($_GET['translation']) &&
- isset($_GET['target']) &&
- is_numeric($_GET['translation'])) {
- $source_node = node_load($_GET['translation']);
- if (empty($source_node) || !node_access('view', $source_node)) {
-
-
-
- return;
- }
- $language_list = language_list();
- $langcode = $_GET['target'];
- if (!isset($language_list[$langcode]) || ($source_node->language == $langcode)) {
-
- return;
- }
-
- if (!empty($source_node->tnid)) {
- $translations = translation_node_get_translations($source_node->tnid);
- 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;
- }
- }
-
- $node->language = $langcode;
- $node->translation_source = $source_node;
- $node->title = $source_node->title;
-
-
- field_attach_prepare_translation('node', $node, $langcode, $source_node, $source_node->language);
- }
- }
- function translation_node_insert($node) {
-
- if (translation_supported_type($node->type)) {
- if (!empty($node->translation_source)) {
- if ($node->translation_source->tnid) {
-
- $tnid = $node->translation_source->tnid;
- }
- else {
-
- $tnid = $node->translation_source->nid;
- db_update('node')
- ->fields(array(
- 'tnid' => $tnid,
- 'translate' => 0,
- ))
- ->condition('nid', $tnid)
- ->execute();
-
- entity_get_controller('node')->resetCache(array($tnid));
- }
- db_update('node')
- ->fields(array(
- 'tnid' => $tnid,
- 'translate' => 0,
- ))
- ->condition('nid', $node->nid)
- ->execute();
-
- $node->tnid = $tnid;
- }
- }
- }
- function translation_node_update($node) {
-
- if (translation_supported_type($node->type)) {
- $langcode = entity_language('node', $node);
- if (isset($node->translation) && $node->translation && !empty($langcode) && $node->tnid) {
-
- db_update('node')
- ->fields(array(
- 'tnid' => $node->tnid,
- 'translate' => $node->translation['status'],
- ))
- ->condition('nid', $node->nid)
- ->execute();
- if (!empty($node->translation['retranslate'])) {
-
- $translations = db_select('node', 'n')
- ->fields('n', array('nid'))
- ->condition('nid', $node->nid, '<>')
- ->condition('tnid', $node->tnid)
- ->execute()
- ->fetchCol();
- db_update('node')
- ->fields(array('translate' => 1))
- ->condition('nid', $translations, 'IN')
- ->execute();
-
- entity_get_controller('node')->resetCache($translations);
- }
- }
- }
- }
- function translation_node_validate($node, $form) {
-
- 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);
- $langcode = entity_language('node', $node);
- if (isset($translations[$langcode]) && $translations[$langcode]->nid != $node->nid ) {
- form_set_error('language', t('There is already a translation in this language.'));
- }
- }
- }
- function translation_node_delete($node) {
-
- if (translation_supported_type($node->type)) {
- translation_remove_from_set($node);
- }
- }
- function translation_remove_from_set($node) {
- if (isset($node->tnid)) {
- $query = db_update('node')
- ->fields(array(
- 'tnid' => 0,
- 'translate' => 0,
- ));
-
- $set_nids = db_query('SELECT nid FROM {node} WHERE tnid = :tnid', array(':tnid' => $node->tnid))->fetchCol();
- if (count($set_nids) == 1) {
-
- $query
- ->condition('tnid', $node->tnid)
- ->execute();
- $flush_set = TRUE;
- }
- else {
- $query
- ->condition('nid', $node->nid)
- ->execute();
-
-
- if ($node->tnid == $node->nid) {
- $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();
- $flush_set = TRUE;
- }
- }
-
- $nids = !empty($flush_set) ? $set_nids : array($node->nid);
- entity_get_controller('node')->resetCache($nids);
- }
- }
- function translation_node_get_translations($tnid) {
- if (is_numeric($tnid) && $tnid) {
- $translations = &drupal_static(__FUNCTION__, array());
- if (!isset($translations[$tnid])) {
- $translations[$tnid] = array();
- $result = db_select('node', 'n')
- ->fields('n', array('nid', 'type', 'uid', 'status', 'title', 'language'))
- ->condition('n.tnid', $tnid)
- ->addTag('node_access')
- ->execute();
- foreach ($result as $node) {
- $langcode = entity_language('node', $node);
- $translations[$tnid][$langcode] = $node;
- }
- }
- return $translations[$tnid];
- }
- }
- function translation_supported_type($type) {
- return variable_get('language_content_type_' . $type, 0) == TRANSLATION_ENABLED;
- }
- function translation_path_get_translations($path) {
- $paths = array();
-
- if ((preg_match("!^node/(\d+)(/.+|)$!", $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];
- }
- }
- return $paths;
- }
- function translation_language_switch_links_alter(array &$links, $type, $path) {
- $language_type = variable_get('translation_language_type', LANGUAGE_TYPE_INTERFACE);
- if ($type == $language_type && preg_match("!^node/(\d+)(/.+|)!", $path, $matches)) {
- $node = node_load((int) $matches[1]);
- if (empty($node->tnid)) {
-
-
-
-
- if (empty($node) || entity_language('node', $node) == LANGUAGE_NONE || !translation_supported_type($node->type)) {
- return;
- }
- $langcode = entity_language('node', $node);
- $translations = array($langcode => $node);
- }
- else {
- $translations = translation_node_get_translations($node->tnid);
- }
- foreach ($links as $langcode => $link) {
- if (isset($translations[$langcode]) && $translations[$langcode]->status) {
-
- $links[$langcode]['href'] = 'node/' . $translations[$langcode]->nid . $matches[2];
- }
- else {
-
- unset($links[$langcode]['href']);
- $links[$langcode]['attributes']['class'][] = 'locale-untranslated';
- }
- }
- }
- }
|