h4kuna/gettext-latte

This package is abandoned and no longer maintained. The author suggests using the symfony/translation package instead.

Gettext translator for Latte template

v1.3.4 2019-09-06 11:26 UTC

README

GettextLatte

Build Status Latest stable

is localization addon for Nette framework 2.3, which natively supports gettext.

Conditions for start-up

  • gettext extension enabled
  • language installed on server, you can check by using command $ locale -a
  • your application written in UTF-8 encoding

In the repository is directory locale containing prepared directory structure for your project. In folder example are files, whose help you setup this translator. You can write your application in your native language, here is example in english, but it may be czech, slovak, german language...

Start-up

Clone this repository or use composer.

composer require h4kuna/gettext-latte

Router keep language.

/**
 * @return Nette\Application\IRouter
 */
public static function createRouter(\h4kuna\Gettext\GettextSetup $translator) {
    $router = new RouteList();
    $router[] = new Route('index.php', 'Homepage:default', Route::ONE_WAY);
    $router[] = new Route('[<lang ' . $translator->routerAccept() . '>/]<presenter>/<action>/[<id>/]', array(
        'presenter' => 'Homepage',
        'action' => 'default',
        'lang' => $translator->getDefault()
    ));

    return $router;
}

examples/config.neon

On Mac encoding is represented as 'en_US.UTF-8' everytime dojo format 'en_US.utf8'.

extensions:
    gettextLatteExtension: h4kuna\Gettext\DI\GettextLatteExtension

gettextLatteExtension:
    langs:
        cs: cs_CZ.utf8
        sk: sk_SK.utf8
        en: en_US.utf8

Install new macro to latte engine with alias for native gettext function {_'' /, .../} and {_n'', '', '' /, .../} new is {_d'catalog', 'message'} and plural {_dn'catalog', 'message' /, .../}.

Run service and support automatic detection of language

Load language as soon as possible.

<?php
use Nette\Application\UI\Presenter;

abstract class BasePresenter extends Presenter {

    /** @persistent */
    public $lang;

    /** @var \h4kuna\Gettext\GettextSetup */
    protected $translator;

    /**
     * Inject translator
     * @param \h4kuna\Gettext\GettextSetup
     */
    public function injectTranslator(\h4kuna\Gettext\GettextSetup $translator) {
        $this->translator = $translator;
    }

    protected function startup() {
        parent::startup();
        $this->lang = $this->translator->setLanguage($this->lang);
    }
    
    /**
     * Or PHP 5.4+ ****************************************************************
     * *************************************************************************
     */
    use \h4kuna\Gettext\InjectTranslator;
}

After install translator please empty temp directory, otherwise you may get "Call to undefined method Nette\Templating\FileTemplate::translate()".

How to write texts in PHP files

Outside the template using gettext.

<?php
echo gettext('Hi'); //or alias _
echo _('Hi');
echo ngettext('dog', 'dogs', 2);

echo sprintf(_('%s possible %s %s'), 'another', 'optional', 'params');

In template using macros. Number of parameters isn't limited. Function sprintf is automatically added. Look at examples/example.latte

macro in templatetranslate to php
{_'Hi'}echo gettext('Hi');
{_'Today is %s', $date}echo sprintf(gettext('Today is %s'), $date);
In the previous version, the inflection was written like this.
{_n'dog', 'dogs', $count}echo ngettext('dog', 'dogs', $count);
Now it's off and is written following way.* (It is possible to turn on with third parameter in constructor.)
{_n'dog', $count}echo ngettext('dog', 'dog', $count);
{_n'%s dog has email %s', $count, $email}echo sprintf(ngettext('%s dog has email %s', '%s dog has email %s', $count), $count, $email);
If you need to decline to negative, variable must contain abs.
{_n'today is %s degree temperature', $absTemperature}echo sprintf(ngettext('today is %s degree temperature', 'today is %s degree temperature', abs($absTemperature)), $absTemperature);
If you have many variables and replace the first variable not governed translation variable must contain plural.
{_n'Name: %s, age: %s year old', $name, $pluralYear}echo sprintf(ngettext('Name: %s, age: %s year old', 'Name: %s, age: %s year old', $pluralYear), $name, $pluralYear);

* It was changed, because inflection is defined in catalog everytime, for language whose has more than 2 level inflection.

Let's start translate

Download PoEdit. Before each Poedit run you must have all templates compiled to php in temp directory, for this is use script like examples/latte-compiler.

Open the .po file. Setup directory search - by default it is temp/cache/latte and app and click "update catalog", after update catalog you don't need restart apache.

If you write application in language with three inflection levels instead of two, for example czech, you must have catalog with translation czech to czech but only for plural.

Downloadable catalog

For your translators you provide catalog for download.

$this->translator->download('cs'); //Offers catalog download

Run Tests

Composer must be instaled. And than you execute only script.

./tests/run-tests.sh