_locale_import_one_string

  1. drupal
    1. 4.7
    2. 5
    3. 6 locale.inc
    4. 7 locale.inc
Versions
4.7 _locale_import_one_string($value, $mode, $lang = NULL)
5 _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NULL, $file = NULL)
6 – 7 _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NULL, $file = NULL, $group = 'default')

Imports a string into the database

@author Jacobo Tarrio

Parameters

$value Information about the string

▾ 2 functions call _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

Code

includes/locale.inc, line 631

<?php
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);
  }
  // Current string is the header information
  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;
  }
  // Some real string to import
  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++;
          } // translation exists
          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;
      }
    }

    // A simple translation
    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++;
        } // translation exists
        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++;
          }
        } // overwrite if empty string
        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++;
        }
      }
    }
  }
}
?>