开发者

How to do Zend_Locale with db and fallback?

So, I've google'd and found a way to make my application work with a db adapter for localization (which works great btw), now the next rising problem is fallback. Here's the scenario:

  • Application has translations for English, German, French and Korean.
  • English is at 100%, the others don't.
  • If a string isn't localized in German, French or Korean needs to fallback to English
  • The adapter uses and ID not a localized string

Example:

<?php

    // This is init'd in the bootstrap
    try {
        $locale = new Zend_Locale(Zend_Locale::BROWSER); /* Q1!!! */
    } catch (Zend_Locale_Exception $e) {
        $locale = new Zend_Locale('en_US');
    }

    $translate = new Zend_Translate(
        'XYZ_Translate_Adapter_Db', // My adapter
        $data,                      // Populated variable
        $locale->getLanguage(),     // Returns en, de, fr or ko
        array()                     // Options to开发者_如何学Go pass to the adapter, none.
    );

    // This is called from the view
    echo $translate->_(123); /* Q2!!! */

?>

The echo'd output will be the corresponding text for the ID 123. Now on the Q1!!! comment, how can I validate just the en/de/fr/ko locales? And for the Q2!!! comment, how can I fallback to English in case of ID 123 not found for the other languages.

Also I'll be handling dates and currencies, so I would like to validate for fr_FR, de_DE, ko_KO and not variants like fr_CA or de_AT. So for the first part I need to validate if the locales are from the country/langs that I need or if they are at least a valid lang (and force them to the proper country); and for the last part I need to validate that if the translation isn't on the db fall back to English.


Zend_Translate supports fall back functionality. Here in the example that fallbacks from de to en

$translate = new Zend_Translate(
array(
    'adapter' => Zend_Translate::AN_XLIFF,
    'content' => APPLICATION_PATH . '/../data/locales/',
    'locale'  => $locale->getLanguage(),
    'scan'  => Zend_Translate::LOCALE_FILENAME,
    'useId'   => true,
    'route'   => array('de' => 'en')
    )
);

Note "route" parameter that will allow you to set a fallback rules. You don't need to bother about region code as its processed automatically. Read more in the Zend_Translate routing section http://framework.zend.com/manual/en/zend.translate.additional.html#zend.translate.additional.rerouting

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜