Skip to content

Assumeru/php-gettext

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

php-gettext

This project is an implementation of gettext in PHP 5.5.

Basic usage

Before starting, make sure every file is properly included, preferably using an auto loader.

  1. Construct a new MO object. The constructor takes a string argument containing a .mo file file_get_contents can be used to supply such a file.
$mo = new \gettext\MO(file_get_contents('my_file.mo'));
  1. Translate a string. This is equivalent to PHP's gettext function.
$translatedString = $mo->translate('My string.');
  1. Translate a plural string. Equivalent to PHP's ngettext function. (Note that the translate method does not automatically substitute %d. Use something like sprintf.)
$numberOfStrings = 2;
$pluralString = $mo->translate('One string.', '%d strings.', $numberOfStrings);

Domains

The translate method accepts a fourth argument to set the translation's domain.

The equivalent to dgettext is therefore:

$domain = 'my_domain';
$translatedString = $mo->translate('My string.', null, null, $domain);

While the equivalent to dngettext is:

$pluralString = $mo->translate('One string.', '%d strings.', $numberOfStrings, $domain);

Multiple files

It's possible to merge MO objects to support spreading translations over multiple files. To merge two MOs simply call the merge method, this will merge the second MO into the first.

$mo1 = new \gettext\MO(file_get_contents('my_file_1.mo'));
$mo2 = new \gettext\MO(file_get_contents('my_file_2.mo'));
$mo1->merge($mo2);

About

php implementation of gettext

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages