Reputation: 261
i am developing web app with ZF2. It is a multilingual website. I would like to use ZF2 Translator for translation. Because, admin can modify the language variables. So, i planned to use the language folder files. I will update the language files while the admin modifying the language variables. Can anyone suggest ideas for this implementation? Thanks.
Upvotes: 0
Views: 1753
Reputation: 13558
Translating based on user input can get a little messy. There is no easy way of doing that right now, as the currently supported formats are all file formats: php files with arrays, a gettext .po file or Tmx/Xliff files. For the easiest access, you would likely store the translations in a database.
You therefore need to write your own loader, loading the translations from the database. An alternative is you will write the translations in a php array and export that to a file, but I would not recommend that. For the database loader, you must implement the RemoteLoaderInterface
. In very simple terms:
use My\DbTable;
use Zend\I18n\Translator\Loader\RemoteLoaderInterface;
Zend\I18n\Translator\TextDoamin;
class DbLoader implements RemoteLoaderInterface
{
protected $table;
public function __construct(DbTable $table)
{
$this->table = $table;
}
public function load($locale, $textDomain)
{
$data = $this->table->loadFromLocale($locale);
// process $data into $messages
// $messages is array(key=>value) with key translation key
$domain = new TextDomain($messages);
return $domain;
}
}
The db table loads the messages in the locale you specify. If you use text domains, pass that on too. Then you need to configure your Translator
such it uses this loader too to load the message. Everything else should be fine then. In your views, you can create translations like this:
<?php echo $this->translate('my original text'); ?>
Upvotes: 1
Reputation: 3527
The SkeletonApp comes with Translation support built-in. However further details on how to use it can also be found in the manual -> http://zf2.readthedocs.org/en/latest/modules/zend.i18n.translating.html
I personally keep all my language files in a folder called /language
In your views you could also simply call
$this->translate('text_you_want_to_translate');
Upvotes: 0