From 511a0c332eb8e6ac7cd77d95eaa61110ad10ab92 Mon Sep 17 00:00:00 2001 From: Tristan Olive Date: Thu, 30 Apr 2015 00:44:50 -0400 Subject: [PATCH] Added static content translation * When a page node is edited, add or update the source translation of the content body * When a page node is viewed, render the translated body, if available (DBOINCP-141) --- .../boinc/modules/boinccore/boinccore.module | 4 +- .../boinctranslate/boinctranslate.module | 110 ++++++++++++++++++ 2 files changed, 112 insertions(+), 2 deletions(-) diff --git a/drupal/sites/default/boinc/modules/boinccore/boinccore.module b/drupal/sites/default/boinc/modules/boinccore/boinccore.module index 657d344d94..989f21cb8e 100644 --- a/drupal/sites/default/boinc/modules/boinccore/boinccore.module +++ b/drupal/sites/default/boinc/modules/boinccore/boinccore.module @@ -686,9 +686,9 @@ function boinccore_get_country_list() { /* * BOINC wrapper for string translation */ -function bts($string, $args = array(), $langcode = NULL, $context = 'boinc: ') { +function bts($string, $args = array(), $langcode = NULL, $context = 'boinc: ', $update = TRUE) { if (function_exists('i18nstrings_ts')) { - $string = i18nstrings_ts($context, $string, $langcode, TRUE); + $string = i18nstrings_ts($context, $string, $langcode, $update); } if (empty($args)) { return $string; diff --git a/drupal/sites/default/boinc/modules/boinctranslate/boinctranslate.module b/drupal/sites/default/boinc/modules/boinctranslate/boinctranslate.module index 725835eea1..1db14d26d4 100644 --- a/drupal/sites/default/boinc/modules/boinctranslate/boinctranslate.module +++ b/drupal/sites/default/boinc/modules/boinctranslate/boinctranslate.module @@ -65,6 +65,116 @@ function boinctranslate_menu() { return $items; } +/** + * Implementation of hook_nodeapi(); add custom actions to node operations + * Obsolete in Drupal 7... + */ +function boinctranslate_nodeapi(&$node, $op, $a3 = null, $a4 = null) { + // In Drupal 7, these operation cases will all exist as their own hooks, + // so let's approximate that here so that this function can simply be removed + // upon migration to 7 + switch($op) { + case 'update': + boinctranslate_node_update($node); + break; + case 'view': + global $language; + boinctranslate_node_view($node, 'full', $language->language); + break; + default: + } +} + +/** + * Implementation of hook_node_update(); add custom actions when a node + * is updated (forward compatible to Drupal 7) + */ +function boinctranslate_node_update($node) { + switch($node->type) { + case 'page': + // Add page content to translation table + $textgroup = 'project'; + $location = "node:{$node->nid}:body"; + $status = NULL; + $lid = db_result(db_query(" + SELECT lid FROM {locales_source} + WHERE location = '%s' AND textgroup = '%s'", + $location, $textgroup + )); + if ($lid) { + $result = db_query(" + UPDATE {locales_source} + SET source = '%s' + WHERE lid = %d", + $node->body, $lid + ); + if ($result) { + watchdog( + 'boinctranslate', + 'Updated translation source strings for node @nid.', + array('@nid' => $node->nid) + ); + } + else { + drupal_set_message( + t('Unable to update translation source strings.'), 'error' + ); + watchdog( + 'boinctranslate', + 'Unable to update translation source strings for node @nid.', + array('@nid' => $node->nid), + WATCHDOG_ERROR + ); + } + } + else { + $result = db_query(" + INSERT INTO {locales_source} + SET location = '%s', textgroup = '%s', source = '%s'", + $location, $textgroup, $node->body + ); + if ($result) { + watchdog( + 'boinctranslate', + 'Added translation source strings for node @nid.', + array('@nid' => $node->nid) + ); + } + else { + drupal_set_message( + t('Unable to add translation source strings.'), 'error' + ); + watchdog( + 'boinctranslate', + 'Unable to add translation source strings for node @nid.', + array('@nid' => $node->nid), + WATCHDOG_ERROR + ); + } + } + break; + + default: + + } +} + +/** + * Implementation of hook_node_view(); add custom actions when a node + * is viewed (forward compatible to Drupal 7) + */ +function boinctranslate_node_view($node, $view_mode, $langcode) { + switch($node->type) { + case 'page': + // Replace the node body with translated content, if available + $node->content['body']['#value'] = bts( + $node->body, array(), $langcode, "project:node:{$node->nid}:body", FALSE + ); + break; + default: + } +} + /** * Implementation of hook_panels_pane_content_alter()