Skip Navigation LinksHome > Categories > Code from a Category

Zend Framework and Translation



User Name: serfcompany
Name: Serf
Contact Me: www.datawebcoder.com
Home Page: www.datawebcoder.com
php,mysql,javascript,html,css. Preferable working with Zend Framework. Good know javascript. I worked with various, javascript frameworks such as(jquery, YUI3, extjs, sencha touch). [More]
Viewed Times: 3389
Add Date: 06/15/2012
Multilingual websites are becoming more popular, I would like to show two possible ways to transfer blocks of static text in your application to the Zend Framework.Zend Framework already provides us with several packages to simplify life, for example, Zend_Locale and Zend_Translate, but how to bring these components together?

Creating a Zend Framework application


First of all, we need an application Zend Framework. You can also use your existing application or create a new right in the Zend Studio for Eclipse:


If you do not know how to install the Zend Framework, please refer to the appropriate management of Zend Framework Quickstart.


Zend_Locale and Zend_Translate.


As mentioned earlier, we need nature, and Zend_Locale Zend_Translate. I initialize the two objects in the class Initializer, which was created by a master project Zend Framework:



Method Initializer :: initLocale () method is called Initializer :: _routeStartup (). Of course, you can implement this functionality in your bootstrap. I chose a very simple way to initialize the object Zend_Locale for this example: I set the variable $ localeValue directly in the method. Of course, this is not recommended! Perhaps you can take the place of the user session or Zend_Locale, at your discretion. For this we use Zend_Translate and Array Adapter. This means that we need to create a php-file, which will return an array of translation.


If you have an object Zend_Locale, you can complete an application by adding the key "Zend_Locale" in Zend_Registry. Maybe there already are some components of ZF. In the next step, we will create the necessary components for translation.


This is the fastest way to create a mechanism for the transfer. File en.inc.php as follows:



The key is a key transfer, which is used in this template, type, and its value is itself a translation.


Let us return to our method Initializer :: initLocale (), we have created an object named Zend_Translate adapter, file transfers and accommodation, setting this object to an analog object Zend_Locale, which is in Zend_Registry. For example, Zend_Form will now be used to transfer information automatically labels Zend_Form_Element.


It's almost all we have to prepare before we can use translations in our view script.


Zend_View_Helper_Translate


Since we put the object in the Zend_Translate registry, you can now use the view script to Translate View Helper. Let's look at an example phtml:



As you can see, each block of static text is used as a parameter of the method translate, object Zend_View. The essence of the object will be created Zend_View_Helper_Translate view automatically. It will use an object Zend_Translate of registry, to translate the parameter string (the string must be a key array of translation) and return the translated string that will be displayed in the view script. Very simple, is not it?


Method


Perhaps you are familiar: "Too many php-code in the template form." Then we will have the same opinion. What do you think about this template:



I turned on each block of static text in the tag . In my opinion it is easier to read and this method has more in common with the tags HTML (maybe someone will tell me how to add your own html-tags in HTML validator in Eclipse - without the implementation of the new validator, so we can get rid of messages about incorrect tags) . I know that these tags are not valid, but we filter them later - with the object Zend_Filter. The object Zend_View can be installed to filter the output Zend_Filter content. Hence, we should implement a new class, for example, Zx_View_Filter_Translate. Following the convention of naming the Zend Framework, you must create a library / Zx / View / Filter / Translate.php and realize there is a class (Zx is my personal ZendFramework-Demo-Extension-Prefix, you can safely do either).


Let's look at the code (if you are also using Zend_Loader, do not necessarily include any of the files):



We need to implement only one method: Zx_View_Filter_Translate :: filter (). The parameter $ value contains the full text of a Kind. In each cycle, we check the location of the text, beginning with and ends with . Then we extract the text between the tags and transfer it by using a Zend_Translate (taken from the registry). In the end, we write the translated string in the same place, but without the tags .


For performance reasons, I did not use regular expressions. I agree with the regular expression code would be cleaner, but the strings are faster. This is my first solution, accepted advice to improve it.


Now we have a filter that is not used. Hence, we need to tell the form where to find it. My option: use the Initializer :: initView (). To get a view at this point the application, we need a little trick:



Create a new object Zend_View, add a new path to the filter (the way that we used in our filter Translate), and the new prefix filter ("Zx_View_Filter", in the other case, by default, "Zend_View_Filter", which is used to create the class name). In addition, we have to set the name of the filter, which we would like to use. In the end, we must set the view object in the static action helper "ViewRenderer". Now is the time to check how it works.


Performance


Since I installed a demo application on Zend Server 4.0 beta (VMware, Ubuntu JeOS 8.04), I was not hard to test for performance with Zend Studio for Eclipse, as well as the integrated Profiler. A few words about my "test environment": I implemented a class with two actions TranslateController helperAction and filterAction. Both actions are not doing anything, except to render Vida ViewHelper or filter. I also implemented a method postDispatch (), which is repeated 150 times, and renders a script called actions. I think it will be easier to understand if you look at the code:



Calling both queries (/ translate / helper and / translate / filter) in Profiler gives us the following results:



/translate/helper


/translate/filter


As you can see, the method postDispatch () requires almost 20% less total time to call the filter, the call to the helper. I tried several times and got the same result. Of course, this is not scientific measurements, but I think it is worthy of attention.


Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Details**:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
 
    
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.