futurerockstars/gettext-latte

Gettext translator for Latte template

dev-master 2023-07-02 15:31 UTC

This package is not auto-updated.

Last update: 2024-12-13 13:39:07 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

* 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