Edit File by line
/home/barbar84/public_h.../wp-inclu.../pomo
File: translations.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Class for a set of entries for translation and their associated headers
[2] Fix | Delete
*
[3] Fix | Delete
* @version $Id: translations.php 1157 2015-11-20 04:30:11Z dd32 $
[4] Fix | Delete
* @package pomo
[5] Fix | Delete
* @subpackage translations
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
require_once __DIR__ . '/plural-forms.php';
[9] Fix | Delete
require_once __DIR__ . '/entry.php';
[10] Fix | Delete
[11] Fix | Delete
if ( ! class_exists( 'Translations', false ) ) :
[12] Fix | Delete
class Translations {
[13] Fix | Delete
public $entries = array();
[14] Fix | Delete
public $headers = array();
[15] Fix | Delete
[16] Fix | Delete
/**
[17] Fix | Delete
* Add entry to the PO structure
[18] Fix | Delete
*
[19] Fix | Delete
* @param array|Translation_Entry $entry
[20] Fix | Delete
* @return bool true on success, false if the entry doesn't have a key
[21] Fix | Delete
*/
[22] Fix | Delete
function add_entry( $entry ) {
[23] Fix | Delete
if ( is_array( $entry ) ) {
[24] Fix | Delete
$entry = new Translation_Entry( $entry );
[25] Fix | Delete
}
[26] Fix | Delete
$key = $entry->key();
[27] Fix | Delete
if ( false === $key ) {
[28] Fix | Delete
return false;
[29] Fix | Delete
}
[30] Fix | Delete
$this->entries[ $key ] = &$entry;
[31] Fix | Delete
return true;
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* @param array|Translation_Entry $entry
[36] Fix | Delete
* @return bool
[37] Fix | Delete
*/
[38] Fix | Delete
function add_entry_or_merge( $entry ) {
[39] Fix | Delete
if ( is_array( $entry ) ) {
[40] Fix | Delete
$entry = new Translation_Entry( $entry );
[41] Fix | Delete
}
[42] Fix | Delete
$key = $entry->key();
[43] Fix | Delete
if ( false === $key ) {
[44] Fix | Delete
return false;
[45] Fix | Delete
}
[46] Fix | Delete
if ( isset( $this->entries[ $key ] ) ) {
[47] Fix | Delete
$this->entries[ $key ]->merge_with( $entry );
[48] Fix | Delete
} else {
[49] Fix | Delete
$this->entries[ $key ] = &$entry;
[50] Fix | Delete
}
[51] Fix | Delete
return true;
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
/**
[55] Fix | Delete
* Sets $header PO header to $value
[56] Fix | Delete
*
[57] Fix | Delete
* If the header already exists, it will be overwritten
[58] Fix | Delete
*
[59] Fix | Delete
* TODO: this should be out of this class, it is gettext specific
[60] Fix | Delete
*
[61] Fix | Delete
* @param string $header header name, without trailing :
[62] Fix | Delete
* @param string $value header value, without trailing \n
[63] Fix | Delete
*/
[64] Fix | Delete
function set_header( $header, $value ) {
[65] Fix | Delete
$this->headers[ $header ] = $value;
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* @param array $headers
[70] Fix | Delete
*/
[71] Fix | Delete
function set_headers( $headers ) {
[72] Fix | Delete
foreach ( $headers as $header => $value ) {
[73] Fix | Delete
$this->set_header( $header, $value );
[74] Fix | Delete
}
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
/**
[78] Fix | Delete
* @param string $header
[79] Fix | Delete
*/
[80] Fix | Delete
function get_header( $header ) {
[81] Fix | Delete
return isset( $this->headers[ $header ] ) ? $this->headers[ $header ] : false;
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
/**
[85] Fix | Delete
* @param Translation_Entry $entry
[86] Fix | Delete
*/
[87] Fix | Delete
function translate_entry( &$entry ) {
[88] Fix | Delete
$key = $entry->key();
[89] Fix | Delete
return isset( $this->entries[ $key ] ) ? $this->entries[ $key ] : false;
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
/**
[93] Fix | Delete
* @param string $singular
[94] Fix | Delete
* @param string $context
[95] Fix | Delete
* @return string
[96] Fix | Delete
*/
[97] Fix | Delete
function translate( $singular, $context = null ) {
[98] Fix | Delete
$entry = new Translation_Entry(
[99] Fix | Delete
array(
[100] Fix | Delete
'singular' => $singular,
[101] Fix | Delete
'context' => $context,
[102] Fix | Delete
)
[103] Fix | Delete
);
[104] Fix | Delete
$translated = $this->translate_entry( $entry );
[105] Fix | Delete
return ( $translated && ! empty( $translated->translations ) ) ? $translated->translations[0] : $singular;
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
/**
[109] Fix | Delete
* Given the number of items, returns the 0-based index of the plural form to use
[110] Fix | Delete
*
[111] Fix | Delete
* Here, in the base Translations class, the common logic for English is implemented:
[112] Fix | Delete
* 0 if there is one element, 1 otherwise
[113] Fix | Delete
*
[114] Fix | Delete
* This function should be overridden by the subclasses. For example MO/PO can derive the logic
[115] Fix | Delete
* from their headers.
[116] Fix | Delete
*
[117] Fix | Delete
* @param int $count number of items
[118] Fix | Delete
*/
[119] Fix | Delete
function select_plural_form( $count ) {
[120] Fix | Delete
return 1 == $count ? 0 : 1;
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
/**
[124] Fix | Delete
* @return int
[125] Fix | Delete
*/
[126] Fix | Delete
function get_plural_forms_count() {
[127] Fix | Delete
return 2;
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
/**
[131] Fix | Delete
* @param string $singular
[132] Fix | Delete
* @param string $plural
[133] Fix | Delete
* @param int $count
[134] Fix | Delete
* @param string $context
[135] Fix | Delete
*/
[136] Fix | Delete
function translate_plural( $singular, $plural, $count, $context = null ) {
[137] Fix | Delete
$entry = new Translation_Entry(
[138] Fix | Delete
array(
[139] Fix | Delete
'singular' => $singular,
[140] Fix | Delete
'plural' => $plural,
[141] Fix | Delete
'context' => $context,
[142] Fix | Delete
)
[143] Fix | Delete
);
[144] Fix | Delete
$translated = $this->translate_entry( $entry );
[145] Fix | Delete
$index = $this->select_plural_form( $count );
[146] Fix | Delete
$total_plural_forms = $this->get_plural_forms_count();
[147] Fix | Delete
if ( $translated && 0 <= $index && $index < $total_plural_forms &&
[148] Fix | Delete
is_array( $translated->translations ) &&
[149] Fix | Delete
isset( $translated->translations[ $index ] ) ) {
[150] Fix | Delete
return $translated->translations[ $index ];
[151] Fix | Delete
} else {
[152] Fix | Delete
return 1 == $count ? $singular : $plural;
[153] Fix | Delete
}
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
/**
[157] Fix | Delete
* Merge $other in the current object.
[158] Fix | Delete
*
[159] Fix | Delete
* @param Object $other Another Translation object, whose translations will be merged in this one (passed by reference).
[160] Fix | Delete
* @return void
[161] Fix | Delete
*/
[162] Fix | Delete
function merge_with( &$other ) {
[163] Fix | Delete
foreach ( $other->entries as $entry ) {
[164] Fix | Delete
$this->entries[ $entry->key() ] = $entry;
[165] Fix | Delete
}
[166] Fix | Delete
}
[167] Fix | Delete
[168] Fix | Delete
/**
[169] Fix | Delete
* @param object $other
[170] Fix | Delete
*/
[171] Fix | Delete
function merge_originals_with( &$other ) {
[172] Fix | Delete
foreach ( $other->entries as $entry ) {
[173] Fix | Delete
if ( ! isset( $this->entries[ $entry->key() ] ) ) {
[174] Fix | Delete
$this->entries[ $entry->key() ] = $entry;
[175] Fix | Delete
} else {
[176] Fix | Delete
$this->entries[ $entry->key() ]->merge_with( $entry );
[177] Fix | Delete
}
[178] Fix | Delete
}
[179] Fix | Delete
}
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
class Gettext_Translations extends Translations {
[183] Fix | Delete
/**
[184] Fix | Delete
* The gettext implementation of select_plural_form.
[185] Fix | Delete
*
[186] Fix | Delete
* It lives in this class, because there are more than one descendand, which will use it and
[187] Fix | Delete
* they can't share it effectively.
[188] Fix | Delete
*
[189] Fix | Delete
* @param int $count
[190] Fix | Delete
*/
[191] Fix | Delete
function gettext_select_plural_form( $count ) {
[192] Fix | Delete
if ( ! isset( $this->_gettext_select_plural_form ) || is_null( $this->_gettext_select_plural_form ) ) {
[193] Fix | Delete
list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header( $this->get_header( 'Plural-Forms' ) );
[194] Fix | Delete
$this->_nplurals = $nplurals;
[195] Fix | Delete
$this->_gettext_select_plural_form = $this->make_plural_form_function( $nplurals, $expression );
[196] Fix | Delete
}
[197] Fix | Delete
return call_user_func( $this->_gettext_select_plural_form, $count );
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
/**
[201] Fix | Delete
* @param string $header
[202] Fix | Delete
* @return array
[203] Fix | Delete
*/
[204] Fix | Delete
function nplurals_and_expression_from_header( $header ) {
[205] Fix | Delete
if ( preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches ) ) {
[206] Fix | Delete
$nplurals = (int) $matches[1];
[207] Fix | Delete
$expression = trim( $matches[2] );
[208] Fix | Delete
return array( $nplurals, $expression );
[209] Fix | Delete
} else {
[210] Fix | Delete
return array( 2, 'n != 1' );
[211] Fix | Delete
}
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
/**
[215] Fix | Delete
* Makes a function, which will return the right translation index, according to the
[216] Fix | Delete
* plural forms header
[217] Fix | Delete
*
[218] Fix | Delete
* @param int $nplurals
[219] Fix | Delete
* @param string $expression
[220] Fix | Delete
*/
[221] Fix | Delete
function make_plural_form_function( $nplurals, $expression ) {
[222] Fix | Delete
try {
[223] Fix | Delete
$handler = new Plural_Forms( rtrim( $expression, ';' ) );
[224] Fix | Delete
return array( $handler, 'get' );
[225] Fix | Delete
} catch ( Exception $e ) {
[226] Fix | Delete
// Fall back to default plural-form function.
[227] Fix | Delete
return $this->make_plural_form_function( 2, 'n != 1' );
[228] Fix | Delete
}
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
/**
[232] Fix | Delete
* Adds parentheses to the inner parts of ternary operators in
[233] Fix | Delete
* plural expressions, because PHP evaluates ternary oerators from left to right
[234] Fix | Delete
*
[235] Fix | Delete
* @param string $expression the expression without parentheses
[236] Fix | Delete
* @return string the expression with parentheses added
[237] Fix | Delete
*/
[238] Fix | Delete
function parenthesize_plural_exression( $expression ) {
[239] Fix | Delete
$expression .= ';';
[240] Fix | Delete
$res = '';
[241] Fix | Delete
$depth = 0;
[242] Fix | Delete
for ( $i = 0; $i < strlen( $expression ); ++$i ) {
[243] Fix | Delete
$char = $expression[ $i ];
[244] Fix | Delete
switch ( $char ) {
[245] Fix | Delete
case '?':
[246] Fix | Delete
$res .= ' ? (';
[247] Fix | Delete
$depth++;
[248] Fix | Delete
break;
[249] Fix | Delete
case ':':
[250] Fix | Delete
$res .= ') : (';
[251] Fix | Delete
break;
[252] Fix | Delete
case ';':
[253] Fix | Delete
$res .= str_repeat( ')', $depth ) . ';';
[254] Fix | Delete
$depth = 0;
[255] Fix | Delete
break;
[256] Fix | Delete
default:
[257] Fix | Delete
$res .= $char;
[258] Fix | Delete
}
[259] Fix | Delete
}
[260] Fix | Delete
return rtrim( $res, ';' );
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
/**
[264] Fix | Delete
* @param string $translation
[265] Fix | Delete
* @return array
[266] Fix | Delete
*/
[267] Fix | Delete
function make_headers( $translation ) {
[268] Fix | Delete
$headers = array();
[269] Fix | Delete
// Sometimes \n's are used instead of real new lines.
[270] Fix | Delete
$translation = str_replace( '\n', "\n", $translation );
[271] Fix | Delete
$lines = explode( "\n", $translation );
[272] Fix | Delete
foreach ( $lines as $line ) {
[273] Fix | Delete
$parts = explode( ':', $line, 2 );
[274] Fix | Delete
if ( ! isset( $parts[1] ) ) {
[275] Fix | Delete
continue;
[276] Fix | Delete
}
[277] Fix | Delete
$headers[ trim( $parts[0] ) ] = trim( $parts[1] );
[278] Fix | Delete
}
[279] Fix | Delete
return $headers;
[280] Fix | Delete
}
[281] Fix | Delete
[282] Fix | Delete
/**
[283] Fix | Delete
* @param string $header
[284] Fix | Delete
* @param string $value
[285] Fix | Delete
*/
[286] Fix | Delete
function set_header( $header, $value ) {
[287] Fix | Delete
parent::set_header( $header, $value );
[288] Fix | Delete
if ( 'Plural-Forms' === $header ) {
[289] Fix | Delete
list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header( $this->get_header( 'Plural-Forms' ) );
[290] Fix | Delete
$this->_nplurals = $nplurals;
[291] Fix | Delete
$this->_gettext_select_plural_form = $this->make_plural_form_function( $nplurals, $expression );
[292] Fix | Delete
}
[293] Fix | Delete
}
[294] Fix | Delete
}
[295] Fix | Delete
endif;
[296] Fix | Delete
[297] Fix | Delete
if ( ! class_exists( 'NOOP_Translations', false ) ) :
[298] Fix | Delete
/**
[299] Fix | Delete
* Provides the same interface as Translations, but doesn't do anything
[300] Fix | Delete
*/
[301] Fix | Delete
class NOOP_Translations {
[302] Fix | Delete
public $entries = array();
[303] Fix | Delete
public $headers = array();
[304] Fix | Delete
[305] Fix | Delete
function add_entry( $entry ) {
[306] Fix | Delete
return true;
[307] Fix | Delete
}
[308] Fix | Delete
[309] Fix | Delete
/**
[310] Fix | Delete
* @param string $header
[311] Fix | Delete
* @param string $value
[312] Fix | Delete
*/
[313] Fix | Delete
function set_header( $header, $value ) {
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
/**
[317] Fix | Delete
* @param array $headers
[318] Fix | Delete
*/
[319] Fix | Delete
function set_headers( $headers ) {
[320] Fix | Delete
}
[321] Fix | Delete
[322] Fix | Delete
/**
[323] Fix | Delete
* @param string $header
[324] Fix | Delete
* @return false
[325] Fix | Delete
*/
[326] Fix | Delete
function get_header( $header ) {
[327] Fix | Delete
return false;
[328] Fix | Delete
}
[329] Fix | Delete
[330] Fix | Delete
/**
[331] Fix | Delete
* @param Translation_Entry $entry
[332] Fix | Delete
* @return false
[333] Fix | Delete
*/
[334] Fix | Delete
function translate_entry( &$entry ) {
[335] Fix | Delete
return false;
[336] Fix | Delete
}
[337] Fix | Delete
[338] Fix | Delete
/**
[339] Fix | Delete
* @param string $singular
[340] Fix | Delete
* @param string $context
[341] Fix | Delete
*/
[342] Fix | Delete
function translate( $singular, $context = null ) {
[343] Fix | Delete
return $singular;
[344] Fix | Delete
}
[345] Fix | Delete
[346] Fix | Delete
/**
[347] Fix | Delete
* @param int $count
[348] Fix | Delete
* @return bool
[349] Fix | Delete
*/
[350] Fix | Delete
function select_plural_form( $count ) {
[351] Fix | Delete
return 1 == $count ? 0 : 1;
[352] Fix | Delete
}
[353] Fix | Delete
[354] Fix | Delete
/**
[355] Fix | Delete
* @return int
[356] Fix | Delete
*/
[357] Fix | Delete
function get_plural_forms_count() {
[358] Fix | Delete
return 2;
[359] Fix | Delete
}
[360] Fix | Delete
[361] Fix | Delete
/**
[362] Fix | Delete
* @param string $singular
[363] Fix | Delete
* @param string $plural
[364] Fix | Delete
* @param int $count
[365] Fix | Delete
* @param string $context
[366] Fix | Delete
*/
[367] Fix | Delete
function translate_plural( $singular, $plural, $count, $context = null ) {
[368] Fix | Delete
return 1 == $count ? $singular : $plural;
[369] Fix | Delete
}
[370] Fix | Delete
[371] Fix | Delete
/**
[372] Fix | Delete
* @param object $other
[373] Fix | Delete
*/
[374] Fix | Delete
function merge_with( &$other ) {
[375] Fix | Delete
}
[376] Fix | Delete
}
[377] Fix | Delete
endif;
[378] Fix | Delete
[379] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function