You are here

function i18nstrings_update_context in Internationalization 6

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:*'

3 calls to i18nstrings_update_context()
i18nblocks_update_6001 in i18nblocks/i18nblocks.install
Rework block string keys, all must use module, delta
i18ncontent_node_type in i18ncontent/i18ncontent.module
Implementation of hook_node_type().
i18nprofile_field_form_submit in i18nprofile/i18nprofile.module
Process profile_field_form submissions.

File

i18nstrings/i18nstrings.module, line 622
Internationalization (i18n) package - translatable strings.

Code

function i18nstrings_update_context($oldname, $newname) {

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

  // Get location with placeholders.
  $location = i18nstrings_location(str_replace('*', '%', $oldname));
  foreach (array(
    'textgroup',
    'type',
    'objectid',
    'property',
  ) as $field) {
    if ((!empty($oldcontext->{$field}) || !empty($newcontext->{$field})) && $oldcontext->{$field} != $newcontext->{$field}) {
      $replace[$field] = $newcontext->{$field};
    }
  }

  // Query and replace if there are any fields. It is possible that under some circumstances fields are the same
  if (!empty($replace)) {
    $result = db_query("SELECT s.*, i.type, i.objectid, i.property FROM {locales_source} s  LEFT JOIN {i18n_strings} i ON s.lid = i.lid WHERE s.textgroup = '%s' AND s.location LIKE '%s'", $oldcontext->textgroup, $location);
    while ($source = db_fetch_object($result)) {

      // Make sure we have string and context.
      $context = i18nstrings_context($oldcontext->textgroup . ':' . $source->location);
      foreach ($replace as $field => $value) {
        $context->{$field} = $value;
      }

      // Update source string.
      db_query("UPDATE {locales_source} SET textgroup = '%s', location = '%s' WHERE lid = %d", $context->textgroup, i18nstrings_location($context), $source->lid);

      // Update object data.
      db_query("UPDATE {i18n_strings} SET type = '%s', objectid = '%s', property = '%s' WHERE lid = %d", $context->type, $context->objectid, $context->property, $source->lid);
    }
    drupal_set_message(t('Updating string names from %oldname to %newname.', array(
      '%oldname' => $oldname,
      '%newname' => $newname,
    )));
  }
}