https://github.com/drago-ex/translator
:earth_africa: Drago Extension for Nette Framework providing localization support with a custom Translator service and DI integration.
https://github.com/drago-ex/translator
nette translator
Last synced: 2 months ago
JSON representation
:earth_africa: Drago Extension for Nette Framework providing localization support with a custom Translator service and DI integration.
- Host: GitHub
- URL: https://github.com/drago-ex/translator
- Owner: drago-ex
- License: mit
- Created: 2015-10-16T07:15:22.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2025-01-28T07:35:07.000Z (5 months ago)
- Last Synced: 2025-04-07T14:18:19.468Z (2 months ago)
- Topics: nette, translator
- Language: PHP
- Homepage:
- Size: 179 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
## Drago Translator
Simple and lightweight translator for Nette Framework, providing localization support using NEON translation files.[](https://raw.githubusercontent.com/drago-ex/translator/master/license.md)
[](https://badge.fury.io/ph/drago-ex%2Ftranslator)
[](https://github.com/drago-ex/translator/actions/workflows/tests.yml)
[](https://github.com/drago-ex/translator/actions/workflows/coding-style.yml)
[](https://www.codefactor.io/repository/github/drago-ex/translator)
[](https://coveralls.io/github/drago-ex/translator?branch=master)## 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:
```neon
extensions:
- Drago\Localization\DI\TranslatorExtension(translateDir: %appDir%/locale)
```## Use in the presenter
To use the translator in your presenter, add the `TranslatorAdapter` trait:
```php
use Drago\Localization\TranslatorAdapter
```## Accessing the Current Language
You can access the currently set language using the following property:
```php
$this->lang;
```## Get Translator Instance
To get the translator instance, use the `getTranslator` method:
```php
$this->getTranslator();
```## Translation File Format
Translation files should be written in the NEON format. For example:
```neon
"Hello, world!": "Hello, world!"
```## Using Translations in Templates
You can translate strings directly in your Latte templates using the following syntax:
```latte
{_"Hello, world!"}{* Using a filter for translation *}
{$var|translate}
```## Translating Forms
To use translations in forms, simply set the translator for the form:
```php
$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:
```php
$router->addRoute('[/]/', 'Presenter:action');
```## Switching Languages in Templates
To switch between languages in your templates, you can use n:href to pass the selected language:
```latte
Czech
English
```