0

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
Community
  • 1
  • 1
sdolgy
  • 6,963
  • 3
  • 41
  • 61

2 Answers2

0

If you look at the translate adapter classes you won't find anything that looks just remotely like they support regions. Actually they do more to strip the regions. I'm afraid you have to write (extend) your own adapter to support regions.

Zend classes are independent objects that sometimes work together almost like magic but they are still separate entities. Although we have great support with regions in Zend_Locale does not mean it is also implemented in translate. It might be in the future, though.

Adrian World
  • 3,118
  • 2
  • 16
  • 25
  • i think this answer would be more than sufficient for my other question... ! http://stackoverflow.com/questions/6722948/why-doesnt-the-zend-locale-honor-abbreviated-formats-like-zh-hk-or-zh-cn .. – sdolgy Aug 01 '11 at 16:30
  • @sdolgy I think I got an even better answer for that – Adrian World Aug 01 '11 at 16:48
0

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 ...

sdolgy
  • 6,963
  • 3
  • 41
  • 61