如何使用 db 和后备来执行 Zend_Locale ?
因此,我在谷歌上搜索并找到了一种使我的应用程序与数据库适配器一起使用以进行本地化的方法(顺便说一句,这非常有效),现在下一个出现的问题是后备。场景如下:
- 应用程序有英语、德语、法语和韩语翻译。
- 英语是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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Zend_Translate 支持回退功能。
在示例中,从 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
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