Title: Gettext_Translations
Published: April 25, 2014
Last modified: August 20, 2026

---

# class Gettext_Translations {}

## In this article

 * [Methods](https://developer.wordpress.org/reference/classes/gettext_translations/?output_format=md#methods)
 * [Source](https://developer.wordpress.org/reference/classes/gettext_translations/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/gettext_translations/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/gettext_translations/?output_format=md#changelog)

[Gettext_Translations](https://developer.wordpress.org/reference/classes/gettext_translations/)
class.

## 󠀁[Methods](https://developer.wordpress.org/reference/classes/gettext_translations/?output_format=md#methods)󠁿

| Name | Description | 
| [Gettext_Translations::gettext_select_plural_form](https://developer.wordpress.org/reference/classes/gettext_translations/gettext_select_plural_form/) | The gettext implementation of select_plural_form. | 
| [Gettext_Translations::make_headers](https://developer.wordpress.org/reference/classes/gettext_translations/make_headers/) | Prepare translation headers. | 
| [Gettext_Translations::make_plural_form_function](https://developer.wordpress.org/reference/classes/gettext_translations/make_plural_form_function/) | Makes a function, which will return the right translation index, according to the plural forms header. | 
| [Gettext_Translations::nplurals_and_expression_from_header](https://developer.wordpress.org/reference/classes/gettext_translations/nplurals_and_expression_from_header/) | Returns the nplurals and plural forms expression from the Plural-Forms header. | 
| [Gettext_Translations::parenthesize_plural_exression](https://developer.wordpress.org/reference/classes/gettext_translations/parenthesize_plural_exression/) | Adds parentheses to the inner parts of ternary operators in plural expressions, because PHP evaluates ternary operators from left to right — deprecated | 
| [Gettext_Translations::set_header](https://developer.wordpress.org/reference/classes/gettext_translations/set_header/) | Sets translation headers. |

## 󠀁[Source](https://developer.wordpress.org/reference/classes/gettext_translations/?output_format=md#source)󠁿

    ```php
    class Gettext_Translations extends Translations {

    	/**
    	 * Number of plural forms.
    	 *
    	 * @var int
    	 *
    	 * @since 2.8.0
    	 */
    	public $_nplurals;

    	/**
    	 * Callback to retrieve the plural form.
    	 *
    	 * @var callable
    	 *
    	 * @since 2.8.0
    	 */
    	public $_gettext_select_plural_form;

    	/**
    	 * The gettext implementation of select_plural_form.
    	 *
    	 * It lives in this class, because there are more than one descendant, which will use it and
    	 * they can't share it effectively.
    	 *
    	 * @since 2.8.0
    	 *
    	 * @param int $count Plural forms count.
    	 * @return int Plural form to use.
    	 */
    	public function gettext_select_plural_form( $count ) {
    		if ( ! isset( $this->_gettext_select_plural_form ) || is_null( $this->_gettext_select_plural_form ) ) {
    			list( $nplurals, $expression )     = $this->nplurals_and_expression_from_header( $this->get_header( 'Plural-Forms' ) );
    			$this->_nplurals                   = $nplurals;
    			$this->_gettext_select_plural_form = $this->make_plural_form_function( $nplurals, $expression );
    		}
    		return call_user_func( $this->_gettext_select_plural_form, $count );
    	}

    	/**
    	 * Returns the nplurals and plural forms expression from the Plural-Forms header.
    	 *
    	 * @since 2.8.0
    	 *
    	 * @param string $header
    	 * @return array{0: int, 1: string}
    	 */
    	public function nplurals_and_expression_from_header( $header ) {
    		if ( preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches ) ) {
    			$nplurals   = (int) $matches[1];
    			$expression = trim( $matches[2] );
    			return array( $nplurals, $expression );
    		} else {
    			return array( 2, 'n != 1' );
    		}
    	}

    	/**
    	 * Makes a function, which will return the right translation index, according to the
    	 * plural forms header.
    	 *
    	 * @since 2.8.0
    	 *
    	 * @param int    $nplurals
    	 * @param string $expression
    	 * @return callable
    	 */
    	public function make_plural_form_function( $nplurals, $expression ) {
    		try {
    			$handler = new Plural_Forms( rtrim( $expression, ';' ) );
    			return array( $handler, 'get' );
    		} catch ( Exception $e ) {
    			// Fall back to default plural-form function.
    			return $this->make_plural_form_function( 2, 'n != 1' );
    		}
    	}

    	/**
    	 * Adds parentheses to the inner parts of ternary operators in
    	 * plural expressions, because PHP evaluates ternary operators from left to right
    	 *
    	 * @since 2.8.0
    	 * @deprecated 6.5.0 Use the Plural_Forms class instead.
    	 *
    	 * @see Plural_Forms
    	 *
    	 * @param string $expression the expression without parentheses
    	 * @return string the expression with parentheses added
    	 */
    	public function parenthesize_plural_exression( $expression ) {
    		$expression .= ';';
    		$res         = '';
    		$depth       = 0;
    		for ( $i = 0; $i < strlen( $expression ); ++$i ) {
    			$char = $expression[ $i ];
    			switch ( $char ) {
    				case '?':
    					$res .= ' ? (';
    					++$depth;
    					break;
    				case ':':
    					$res .= ') : (';
    					break;
    				case ';':
    					$res  .= str_repeat( ')', $depth ) . ';';
    					$depth = 0;
    					break;
    				default:
    					$res .= $char;
    			}
    		}
    		return rtrim( $res, ';' );
    	}

    	/**
    	 * Prepare translation headers.
    	 *
    	 * @since 2.8.0
    	 *
    	 * @param string $translation
    	 * @return array<string, string> Translation headers
    	 */
    	public function make_headers( $translation ) {
    		$headers = array();
    		// Sometimes \n's are used instead of real new lines.
    		$translation = str_replace( '\n', "\n", $translation );
    		$lines       = explode( "\n", $translation );
    		foreach ( $lines as $line ) {
    			$parts = explode( ':', $line, 2 );
    			if ( ! isset( $parts[1] ) ) {
    				continue;
    			}
    			$headers[ trim( $parts[0] ) ] = trim( $parts[1] );
    		}
    		return $headers;
    	}

    	/**
    	 * Sets translation headers.
    	 *
    	 * @since 2.8.0
    	 *
    	 * @param string $header
    	 * @param string $value
    	 */
    	public function set_header( $header, $value ) {
    		parent::set_header( $header, $value );
    		if ( 'Plural-Forms' === $header ) {
    			list( $nplurals, $expression )     = $this->nplurals_and_expression_from_header( $this->get_header( 'Plural-Forms' ) );
    			$this->_nplurals                   = $nplurals;
    			$this->_gettext_select_plural_form = $this->make_plural_form_function( $nplurals, $expression );
    		}
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/pomo/translations.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/pomo/translations.php#L254)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/pomo/translations.php#L254-L408)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/gettext_translations/?output_format=md#related)󠁿

| Uses | Description | 
| [Translations](https://developer.wordpress.org/reference/classes/translations/)`wp-includes/pomo/translations.php` |

Translations class.

  |

| Used by | Description | 
| [PO](https://developer.wordpress.org/reference/classes/po/)`wp-includes/pomo/po.php` |  | 
| [MO](https://developer.wordpress.org/reference/classes/mo/)`wp-includes/pomo/mo.php` |  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/gettext_translations/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.8.0](https://developer.wordpress.org/reference/since/2.8.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fgettext_translations%2F)
before being able to contribute a note or feedback.