Search code examples
phpzend-frameworkzend-translatezend-locale

Zend_Translate problems between two languages


Hot off the heels of my other question:

These are the two languages I'm wanting to provide translations for using the Zend framework. When a user decides that they don't want to use English as their primary language, they are offered the opportunity to select an alternate language:

  • zh_Hans
  • zh_Hant

When I use the preceeding with the following code:

 require_once 'Zend/Locale.php';
 $locale = new Zend_Locale();
 $locale->setLocale('zh_Hans'); // for example

The actual language that is available to me is zh and not zh_Hans or zh_CN

So now, when it comes to using Zend for translation

 require_once 'Zend/Translate.php';
 $translate = new Zend_Translate(array('adapter' => 'array',
                  'content' => 'translations/zh_Hant.trans',
                  'locale' => $locale->getLanguage()
                  ));

It fails ... because zh doesn't exist as a language file. this is expected as I am telling $translate that the $locale is the language ...

  1. So I try the following:

    'locale' => $locale->getLanguage() . '_' . $locale->getRegion()

This also fails as $locale->getRegion() is empty ...

Question:

  • What is the proper way to set the language of a remote user's locale using the Zend framework so that language _ region is available for Zend_Translate?
    -- referencing my other question, zh_HK and zh_CN is incorrect. zh_Hans / zh_Hant is

Solution

  • My less than elegant hack:

      $supported_langs = array(
        'en_US' => 'en_US',
        'en_GB' => 'en_GB',
        'zh_Hans' => 'zh_CN',
        'zh_Hant' => 'zh_HK',
        'es' = > 'es'
      );
    
      require_once 'Zend/Translate.php';
      $targetLanguage = $locale->getLanguage();
      if ($locale->getRegion() != null) { 
         $targetLanguage = $locale->getLanguage() . '_' . $locale->getRegion();
      }
      $contentFile = dirname(__FILE__) . '/../translations/' . $locale->getLanguage() . '/general-' . $targetLanguage . '.trans';
      $translation_language = array_search($targetLanguage, $supported_langs);
    
      $translate = new Zend_Translate(
         array(
            'adapter' => 'array',
            'content' => $contentFile,
            'locale'  => $translation_language
            )
      );
    

    My hope was that Zend_Locale and Zend_Translate would work seamlessly together. Maybe someone has a cleaner idea ...