Skip to content

🌍 Lightweight translator for Nette Framework using NEON files, supporting global and module-specific translations.

License

Notifications You must be signed in to change notification settings

drago-ex/translator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Drago Translator

Lightweight translator for Nette Framework using NEON files, supporting global and module-specific translations.

License: MIT PHP version Tests Coding Style CodeFactor Coverage Status

Requirements

  • PHP >= 8.3
  • Nette Framework
  • Composer

Installation

composer require drago-ex/translator

Extension registration

Register the DI extension in your NEON configuration. You must provide a base directory for translation files.

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

Optional configuration

translator:
	moduleLocaleDir: %appDir%/modules/Blog/locale

The translator supports multiple translation directories.

  • The base directory is always loaded first
  • The optional module directory is loaded second
  • If translation keys collide, later directories override earlier ones

Translation files must be named by language code:

cs.neon
en.neon

Translation File Format

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

Using Translator in Presenters

Add the TranslatorAdapter trait to your presenter:

use Drago\Localization\TranslatorAdapter;

The trait provides:

  • persistent language parameter ($lang)
  • automatic translator initialization
  • template integration

Accessing the Current Language

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

$this->lang;

Getting Translator Instance

To get the initialized translator for the current language:

$this->getTranslator()

Using Translations in Templates

The translator is automatically registered in templates. Example usage in Latte:

{_"Hello, world!"}
{$label|translate}

Using Translator in Forms

To enable translations in forms, set the translator explicitly:

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

Routing for Language Switching

To support language prefixes, configure your routes accordingly:

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

Switching Languages in Templates

You can switch languages by passing the lang parameter:

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

Notes

  • Translator loads translations lazily on first use
  • Translations are loaded once per request
  • Missing keys return the original message

About

🌍 Lightweight translator for Nette Framework using NEON files, supporting global and module-specific translations.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages