Search code examples
phpinternationalizationzend-translate

Default locale using Zend Translate with gettext files


After hours of battling with gettext functions and admitting defeat, I downloaded the Zend Framework for use in my project. It finally worked with the gettext file that I have so far, but now I'm wondering how do I show the default text in the default locale?

Part of my config.php file (it's included in every file at the top):

<?php
// ...
define('LANGUAGE', (isset($_GET['lang']) && $_GET['lang'] === 'fr') ? 'fr-CA' : 'en-CA');
define('LOCALE', (isset($_GET['lang']) && $_GET['lang'] === 'fr') ? 'fr_CA' : 'en_CA');
// ...

$translator = new \Zend\I18n\Translator\Translator();
$translator->addTranslationFile('gettext', dirname(__FILE__) . '/locale/' . LOCALE . '.mo', 'messages', LOCALE);
$translator->setLocale(LOCALE);

The French translation works fine, but for English it is looking for the en_CA.mo file (I'm assuming from the addTranslationFile() method) which doesn't exist. How do I make it so that the keys are echoed if the English version is being displayed? It seems redundant to make an en_CA.mo file.


Solution

  • If you are allowing multiple translations for different languages to be translated then you'll need to create at the very least a blank en_CA.mo file. Otherwise, you'll find errors being displayed. Since the en_CA.mo file is blank, the translation keys will be displayed instead of the actual translation itself.