Zend Skeleton Application And G403Translator: Db Driven Translation

27 Aug 2012

I recently had need to do some translations with ZF2, the current state of ZF2 means that you can only load translations from files so I decided to take this oppertunity to experiment with creating my own module for doing it.

DASPRiD had already had a bash at creating something for this but unfortunately it didn’t work, I was able to take the code and work with it to produce this module.

There’s a generic Zend Skeleton Module which I cloned locally and created my own GitHub repo for storing the final product. It’s also published in Packagist so it’s dead easy to install.

So first we fork or clone the ZendSkeleton Application and then checkout a new branch to work on
we edited the composer.json to get the latest version of ZF2 and also to fetch our G403Translation module
composer.json file

Then we get composer to install our modules for us composer install command

After that we set up our db and user and imported our SQL that’s in the for the module sql Here we’re using custom table names, the default assumed within the module are zend_locale and zend_translate_message.

Then we set up the db connection within the code itself db connection config file
this file is within config/autoload/ and also another file for setting up the translator, as with all configs this can be done in a module config or in the config/autoload/ directory in this example inside the autload config translator config file

We then insert some data into the tables

locale table messages table

And we can see the change in our Zend Skeleton Application from Original ZSA to New ZSA

Tah dah!

To learn a bit about plurals read here.