Skip to content

Latest commit

 

History

History
78 lines (65 loc) · 2.82 KB

readme.md

File metadata and controls

78 lines (65 loc) · 2.82 KB

Drago Translator

Simple and lightweight translator for Nette Framework, providing localization support using NEON translation files.

License: MIT PHP version Tests Coding Style CodeFactor Coverage Status

Technology

  • PHP 8.3 or higher
  • composer

Installation

composer require drago-ex/translator

Extension registration

Register the Drago\Localization\DI\TranslatorExtension in your Nette project by adding the following configuration to your neon file:

extensions:
	- Drago\Localization\DI\TranslatorExtension(translateDir: %appDir%/locale)

Use in the presenter

To use the translator in your presenter, add the TranslatorAdapter trait:

use Drago\Localization\TranslatorAdapter

Accessing the Current Language

You can access the currently set language using the following property:

$this->lang;

Get Translator Instance

To get the translator instance, use the getTranslator method:

$this->getTranslator();

Translation File Format

Translation files should be written in the NEON format. For example:

"Hello, world!": "Hello, world!"

Using Translations in Templates

You can translate strings directly in your Latte templates using the following syntax:

{_"Hello, world!"}

{* Using a filter for translation *}
{$var|translate}

Translating Forms

To use translations in forms, simply set the translator for the form:

$form->setTranslator($this->getTranslator());

Route Configuration for Language Switching

Set up your routes to support language prefixes. For example, you can define routes with language codes:

$router->addRoute('[<lang=en cs|en>/]<presenter>/<action>', 'Presenter:action');

Switching Languages in Templates

To switch between languages in your templates, you can use n:href to pass the selected language:

<a n:href="this, 'lang' => 'cs'">Czech</a>
<a n:href="this, 'lang' => 'en'">English</a>