GP_Format_PO::print_exported_file PHP Method

print_exported_file() public method

public print_exported_file ( $project, $locale, $translation_set, $entries )
    public function print_exported_file($project, $locale, $translation_set, $entries)
    {
        $po = new $this->class();
        // See https://www.gnu.org/software/gettext/manual/html_node/Header-Entry.html for header details.
        // TODO: add more meta data in the project: language team, report URL.
        $this->set_header($po, 'PO-Revision-Date', GP::$translation->last_modified($translation_set) . '+0000');
        $this->set_header($po, 'MIME-Version', '1.0');
        $this->set_header($po, 'Content-Type', 'text/plain; charset=UTF-8');
        $this->set_header($po, 'Content-Transfer-Encoding', '8bit');
        $this->set_header($po, 'Plural-Forms', "nplurals={$locale->nplurals}; plural={$locale->plural_expression};");
        $this->set_header($po, 'X-Generator', 'GlotPress/' . GP_VERSION);
        $language_code = $this->get_language_code($locale);
        if (false !== $language_code) {
            $this->set_header($po, 'Language', $language_code);
        }
        // Force export only current translations.
        $filters = array();
        $filters['status'] = 'current';
        foreach ($entries as $entry) {
            $po->add_entry($entry);
        }
        $current = $project;
        $project_tree = array();
        $project_tree[] = $current->name;
        while ($current->parent_project_id > 0) {
            $current = GP::$project->get($current->parent_project_id);
            $project_tree[] = $current->name;
        }
        $project_tree = array_reverse($project_tree);
        $project_id_version = implode(' - ', $project_tree);
        /**
         * Filter the project name and version header before export.
         *
         * @since 2.1.0
         *
         * @param string $project_id_version The default project name/version to use in the header and
         *                                   comments ( "Parent - Child - GrandChild - etc." by default).
         * @param array  $project_tree       An array of the parent/child project tree, ordered from Parent
         *                                   to child to grandchild to etc...
         */
        $project_id_version = apply_filters('gp_pomo_export_project_id_version', $project_id_version, $project_tree);
        $this->set_header($po, 'Project-Id-Version', $project_id_version);
        $this->add_comments_before_headers($po, "Translation of {$project_id_version} in {$locale->english_name}\n");
        $this->add_comments_before_headers($po, "This file is distributed under the same license as the {$project_id_version} package.\n");
        return $po->export();
    }

Usage Example

Exemplo n.º 1
0
 /**
  * @ticket GH-450
  */
 public function test_po_export_includes_project_id_version_header()
 {
     if ('GP_Format_PO' !== get_class($this->format)) {
         $this->markTestSkipped();
     }
     $parent_project_one = $this->factory->project->create();
     $parent_project_two = $this->factory->project->create(array('parent_project_id' => $parent_project_one->id));
     $set = $this->factory->translation_set->create_with_project_and_locale(array(), array('parent_project_id' => $parent_project_two->id));
     $project = $set->project;
     $locale = $this->factory->locale->create();
     $entries_for_export = $this->get_entries_for_export($set);
     $file = $this->format->print_exported_file($project, $locale, $set, $entries_for_export);
     $expected = sprintf('"Project-Id-Version: %s - %s - %s\\n"', $parent_project_one->name, $parent_project_two->name, $project->name);
     $this->assertContains($expected, $file);
 }