futurerockstars / gettext-latte
Gettext translator for Latte template
Requires
- php: >=7.4.0 <8.0.0
- h4kuna/latte-php-tokenizer: ^1.0.0
- latte/latte: ^2.7.4
- nette/application: ^3.1.11
- nette/caching: ^3.1.4
- nette/di: ^3.0.16
- nette/finder: ^2.6.0|^3.0.0
- nette/http: ^3.2.2
- nette/utils: ^3.2.9|^4.0.0
Requires (Dev)
- nette/bootstrap: ^3.1.4
- nette/tester: ^2.4.3
- orisai/coding-standard: ^3.7.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-deprecation-rules: ^1.1.0
- phpstan/phpstan-nette: ^1.2.0
- phpstan/phpstan-strict-rules: ^1.5.0
- staabm/annotate-pull-request-from-checkstyle: ^1.8.0
- tracy/tracy: ^2.6.8
This package is not auto-updated.
Last update: 2024-11-15 13:16:56 UTC
README
GettextLatte
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