You are here

function _locale_import_one_string in Drupal 4

Same name and namespace in other branches
  1. 5 includes/locale.inc \_locale_import_one_string()
  2. 6 includes/locale.inc \_locale_import_one_string()
  3. 7 includes/locale.inc \_locale_import_one_string()

Imports a string into the database

@author Jacobo Tarrio

Parameters

$value Information about the string:

2 calls to _locale_import_one_string()
_locale_import_po in includes/locale.inc
Parses Gettext Portable Object file information and inserts into database
_locale_import_read_po in includes/locale.inc
Parses Gettext Portable Object file into an array

File

includes/locale.inc, line 630
Admin-related functions for locale.module.

Code

function _locale_import_one_string($value, $mode, $lang = NULL) {
  static $additions = 0;
  static $updates = 0;
  static $headerdone = FALSE;

  // Report the changes made (called at end of import)
  if ($value == 'report') {
    return array(
      $headerdone,
      $additions,
      $updates,
    );
  }
  elseif ($value['msgid'] == '') {
    $hdr = _locale_import_parse_header($value['msgstr']);

    // Get the plural formula
    if ($hdr["Plural-Forms"] && ($p = _locale_import_parse_plural_forms($hdr["Plural-Forms"], $file->filename))) {
      list($nplurals, $plural) = $p;
      db_query("UPDATE {locales_meta} SET plurals = %d, formula = '%s' WHERE locale = '%s'", $nplurals, $plural, $lang);
    }
    else {
      db_query("UPDATE {locales_meta} SET plurals = %d, formula = '%s' WHERE locale = '%s'", 0, '', $lang);
    }
    $headerdone = TRUE;
  }
  else {
    $comments = filter_xss_admin(_locale_import_shorten_comments($value['#']));

    // Handle a translation for some plural string
    if (strpos($value['msgid'], "\0")) {
      $english = explode("\0", $value['msgid'], 2);
      $entries = array_keys($value['msgstr']);
      for ($i = 3; $i <= count($entries); $i++) {
        $english[] = $english[1];
      }
      $translation = array_map('_locale_import_append_plural', $value['msgstr'], $entries);
      $english = array_map('_locale_import_append_plural', $english, $entries);
      foreach ($translation as $key => $trans) {
        if ($key == 0) {
          $plid = 0;
        }
        $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $english[$key]));
        if ($loc->lid) {

          // a string exists
          $lid = $loc->lid;

          // update location field
          db_query("UPDATE {locales_source} SET location = '%s' WHERE lid = %d", $comments, $lid);
          $trans2 = db_fetch_object(db_query("SELECT lid, translation, plid, plural FROM {locales_target} WHERE lid = %d AND locale = '%s'", $lid, $lang));
          if (!$trans2->lid) {

            // no translation in current language
            db_query("INSERT INTO {locales_target} (lid, locale, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $lang, filter_xss_admin($trans), $plid, $key);
            $additions++;
          }
          else {
            if ($mode == 'overwrite' || $trans2->translation == '') {
              db_query("UPDATE {locales_target} SET translation = '%s', plid = %d, plural = %d WHERE locale = '%s' AND lid = %d", filter_xss_admin($trans), $plid, $key, $lang, $lid);
              if ($trans2->translation == '') {
                $additions++;
              }
              else {
                $updates++;
              }
            }
          }
        }
        else {

          // no string
          db_query("INSERT INTO {locales_source} (location, source) VALUES ('%s', '%s')", $comments, filter_xss_admin($english[$key]));
          $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $english[$key]));
          $lid = $loc->lid;
          db_query("INSERT INTO {locales_target} (lid, locale, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $lang, filter_xss_admin($trans), $plid, $key);
          if ($trans != '') {
            $additions++;
          }
        }
        $plid = $lid;
      }
    }
    else {
      $english = $value['msgid'];
      $translation = $value['msgstr'];
      $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $english));
      if ($loc->lid) {

        // a string exists
        $lid = $loc->lid;

        // update location field
        db_query("UPDATE {locales_source} SET location = '%s' WHERE source = '%s'", $comments, $english);
        $trans = db_fetch_object(db_query("SELECT lid, translation FROM {locales_target} WHERE lid = %d AND locale = '%s'", $lid, $lang));
        if (!$trans->lid) {

          // no translation in current language
          db_query("INSERT INTO {locales_target} (lid, locale, translation) VALUES (%d, '%s', '%s')", $lid, $lang, filter_xss_admin($translation));
          $additions++;
        }
        else {
          if ($mode == 'overwrite') {

            //overwrite in any case
            db_query("UPDATE {locales_target} SET translation = '%s' WHERE locale = '%s' AND lid = %d", filter_xss_admin($translation), $lang, $lid);
            if ($trans->translation == '') {
              $additions++;
            }
            else {
              $updates++;
            }
          }
          else {
            if ($trans->translation == '') {
              db_query("UPDATE {locales_target} SET translation = '%s' WHERE locale = '%s' AND lid = %d", $translation, $lang, $lid);
              $additions++;
            }
          }
        }
      }
      else {

        // no string
        db_query("INSERT INTO {locales_source} (location, source) VALUES ('%s', '%s')", $comments, $english);
        $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $english));
        $lid = $loc->lid;
        db_query("INSERT INTO {locales_target} (lid, locale, translation) VALUES (%d, '%s', '%s')", $lid, $lang, filter_xss_admin($translation));
        if ($translation != '') {
          $additions++;
        }
      }
    }
  }
}