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

Exports a Portable Object (Template) file for a language

Parameters

$language Selects a language to generate the output for:

1 call to _locale_export_po()
_locale_export_po_submit in includes/locale.inc
Process a locale export form submissions.
1 string reference to '_locale_export_po'
_locale_admin_export_screen in includes/locale.inc
User interface for the translation export screen

File

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

Code

function _locale_export_po($language) {
  global $user;

  // Get language specific strings, or all strings
  if ($language) {
    $meta = db_fetch_object(db_query("SELECT * FROM {locales_meta} WHERE locale = '%s'", $language));
    $result = db_query("SELECT s.lid, s.source, s.location, t.translation, t.plid, t.plural FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE t.locale = '%s' ORDER BY t.plid, t.plural", $language);
  }
  else {
    $result = db_query("SELECT s.lid, s.source, s.location, t.plid, t.plural FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid ORDER BY t.plid, t.plural");
  }

  // Build array out of the database results
  $parent = array();
  while ($child = db_fetch_object($result)) {
    if ($child->source != '') {
      $parent[$child->lid]['comment'] = $child->location;
      $parent[$child->lid]['msgid'] = $child->source;
      $parent[$child->lid]['translation'] = $child->translation;
      if ($child->plid) {
        $parent[$child->lid]['child'] = 1;
        $parent[$child->plid]['plural'] = $child->lid;
      }
    }
  }

  // Generating Portable Object file for a language
  if ($language) {
    $filename = $language . '.po';
    $header .= "# {$meta->name} translation of " . variable_get('site_name', 'Drupal') . "\n";
    $header .= '# Copyright (c) ' . date('Y') . ' ' . $user->name . ' <' . $user->mail . ">\n";
    $header .= "#\n";
    $header .= "msgid \"\"\n";
    $header .= "msgstr \"\"\n";
    $header .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n";
    $header .= "\"POT-Creation-Date: " . date("Y-m-d H:iO") . "\\n\"\n";
    $header .= "\"PO-Revision-Date: " . date("Y-m-d H:iO") . "\\n\"\n";
    $header .= "\"Last-Translator: " . $user->name . ' <' . $user->mail . ">\\n\"\n";
    $header .= "\"Language-Team: " . $meta->name . ' <' . $user->mail . ">\\n\"\n";
    $header .= "\"MIME-Version: 1.0\\n\"\n";
    $header .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
    $header .= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
    if ($meta->formula && $meta->plurals) {
      $header .= "\"Plural-Forms: nplurals=" . $meta->plurals . "; plural=" . strtr($meta->formula, array(
        '$' => '',
      )) . ";\\n\"\n";
    }
    $header .= "\n";
    watchdog('locale', t('Exported %locale translation file: %filename.', array(
      '%locale' => theme('placeholder', $meta->name),
      '%filename' => theme('placeholder', $filename),
    )));
  }
  else {
    $filename = 'drupal.pot';
    $header .= "# LANGUAGE translation of PROJECT\n";
    $header .= "# Copyright (c) YEAR NAME <EMAIL@ADDRESS>\n";
    $header .= "#\n";
    $header .= "msgid \"\"\n";
    $header .= "msgstr \"\"\n";
    $header .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n";
    $header .= "\"POT-Creation-Date: " . date("Y-m-d H:iO") . "\\n\"\n";
    $header .= "\"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\\n\"\n";
    $header .= "\"Last-Translator: NAME <EMAIL@ADDRESS>\\n\"\n";
    $header .= "\"Language-Team: LANGUAGE <EMAIL@ADDRESS>\\n\"\n";
    $header .= "\"MIME-Version: 1.0\\n\"\n";
    $header .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
    $header .= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
    $header .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n";
    $header .= "\n";
    watchdog('locale', t('Exported translation file: %filename.', array(
      '%filename' => theme('placeholder', $filename),
    )));
  }

  // Start download process
  header("Content-Disposition: attachment; filename={$filename}");
  header("Content-Type: text/plain; charset=utf-8");
  print $header;
  foreach ($parent as $lid => $message) {
    if (!isset($message['child'])) {
      if ($message['comment']) {
        print '#: ' . $message['comment'] . "\n";
      }
      print 'msgid ' . _locale_export_print($message['msgid']);
      if ($plural = $message['plural']) {
        print 'msgid_plural ' . _locale_export_print($parent[$plural]['msgid']);
        if ($language) {
          $translation = $message['translation'];
          for ($i = 0; $i < $meta->plurals; $i++) {
            print 'msgstr[' . $i . '] ' . _locale_export_print($translation);
            if ($plural) {
              $translation = $parent[$plural]['translation'];
              if ($i > 1) {
                $translation = _locale_export_remove_plural($translation);
              }
              $plural = $parent[$plural]['plural'];
            }
            else {
              $translation = '';
            }
          }
        }
        else {
          print 'msgstr[0] ""' . "\n";
          print 'msgstr[1] ""' . "\n";
        }
      }
      else {
        if ($language) {
          print 'msgstr ' . _locale_export_print($message['translation']);
        }
        else {
          print 'msgstr ""' . "\n";
        }
      }
      print "\n";
    }
  }
  die;
}