Multi-lingual views in Zend Framework 1

Ever felt like this?

Build us a multilingual website

If so, do read on. Because it has become easier.

Do you want to quickly organize your multi-language website in Zend Framework, such that you can easily add languages without breaking the website? And from there, progressively translate pieces of your website? Imagine a website where you have a handful of static content – think of about pages, policy pages and disclaimer pages – and you want to easily add translated pages to your system, without having to go through programming the logic of selecting the right view for these pages.

Look no further, for the ZF1E library offers exactly that solution. Using the built-in MultiLanguage resource, it will determine which language part of your website your visitor is accessing, and grab the correct view for that language.

The multi-language aware controller class

I have recently added a base controller that is multi-language aware. This controller will replace the default view with a language-specific view. This is how you would use such a controller, with as an example the about action being multi-language aware:

class IndexController extends ZFE_Controller_MultiLanguage
{
    protected $i18nActions = array(
        'about'
    );
 
    public function aboutAction()
    {
    }
}

You extend your controller from ZFE_Controller_MultiLanguage and add the multi-lingual actions in the $i18nActions array, just like you’d add AJAX-actions in the $ajaxable array for the AJAX context switcher. Under the hood, the ZFE_Controller_MultiLanguage base controller checks in its postDispatch routine whether to rewrite the default view script into a language-specific view script. This language-specific view script should be put in your usual view script directory structure, but the file-name contains the language code. Imagine the website supports English, Dutch and German, and we have a Dutch visitor. The language code for Dutch would be nl. By default, Zend would look for an about.phtml file, but the ZFE_Controller_MultiLanguage first checks whether the language-specific about-nl.phtml exists. If this does not exist, and the default fallback language has been set to English, it will look for about-en.phtml. If that still does not exist, it will look for the usual about.phtml, and finally throw the usual exception when that view script file is not found.

This allows you to apply the library to your existing Zend Framework 1 project without breaking it. You can progressively move the files into different language-specific files, and progressively translate them.

The Partial view helper

What people also use to include a bunch of text, is the Partial view helper. The ZF1E library overloads the default view helper to become multi-language aware, so that you can keep doing:

echo $this->partial('extra.phtml', $this);

without having to worry about the language-specific versions. ZF1E’s Partial helper does that by checking whether the application has been set up for multiple languages using the MultiLanguage resource, and rewrites the file-name into extra-en.phtml for the English version, and extra-nl.phtml for the Dutch version. It does all the checks whether the files exist, and the fallback operations from given language to default language, and from there to just the original file extra.phtml, so you do not have to worry about these.

Keep calm and be multilingual

So what are you waiting for? Let’s quickly add languages to your Zend projects! And if there is something, anything, that you think should be in any multi-lingual application, do let me know in the comments below!

GitHub: Zend Framework 1 Extension library

What are your thoughts?