I have the whole MVC-Model set up and use HTML views as templates. But I have german strings in there that I would like to translate to other languages at some point.
What is the best way to do this? I know I have to use Zend_Translate, but do I have to implement a single call to a translate function for every word that I have in my view templates?
Zend Framework is a collection of professional PHP packages with more than 570 million installations. It can be used to develop web applications and services using PHP 5.6+, and provides 100% object-oriented code using a broad spectrum of language features.
Zend\Mvc is a brand new MVC implementation designed from the ground up for Zend Framework 2, focusing on performance and flexibility. The MVC layer is built on top of the following components: Zend\ServiceManager - Zend Framework provides a set of default service definitions set up at Zend\Mvc\Service.
Zend Framework is a collection of 60+ packages for professional PHP development. Each package is available on GitHub and can be installed via Composer.
When it comes to PHP frameworks, Zend is counted among the best. Zend Framework offers lots of benefits for creating feature-rich and dynamic web solutions. MVC features and a strong component library have made Zend a popular PHP framework for creating a myriad of web solutions.
First of all, I'd suggest to use complete phrases as the basis for your translation. With words you always have the problem that languages are not consistent when it comes to sentence structure.
Then you have to choose one of the available Zend_Transalate adapters: Array, Csv, Gettext, Ini, Tbx, Tmx, Qt, Xliff or XmlTm. Most of them are adapters to industry standards for storing translation information, so it probably would suffice if you chose Array, Csv or Ini for the beginning and for the ease of use. Please see 49.2.1. How to decide which translation adapter to use in the Zend Framework manual.
// setup your translation
$translate = new Zend_Translate('csv', '/my/path/source-de.csv', 'de');
$translate->addTranslation('/my/path/source-en.csv', 'en');
// add the translation adapter to the registry
Zend_Registry::set('Zend_Translate', $translate);
As there is a Zend_View_Helper_Translate
that access the standardized Zend_Registry
entry Zend_Translate
as the default translation source, you can use the following in your views:
[...]
<title><?php echo this->translate('Title'); ?></title>
[...]
<p>You can also do <?php echo $this->translate('Hello %1$s', $this->userName); ?></p>
[...]
Please note that this only is a short introduction into Zend_Translate
and by no means a complete presentation of the functionality provided by this component. For example there is a lot to be said about determining the locale the translation adapter will use.
I'd suggest you read the following in the Zend Framework manual, because localization can be a complex issue and Zend_Translate
can not be described entirely in here:
No, you don't have to translate every single word. The idea of translation in this sense is more a of a message translation idea.
In my current project we do two types of translations. When it's just going to be a short text, we type that directly into the source code for purposes of readability.
I.E.
<?=$this->translate('Add');?>
<?=$this->translate('Delete');?>
<?=$this->translate('Are you sure you want to delete %1$s?', $thing);?>
But if the message is much longer, we usually follow a standard to mark it for the translation system:
<?=$this->translate('controller-action_form-information');?>
The idea being that you can then replace that with a very long text in your translation tool and it keeps the view script tidy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With