如何使用 db 和后备来执行 Zend_Locale ?

发布于 2024-10-11 12:15:29 字数 1214 浏览 2 评论 0原文

因此,我在谷歌上搜索并找到了一种使我的应用程序与数据库适配器一起使用以进行本地化的方法(顺便说一句,这非常有效),现在下一个出现的问题是后备。场景如下:

  • 应用程序有英语、德语、法语和韩语翻译。
  • 英语是100%,其他的不是。
  • 如果字符串未本地化为德语、法语或韩语,则需要回退为英语
  • 适配器使用的 ID 不是本地化的字符串

示例:

<?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 pass to the adapter, none.
    );

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

?>

回显的输出将是 ID 123 的相应文本。现在在第一季度!!!评论,我如何才能验证 en/de/fr/ko 语言环境?还有第二季度!!!评论,如果找不到其他语言的ID 123,我如何回退到英语。

另外,我将处理日期和货币,因此我想验证 fr_FR、de_DE、ko_KO,而不是 fr_CA 或 de_AT 等变体。因此,对于第一部分,我需要验证语言环境是否来自我需要的国家/地区/语言,或者它们是否至少是有效的语言(并强制它们到正确的国家/地区);对于最后一部分,我需要验证如果翻译不在数据库上,则返回英语。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬花如无物 2024-10-18 12:15:29

Zend_Translate 支持回退功能。
在示例中,从 de 回退到 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')
    )
);

请注意“route”参数,该参数将允许您设置回退规则。
您无需担心区域代码,因为它会自动处理。
阅读 Zend_Translate 路由部分了解更多内容
http://framework.zend.com /manual/en/zend.translate.additional.html#zend.translate.additional.rerouting

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文