Coder Social home page Coder Social logo

translator-nette's Introduction

Bckp\Translator

Downloads this Month Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version License

Simple and fast PHP translator

Usage

The best way to install bckp/translator-nette is using Composer:

$ composer require bckp/translator-nette

Configuration

In you nette config, add extension with temporary dir

extensions:
	translator: Bckp\Translator\Nette\Bridges\TranslatorExtension(%tempDir%/translator)

and set translator itself

translator:
	languages:
		- cs #Language codes
	path:
		- %appDir%/Locale # Where to search for language file
	resolver: true # Use resolver to get proper language?
	debugger: %debugMode%

Translator will find all files in path and make map to the DI. If debugger is on, it will on each request check, if any file is modified and rebuild all language file (only needed ones).

Naming convention

All language files should be in Neon format, and proper naming is: {module}.{langCode}.neon

Translation file format

Translation files are written in NEON format. Plural strings are in ARRAY, otherwise STRING.

welcome: 'Vítejte'
withArgs: 'Ahoj, já jsem %s, přeji krásné %s'
withArgsRev: 'Krásné %2$s, já jsem %1$s'
plural:
	zero: 'žádný člověk'
	one: 'jeden člověk'
	few: '%d lidé'
	other: '%d lidí'
next: 'This is next translation'

Translatings

Translator will auto-register into all presenters, that uses TranslatorAwareTrait, during onStartup part, and even auto-register into templates during onRender part.

<?php
class Presenter extends Nette\Application\UI\Presenter {
	use TranslatorAwarePresenter; // this comes with translator extension
}

Translating in Presenters

	$changes = $this->model->doSomeChanges();
	$this->flashMessage($this->translator->translate(['messages.flash.success', $changes], $changes));
	
	$form->addError($this->translator->translate('messages.error.form.empty'));

If you want do shortcut, you can define own translate method, like this

class Presenter extends Nette\Application\UI\Presenter {
	use TranslatorAwarePresenter; // this comes with translator extension

	public function translate($message, ...$params){
		$this->translator->translate($message, ...$params);
	}

	public function renderTest(){
		$message = $this->translate('messages.test');
	}
}

Translating in Templates

In template, you can use normal Latte way, so translating is super easy

	<div>{_'messages.test'}</div>

Every parameter you pass to the translate will be passed into sprintf in Bckp\Translator internally, so you can add order in neon translation format.

See (https://doc.nette.org/en/3.0/localization) for more informations about how to translate

See (https://github.com/bckp/translator-core) for more informations about Bckp\Translate-core

translator-nette's People

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.