Make WordPress Core

Changeset 35714


Ignore:
Timestamp:
11/20/2015 04:33:12 AM (9 years ago)
Author:
dd32
Message:

Merge the changes to GlotPress's POMO from upstream to WordPress's copy.
Fixes #34748

Location:
trunk/src/wp-includes/pomo
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/pomo/entry.php

    r35686 r35714  
    33 * Contains Translation_Entry class
    44 *
    5  * @version $Id: entry.php 718 2012-10-31 00:32:02Z nbachiyski $
     5 * @version $Id: entry.php 1157 2015-11-20 04:30:11Z dd32 $
    66 * @package pomo
    77 * @subpackage entry
     
    5050            $this->$varname = $value;
    5151        }
    52         if (isset($args['plural'])) $this->is_plural = true;
     52        if (isset($args['plural']) && $args['plural']) $this->is_plural = true;
    5353        if (!is_array($this->translations)) $this->translations = array();
    5454        if (!is_array($this->references)) $this->references = array();
     
    6969     */
    7070    function key() {
    71         if (is_null($this->singular)) return false;
     71        if ( null === $this->singular || '' === $this->singular ) return false;
    7272
    7373        // Prepend context and EOT, like in MO files
    74         $key = is_null($this->context)? $this->singular : $this->context.chr(4).$this->singular;
     74        $key = !$this->context? $this->singular : $this->context.chr(4).$this->singular;
    7575        // Standardize on \n line endings
    7676        $key = str_replace( array( "\r\n", "\r" ), "\n", $key );
  • trunk/src/wp-includes/pomo/mo.php

    r34348 r35714  
    33 * Class for working with MO files
    44 *
    5  * @version $Id: mo.php 718 2012-10-31 00:32:02Z nbachiyski $
     5 * @version $Id: mo.php 1157 2015-11-20 04:30:11Z dd32 $
    66 * @package pomo
    77 * @subpackage mo
     
    125125        $exported = $entry->singular;
    126126        if ($entry->is_plural) $exported .= chr(0).$entry->plural;
    127         if (!is_null($entry->context)) $exported = $entry->context . chr(4) . $exported;
     127        if ($entry->context) $exported = $entry->context . chr(4) . $exported;
    128128        return $exported;
    129129    }
     
    135135    function export_translations($entry) {
    136136        //TODO: warnings for control characters
    137         return implode(chr(0), $entry->translations);
     137        return $entry->is_plural ? implode(chr(0), $entry->translations) : $entry->translations[0];
    138138    }
    139139
  • trunk/src/wp-includes/pomo/po.php

    r34348 r35714  
    33 * Class for working with PO files
    44 *
    5  * @version $Id: po.php 718 2012-10-31 00:32:02Z nbachiyski $
     5 * @version $Id: po.php 1158 2015-11-20 04:31:23Z dd32 $
    66 * @package pomo
    77 * @subpackage po
     
    1010require_once dirname(__FILE__) . '/translations.php';
    1111
    12 define('PO_MAX_LINE_LEN', 79);
     12if ( ! defined( 'PO_MAX_LINE_LEN' ) ) {
     13    define('PO_MAX_LINE_LEN', 79);
     14}
    1315
    1416ini_set('auto_detect_line_endings', 1);
     
    98100     * @return string the poified string
    99101     */
    100     function poify($string) {
     102    public static function poify($string) {
    101103        $quote = '"';
    102104        $slash = '\\';
     
    129131     * @return string enascaped string
    130132     */
    131     function unpoify($string) {
    132         $escapes = array('t' => "\t", 'n' => "\n", '\\' => '\\');
     133    public static function unpoify($string) {
     134        $escapes = array('t' => "\t", 'n' => "\n", 'r' => "\r", '\\' => '\\');
    133135        $lines = array_map('trim', explode("\n", $string));
    134136        $lines = array_map(array('PO', 'trim_quotes'), $lines);
     
    150152            }
    151153        }
     154
     155        // Standardise the line endings on imported content, technically PO files shouldn't contain \r
     156        $unpoified = str_replace( array( "\r\n", "\r" ), "\n", $unpoified );
     157
    152158        return $unpoified;
    153159    }
     
    161167     * @param string $with prepend lines with this string
    162168     */
    163     function prepend_each_line($string, $with) {
     169    public static function prepend_each_line($string, $with) {
    164170        $php_with = var_export($with, true);
    165171        $lines = explode("\n", $string);
     
    181187     *  like :, default is a space
    182188     */
    183     function comment_block($text, $char=' ') {
     189    public static function comment_block($text, $char=' ') {
    184190        $text = wordwrap($text, PO_MAX_LINE_LEN - 3);
    185191        return PO::prepend_each_line($text, "#$char ");
     
    194200     *  false if the entry is empty
    195201     */
    196     function export_entry(&$entry) {
    197         if (is_null($entry->singular)) return false;
     202    public static function export_entry(&$entry) {
     203        if ( null === $entry->singular || '' === $entry->singular ) return false;
    198204        $po = array();
    199205        if (!empty($entry->translator_comments)) $po[] = PO::comment_block($entry->translator_comments);
     
    201207        if (!empty($entry->references)) $po[] = PO::comment_block(implode(' ', $entry->references), ':');
    202208        if (!empty($entry->flags)) $po[] = PO::comment_block(implode(", ", $entry->flags), ',');
    203         if (!is_null($entry->context)) $po[] = 'msgctxt '.PO::poify($entry->context);
     209        if ($entry->context) $po[] = 'msgctxt '.PO::poify($entry->context);
    204210        $po[] = 'msgid '.PO::poify($entry->singular);
    205211        if (!$entry->is_plural) {
    206212            $translation = empty($entry->translations)? '' : $entry->translations[0];
     213            $translation = PO::match_begin_and_end_newlines( $translation, $entry->singular );
    207214            $po[] = 'msgstr '.PO::poify($translation);
    208215        } else {
     
    210217            $translations = empty($entry->translations)? array('', '') : $entry->translations;
    211218            foreach($translations as $i => $translation) {
     219                $translation = PO::match_begin_and_end_newlines( $translation, $entry->plural );
    212220                $po[] = "msgstr[$i] ".PO::poify($translation);
    213221            }
    214222        }
    215223        return implode("\n", $po);
     224    }
     225
     226    public static function match_begin_and_end_newlines( $translation, $original ) {
     227        if ( '' === $translation ) {
     228            return $translation;
     229        }
     230
     231        $original_begin = "\n" === substr( $original, 0, 1 );
     232        $original_end = "\n" === substr( $original, -1 );
     233        $translation_begin = "\n" === substr( $translation, 0, 1 );
     234        $translation_end = "\n" === substr( $translation, -1 );
     235
     236        if ( $original_begin ) {
     237            if ( ! $translation_begin ) {
     238                $translation = "\n" . $translation;
     239            }
     240        } elseif ( $translation_begin ) {
     241            $translation = ltrim( $translation, "\n" );
     242        }
     243
     244        if ( $original_end ) {
     245            if ( ! $translation_end ) {
     246                $translation .= "\n";
     247            }
     248        } elseif ( $translation_end ) {
     249            $translation = rtrim( $translation, "\n" );
     250        }
     251
     252        return $translation;
    216253    }
    217254
     
    401438     * @return sring
    402439     */
    403     function trim_quotes($s) {
     440    public static function trim_quotes($s) {
    404441        if ( substr($s, 0, 1) == '"') $s = substr($s, 1);
    405442        if ( substr($s, -1, 1) == '"') $s = substr($s, 0, -1);
  • trunk/src/wp-includes/pomo/streams.php

    r34348 r35714  
    44 * Based on the classes from Danilo Segan <danilo@kvota.net>
    55 *
    6  * @version $Id: streams.php 718 2012-10-31 00:32:02Z nbachiyski $
     6 * @version $Id: streams.php 1157 2015-11-20 04:30:11Z dd32 $
    77 * @package pomo
    88 * @subpackage streams
  • trunk/src/wp-includes/pomo/translations.php

    r35686 r35714  
    33 * Class for a set of entries for translation and their associated headers
    44 *
    5  * @version $Id: translations.php 718 2012-10-31 00:32:02Z nbachiyski $
     5 * @version $Id: translations.php 1157 2015-11-20 04:30:11Z dd32 $
    66 * @package pomo
    77 * @subpackage translations
Note: See TracChangeset for help on using the changeset viewer.