0

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.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156

1 Answers1

2

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.

Diemuzi
  • 3,507
  • 7
  • 36
  • 61
  • I was thinking that. Upon implementation though, the Translator class tried to parse the empty file: `Uncaught exception 'Zend\I18n\Exception\InvalidArgumentException' with message '/home/user/public_html/project/includes/locale/en_CA.mo is not a valid gettext file' in /home/user/public_html/project/libraries/Zend/I18n/Translator/Loader/Gettext.php:80` – rink.attendant.6 Jan 21 '13 at 22:19
  • 1
    The translation file has to have at least the header information. I recommend to try using PoEdit and create the file there. You could even try adding at least 1 translation key to be translated this way you know for certain that file has the required header information. Instead of just trying to create a blank and empty file. – Diemuzi Jan 22 '13 at 15:34
  • It worked, guess I just needed a blank translation file. Thanks. – rink.attendant.6 Jan 22 '13 at 22:50