Geen omschrijving

entry.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Contains Translation_Entry class
  4. *
  5. * @version $Id: entry.php 1157 2015-11-20 04:30:11Z dd32 $
  6. * @package pomo
  7. * @subpackage entry
  8. */
  9. if ( ! class_exists( 'Translation_Entry', false ) ) :
  10. /**
  11. * Translation_Entry class encapsulates a translatable string.
  12. */
  13. class Translation_Entry {
  14. /**
  15. * Whether the entry contains a string and its plural form, default is false.
  16. *
  17. * @var bool
  18. */
  19. public $is_plural = false;
  20. public $context = null;
  21. public $singular = null;
  22. public $plural = null;
  23. public $translations = array();
  24. public $translator_comments = '';
  25. public $extracted_comments = '';
  26. public $references = array();
  27. public $flags = array();
  28. /**
  29. * @param array $args {
  30. * Arguments array, supports the following keys:
  31. *
  32. * @type string $singular The string to translate, if omitted an
  33. * empty entry will be created.
  34. * @type string $plural The plural form of the string, setting
  35. * this will set `$is_plural` to true.
  36. * @type array $translations Translations of the string and possibly
  37. * its plural forms.
  38. * @type string $context A string differentiating two equal strings
  39. * used in different contexts.
  40. * @type string $translator_comments Comments left by translators.
  41. * @type string $extracted_comments Comments left by developers.
  42. * @type array $references Places in the code this string is used, in
  43. * relative_to_root_path/file.php:linenum form.
  44. * @type array $flags Flags like php-format.
  45. * }
  46. */
  47. public function __construct( $args = array() ) {
  48. // If no singular -- empty object.
  49. if ( ! isset( $args['singular'] ) ) {
  50. return;
  51. }
  52. // Get member variable values from args hash.
  53. foreach ( $args as $varname => $value ) {
  54. $this->$varname = $value;
  55. }
  56. if ( isset( $args['plural'] ) && $args['plural'] ) {
  57. $this->is_plural = true;
  58. }
  59. if ( ! is_array( $this->translations ) ) {
  60. $this->translations = array();
  61. }
  62. if ( ! is_array( $this->references ) ) {
  63. $this->references = array();
  64. }
  65. if ( ! is_array( $this->flags ) ) {
  66. $this->flags = array();
  67. }
  68. }
  69. /**
  70. * PHP4 constructor.
  71. *
  72. * @deprecated 5.4.0 Use __construct() instead.
  73. *
  74. * @see Translation_Entry::__construct()
  75. */
  76. public function Translation_Entry( $args = array() ) {
  77. _deprecated_constructor( self::class, '5.4.0', static::class );
  78. self::__construct( $args );
  79. }
  80. /**
  81. * Generates a unique key for this entry.
  82. *
  83. * @return string|false The key or false if the entry is empty.
  84. */
  85. public function key() {
  86. if ( null === $this->singular || '' === $this->singular ) {
  87. return false;
  88. }
  89. // Prepend context and EOT, like in MO files.
  90. $key = ! $this->context ? $this->singular : $this->context . "\4" . $this->singular;
  91. // Standardize on \n line endings.
  92. $key = str_replace( array( "\r\n", "\r" ), "\n", $key );
  93. return $key;
  94. }
  95. /**
  96. * @param object $other
  97. */
  98. public function merge_with( &$other ) {
  99. $this->flags = array_unique( array_merge( $this->flags, $other->flags ) );
  100. $this->references = array_unique( array_merge( $this->references, $other->references ) );
  101. if ( $this->extracted_comments != $other->extracted_comments ) {
  102. $this->extracted_comments .= $other->extracted_comments;
  103. }
  104. }
  105. }
  106. endif;