entry.php 3.6 KB

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