You are here

function i18n_string_install_update_context in Internationalization 7

Update context for strings.

As some string locations depend on configurable values, the field needs sometimes to be updated without losing existing translations. I.e:

  • profile fields indexed by field name.
  • content types indexted by low level content type name.

Example: 'profile:field:oldfield:*' -> 'profile:field:newfield:*'

2 calls to i18n_string_install_update_context()
i18n_node_update_7000 in i18n_node/i18n_node.install
Implements hook_i18n_update_drupal6().
i18n_string_update_context in i18n_string/i18n_string.module
Update context for strings.

File

i18n_string/i18n_string.install, line 153
Installation file for i18n_string module.

Code

function i18n_string_install_update_context($oldname, $newname) {

  // Get context replacing '*' with empty string.
  $oldcontext = explode(':', $oldname);
  $newcontext = explode(':', $newname);

  /*
  i18n_string_context(str_replace('*', '', $oldname));
  $newcontext = i18n_string_context(str_replace('*', '', $newname));
  */

  // Get location with placeholders.
  foreach (array(
    'textgroup',
    'type',
    'objectid',
    'property',
  ) as $index => $field) {
    if ($oldcontext[$index] != $newcontext[$index]) {
      $replace[$field] = $newcontext[$index];
    }
  }

  // Query and replace if there are any fields. It is possible that under some circumstances fields are the same
  if (!empty($replace)) {
    $textgroup = array_shift($oldcontext);
    $context = str_replace('*', '%', implode(':', $oldcontext));
    $count = 0;
    $query = db_select('i18n_string', 's')
      ->fields('s')
      ->condition('s.textgroup', $textgroup)
      ->condition('s.context', $context, 'LIKE');
    foreach ($query
      ->execute()
      ->fetchAll() as $source) {
      foreach ($replace as $field => $value) {
        $source->{$field} = $value;
      }

      // Recalculate location, context, objectindex
      $source->context = $source->type . ':' . $source->objectid . ':' . $source->property;
      $source->location = $source->textgroup . ':' . $source->context;
      $source->objectindex = (int) $source->objectid;

      // Update source string.
      $update = array(
        'textgroup' => $source->textgroup,
        'context' => $source->context,
      );
      db_update('locales_source')
        ->fields($update + array(
        'location' => $source->location,
      ))
        ->condition('lid', $source->lid)
        ->execute();

      // Update object data.
      db_update('i18n_string')
        ->fields($update + array(
        'type' => $source->type,
        'objectid' => $source->objectid,
        'property' => $source->property,
        'objectindex' => $source->objectindex,
      ))
        ->condition('lid', $source->lid)
        ->execute();
      $count++;
    }
    drupal_set_message(t('Updated @count string names from %oldname to %newname.', array(
      '@count' => $count,
      '%oldname' => $oldname,
      '%newname' => $newname,
    )));
  }
}