Reputation: 368
I'am currently trying to get all translated messages in order to create a json and make it available for JavaScript translations. But I don't seem to find a way to get all translations. Any help is appreciated.
Upvotes: 0
Views: 477
Reputation: 51
It is quite simple actualy:
/**
* Return all the messages.
*
* @param string $textDomain
* @param null $locale
*
* @return mixed
*/
public function getAllMessages($textDomain = 'default', $locale = null)
{
$locale = $locale ?: $this->getLocale();
if (!isset($this->messages[$textDomain][$locale])) {
$this->loadMessages($textDomain, $locale);
}
return $this->messages[$textDomain][$locale];
}
This is a public Method of the Class \Zend\I18n\Translator\Translator
.
Probably added by the author of the anwser above. Made it to the framework...
Upvotes: 2
Reputation: 13558
The api of the translator does not help you with this. In short: it is not possible to do this via the Zend Framework 2 translator.
The reason is the translator supports various adapters, so you can also load translations via the database or an external api. The only way is to work with the file translations itself directly. So expose your .po file or .ini and let javascript parse that one.
So unfortunately, there is no such way. The only thing I can come up with is to hack around the translator:
use Zend\I18n\Translator\Translator;
class MyTranslator extends Translator
{
public function getAllMessages($textDomain = 'default', $locale = null)
{
$locale = $locale ?: $this->getLocale();
if (!isset($this->messages[$textDomain][$locale])) {
$this->loadMessages($textDomain, $locale);
}
return $this->messages[$textDomain][$locale];
}
}
Then use MyTranslator instead of Translator.
Upvotes: 1