开发者

Zend_Translate problems between two languages

开发者 https://www.devze.com 2023-03-25 13:42 出处:网络
Hot off the heels of my other question: why doesn't the Zend_Locale honor abbreviated formats like zh_HK or zh_CN

Hot off the heels of my other question:

  • why doesn't the Zend_Locale honor abbreviated formats like zh_HK or zh_CN

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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号