# filters/TranslateGettext.php

## \TranslateGettext
jump to top

Package: phing\filters

Replaces gettext("message id") and _("message id") with the translated string.

Gettext is great for creating multi-lingual sites, but in some cases (e.g. for performance reasons) you may wish to replace the gettext calls with the translations of the strings; that's what this task is for. Note that this is similar to ReplaceTokens, but both the find and the replace aspect is more complicated -- hence this is a separate, stand-alone filter.

<

p> Example:

Implements
  \ChainableReader
Parent(s)
  \BaseParamFilterReader < \BaseFilterReader < \FilterReader < \Reader
See
  \BaseFilterReader
Access
  public
Author
  Hans Lellelid
Copyright
  © 2003 seasonfive. All rights reserved
Version
  $Revision: 526 $ $Date: 2009-08-11 14:11:17 +0200 (Tue, 11 Aug 2009) $

### Constants

`DOMAIN_KEY = "domain"`

`DIR_KEY = "dir"`

`LOCALE_KEY = "locale"`

### Properties

`$_parameters= 'array()'`
inherited

The passed in parameter array.

Inherited from: \BaseParamFilterReader::$_parameters
Default value `array()`Details

Type
Inherited_from
  \BaseParamFilterReader::$_parameters

`$dir= ''`

The dir containing LC_MESSAGES

Details

Type

`$domain= ''messages''`

The domain to use

Default value`'messages'`Details

Type

`\Reader $in= ''`
inherited

Inherited from: \FilterReader::$in
Details

Type
  \Reader
Inherited_from
  \FilterReader::$in

`$initialized= 'false'`
inherited

Have the parameters passed been interpreted?

Inherited from: \BaseFilterReader::$initialized
Default value `false` Details

Type
Inherited_from
  \BaseFilterReader::$initialized

`$locale= ''`

The locale to use

Details

Type

`$project= 'null'`
inherited

The Phing project this filter is part of.

Inherited from: \BaseFilterReader::$project
Default value `null` Details

Type
Inherited_from
  \BaseFilterReader::$project

`$storedLocale= ''`

The system locale before it was changed for this filter.

Details

Type

### Methods

`__construct( \Reader $in = null ) : void`
inherited

Constructor used by Phing's introspection mechanism.

Inherited from: \BaseFilterReader::__construct()

The original filter reader is only used for chaining purposes, never for filtering purposes (and indeed it would be useless for filtering purposes, as it has no real data to filter). ChainedReaderHelper uses this placeholder instance to create a chain of real filters.

Parameters

Name | Type | Description
--- | --- | ---
$in | \Reader |

`_initialize( ) : void`

Parses the parameters if this filter is being used in "generic" mode.

`chain( \Reader $reader ) : \TranslateGettext`

Creates a new TranslateGettext filter using the passed in Reader for instantiation.

Parameters

Name | Type | Description
--- | --- | ---
$reader | \Reader | A Reader object providing the underlying stream. Must not be `null`.

Returns

Type | Description
--- | ---
\TranslateGettext | A new filter based on this configuration, but filtering the specified reader

`checkAttributes( ) : void`

Make sure that required attributes are set.

Throws

Exception | Description
--- | ---
\BuldException | - if any required attribs aren't set.

`close( ) : void`
inherited

Close stream.

Inherited from: \FilterReader::close()
Throws

Exception | Description
--- | ---
\IOException | if there is an error closing stream

`eof( ) : boolean`
inherited

Returns whether the end of file has been reached with input stream.

Inherited from: \BaseFilterReader::eof()
Returns

Type | Description
--- | ---
boolean |

`getDir( ) : \PhingFile`

Gets the root locale directory.

Returns

Type | Description
--- | ---
\PhingFile |

`getDomain( ) : string`

Get the current domain.

Returns

Type | Description
--- | ---
string |

`getInitialized( ) : boolean`
inherited

Returns the initialized status.

Inherited from: \BaseFilterReader::getInitialized()
Returns

Type | Description
--- | ---
boolean | whether or not the filter is initialized

`getLocale( ) : string`

Gets the locale to use for translation.

Returns

Type | Description
--- | ---
string |

`getParameters( ) : void`
inherited

Inherited from: \BaseParamFilterReader::getParameters()
`getProject( ) : object`
inherited

Returns the project this filter is part of.

Inherited from: \BaseFilterReader::getProject()
Returns

Type | Description
--- | ---
object | The project this filter is part of

`getResource( ) : string`
inherited

Returns the filename, url, etc. that is being read from.

Inherited from: \FilterReader::getResource()

This is critical for, e.g., ExpatParser's ability to know the filename that is throwing an ExpatParserException, etc.

Returns

Type | Description
--- | ---
string |

`initEnvironment( ) : void`

Initialize the gettext/locale environment.

This method will change some env vars and locale settings; the restoreEnvironment should put them all back :)

Throws

Exception | Description
--- | ---
\BuildException | - if locale cannot be set.

Details

See
  \restoreEnvironment()

`log( string $msg, int $level = Project::MSG_INFO ) : void`
inherited

Convenience method to support logging in filters.

Inherited from: \BaseFilterReader::log()
Parameters

Name | Type | Description
--- | --- | ---
$msg | string | Message to log.
$level | int | Priority level.

`mark( ) : void`
inherited

If supported, places a "marker" (like a bookmark) at current stream position.

Inherited from: \Reader::mark()

A subsequent call to reset() will move stream position back to last marker (if supported).

`markSupported( ) : boolean`
inherited

Whether marking is supported.

Inherited from: \Reader::markSupported()
Returns

Type | Description
--- | ---
boolean |

`read( \off $len = null ) : mixed`

Returns the filtered stream.

The original stream is first read in fully, and then translation is performed.

Parameters

Name | Type | Description
--- | --- | ---
$len | \off | Offset at which to start storing characters.

Returns

Type | Description
--- | ---
mixed | the filtered stream, or -1 if the end of the resulting stream has been reached.

Throws

Exception | Description
--- | ---
\IOException | - if the underlying stream throws an IOException during reading
\BuildException | - if the correct params are not supplied

`readLine( ) : \the`
inherited

Reads a line of text ending with '\n' (or until the end of the stream).

Inherited from: \BaseFilterReader::readLine()

The returned String retains the '\n'.

Returns

Type | Description
--- | ---
\the | line read, or <code>null</code> if the end of the stream has already been reached

Throws

Exception | Description
--- | ---
\IOException | if the underlying reader throws one during reading

`ready( ) : boolean`
inherited

Is stream ready for reading.

Inherited from: \Reader::ready()
Returns

Type | Description
--- | ---
boolean |

`reset( ) : void`
inherited

Reset the current position in stream to beginning or last mark (if supported).

Inherited from: \FilterReader::reset()
`restoreEnvironment( ) : void`

Restores environment settings and locale.

This does not restore any gettext-specific settings (e.g. textdomain()).

`setDir( \PhingFile $dir ) : void`

Sets the root locale directory.

Parameters

Name | Type | Description
--- | --- | ---
$dir | \PhingFile |

`setDomain( string $domain ) : void`

Set the text domain to use.

The text domain must correspond to the name of the compiled .mo files. E.g. "messages" ==> $dir/LC_MESSAGES/messages.mo "mydomain" ==> $dir/LC_MESSAGES/mydomain.mo

Parameters

Name | Type | Description
--- | --- | ---
$domain | string |

`setInitialized( boolean $initialized ) : void`
inherited

Sets the initialized status.

Inherited from: \BaseFilterReader::setInitialized()
Parameters

Name | Type | Description
--- | --- | ---
$initialized | boolean | Whether or not the filter is initialized.

`setLocale( string $locale ) : void`

Sets the locale to use for translation.

Note that for gettext() to work, you have to make sure this locale is specific enough for your system (e.g. some systems may allow an 'en' locale, but others will require 'en_US', etc.).

Parameters

Name | Type | Description
--- | --- | ---
$locale | string |

`setParameters( $parameters ) : void`
inherited

Inherited from: \BaseParamFilterReader::setParameters()
Parameters

Name | Type | Description
--- | --- | ---
$parameters |  |

`setProject( object $project ) : void`
inherited

Sets the project to work with.

Inherited from: \BaseFilterReader::setProject()
Parameters

Name | Type | Description
--- | --- | ---
$project | object | The project this filter is part of. Should not be `null`.

`setReader( $in ) : void`
inherited

Inherited from: \FilterReader::setReader()
Parameters

Name | Type | Description
--- | --- | ---
$in |  |

`skip( int $n ) : void`
inherited

Move stream position relative to current pos.

Inherited from: \FilterReader::skip()
Parameters

Name | Type | Description
--- | --- | ---
$n | int |

`xlateStringCallback( array $matches ) : string`

Performs gettext translation of msgid and returns translated text.

This function simply wraps gettext() call, but provides ability to log string replacements. (alternative would be using preg_replace with /e which would probably be faster, but no ability to debug/log.)

Parameters

Name | Type | Description
--- | --- | ---
$matches | array | Array of matches; we're interested in $matches[2].

Returns

Type | Description
--- | ---
string | Translated text

Documentation was generated by DocBlox 0.15.1.