Documentation Framecore

Documentation on working with FrameCore. Setting up and using classes. Development of applications based on this platform

Translate

Class for implementing language translation in your application


Class Translate has method __construct

__construct($config = false, $moduleName = false, $collection = [], $useDirectly = false, $isManager = false)

If want create multi-language module:

first, you need to create new file with name like you module param and code language. For example: we have new module Foo. In folder we will create new file translations /messages/Foo_EN.php

You need to add an array with a key and translation, which will be used in your module later on.

$messages = [
   'key'=>'translation',
   'key-in-your-translation'=>'My simple Translation',
]

Then, in your new module

namespace Modules;
use Spotsystem\Translate;

and need initialize you new file in your module in __construct method

$this->view->translate = new Translate(false, 'Foo', []);

Example Initialization

namespace Modules;
use Spotsystem\Translate;

class Foo extends \Controllers_Base
{
   ...
   __construct($parent = false)
   {
      if ($parent) 
      $this->view->translate = new Translate(
         false, 
         'Foo', 
         (isset($this->view->translate)) ? $this->view->translate : [], 
         ($this->di->dispatcher->getControllerName() === 'admin') ? true : false
      );
   }
   ...
}

After that, we can use in our methods translation for strings by key

public function indexAction()
{
   $this->view->setTitle($this->view->translate->_('key-in-your-translation'));
}

and we can see in view

<title>My Simple Translation</title>

If the system or module supports more languages than you have translations for (in case the user activates a language other than English), the system will automatically set English as the language.





Return Back