You are here

function i18nstrings_add_string in Internationalization 6

Add source string to the locale tables for translation.

It will also add data into i18n_strings table for faster retrieval and indexing of groups of strings. Some string context doesn't have a numeric oid (I.e. content types), it will be set to zero.

This function checks for already existing string without context for this textgroup and updates it accordingly. It is intended for backwards compatibility, using already created strings.

Parameters

$name: Textgroup and location glued with ':'

$string: Source string (string in default language)

$format: Input format, for strings that will go through some filter

Return value

Update status.

1 call to i18nstrings_add_string()
i18nstrings_update_string in i18nstrings/i18nstrings.module
Update / create / remove string.

File

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

Code

function i18nstrings_add_string($name, $string, $format = NULL) {
  $context = i18nstrings_context($name, $string, $format);
  $location = i18nstrings_location($context);

  // Check if we have a source string.
  $source = i18nstrings_get_source($context, $string);

  // Default return status if nothing happens
  $status = -1;

  // The string may not be allowed for translation depending on its format.
  if (isset($format) && !i18nstrings_allowed_format($format)) {
    if ($source) {

      // The format may have changed and it's not allowed now, delete the source string
      return i18nstrings_remove_string($context);
    }
    else {

      // We just don't do anything
      return $status;
    }
  }
  if ($source) {
    if ($source->source != $string) {

      // String has changed
      db_query("UPDATE {locales_source} SET source = '%s', location = '%s' WHERE lid = %d", $string, $location, $source->lid);
      db_query("UPDATE {locales_target} SET i18n_status = %d WHERE lid = %d", I18NSTRINGS_STATUS_UPDATE, $source->lid);
      $status = SAVED_UPDATED;
    }
    elseif ($source->location != $location) {

      // It's not changed but it didn't have location set
      db_query("UPDATE {locales_source} SET location = '%s' WHERE lid = %d", $location, $source->lid);
      $status = SAVED_UPDATED;
    }

    // Complete metadata.
    $context->lid = $source->lid;
  }
  else {
    db_query("INSERT INTO {locales_source} (location, source, textgroup, version) VALUES ('%s', '%s', '%s', '%s')", $location, $string, $context->textgroup, 1);

    // Mysql just gets last id for latest query
    $context->lid = db_last_insert_id('locales_source', 'lid');

    // Clear locale cache so this string can be added in a later request.
    cache_clear_all('locale:' . $context->textgroup . ':', 'cache', TRUE);

    // Create string.
    $status = SAVED_NEW;
  }

  // Update metadata
  i18nstrings_save_context($context);
  return $status;
}