We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to use multi-lingual in the controller (and models)?

According to the Phalcon manual, we can use $t->_("something") to display the translated string

But how to use it in a controller? like the error message $this->flashSession->error("something");, then in the views, we can display the translated string by {{ flashSession.output() }} automaticlly

edited Oct '15
public function indexAction()
{
    $t = $this->getTranslation();
    ...
    $this->view->t = $t;
    ...
    $this->flashSession->error($t->_('something'))
    ...
}

Something like this



31.3k

Thanks! I'll try it

public function indexAction()
{
   $t = $this->getTranslation();
   ...
   $this->view->t = $t;
   ...
   $this->flashSession->error($t->_('something'))
  ...
}

Something like this



31.3k
edited Nov '15

May I ask an another help?

how to use the translater in the model?

for example the Username and Email validator:

public function validation()
    {
        $this->validate(new UniquenessValidator(array(
            'field' => 'name',
            'message' => 'Sorry! The username is in use!' //It should replaced with multi-lingual support
        )));
        $this->validate(new EmailValidator(array(
            'field' => 'email'
        )));
        $this->validate(new UniquenessValidator(array(
            'field' => 'email',
            'message' => 'Sorry! The Email is in use!' //It should replaced with multi-lingual support
        )));
        if ($this->validationHasFailed() == true) {
            return false;
        }
    }
public function indexAction()
{
   $t = $this->getTranslation();
   ...
   $this->view->t = $t;
   ...
   $this->flashSession->error($t->_('something'))
  ...
}

Something like this



3.5k
Accepted
answer
edited Nov '15

May I ask an another help?

Sure! )))

how to use the translater in the model?

You may use as in a controller

In my projects, I use an another method. I have created a global function that takes string for the translte, an array for replace, and the path to the translation file (optional). It's more easy and faster - one file parsed once and save to global variable. May be it's not OOP style but it's useful for multimodule application too.

<?php
$g_translation_files = [];
function __($str, $placeholders = null, $file = null)
{
    global $g_translation_files;
    // CCheck is $file in $g_translation_files - this means that file was loaded 
    // else load file with phalcon adapter to $g_translation_files[$file]

    // If $file is not null call function _ $g_translation_files[$file] item and return
    // Example: 
    return $g_translation_files[$file]->_($str, $placeholders);

    // If $file is null call function _ for all $g_translation_files items
    // Some like below
    foreach ($g_translation_files as $translation_file)
    {
        $tmp_str = $translation_file->_($str, $placeholders)
        if ($tmp_str != $str)
        {
            $str = $tmp_str;
            break;
        }
    }
    return $str;
}


31.3k

Thanks! That's a good idea!

May I ask an another help?

Sure! )))

how to use the translater in the model?

You may use as in a controller

In my projects, I use an another method. I have created a global function that takes string for the translte, an array for replace, and the path to the translation file (optional). It's more easy and faster - one file parsed once and save to global variable. May be it's not OOP style but it's useful for multimodule application too.

<?php
$g_translation_files = [];
function __($str, $placeholders = null, $file = null)
{
  global $g_translation_files;
  // CCheck is $file in $g_translation_files - this means that file was loaded 
  // else load file with phalcon adapter to $g_translation_files[$file]

  // If $file is not null call function _ $g_translation_files[$file] item and return
  // Example: 
  return $g_translation_files[$file]->_($str, $placeholders);

  // If $file is null call function _ for all $g_translation_files items
  // Some like below
  foreach ($g_translation_files as $translation_file)
  {
      $tmp_str = $translation_file->_($str, $placeholders)
      if ($tmp_str != $str)
      {
          $str = $tmp_str;
          break;
      }
  }
  return $str;
}