Zend转换URL和语言切换器 [英] Zend translate URL and language switcher

查看:105
本文介绍了Zend转换URL和语言切换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法通过Zend_Controller_Router使我的URL i18n兼容.

I have managed to make my URL i18n compliant using Zend_Controller_Router.

即: en/user/login变为fr/utilisateur/connexion,并且两个URL都指向相同的controller/action.

Ie: en/user/login becomes fr/utilisateur/connexion and both URLs go to the same controller/action.

我面临的问题如下

我有一个语言切换器,显示如下:

I have a language switcher that is displayed as follow :

Français
English
Italiano
etc.

当前使用的语言没有anchor标记,但所有其他语言都具有.

The currently active language doesn't have an anchor tag, but all others do.

对于带有anchor的语言,我正在构建URL,并希望将其翻译为特定的语言.

For the languages that have an anchor on them I am building the URL and I want them to be translated in their specific languages.

当前,如果我使用的是French,则所有网址都将以法语构建,即使我在URL视图助手(tried "@locale" => 'en'"@locale" => new Zend_Locale('en'))中将@local键设置为参数

Currently if I am in French all the urls get builded in french, even if I set the @local key as a param in the URL view helper (tried "@locale" => 'en', "@locale" => new Zend_Locale('en'))

en/utilisateur/connexion
it/utilisateur/connexion

代替

en/user/login
it/utente/collegamento

因为构建URL时使用的语言环境是在整个应用程序中定义的语言环境.

because the locale used while building the URL is the one that is defined application wide.

编辑

我对问题进行了更深入的研究,发现只有当前语言环境会加载其资源,这意味着我无法使用正确的语言来获取要使用正确语言构建的路线的路线

I digged a bit deeper in my issue an I found that only the current locale add its resources loaded, which mean I can't get route in the proper language for the route to be built in the right language

我的新问题是:如何加载多种语言的翻译资源?

My new issue is : how do I load multiple language translation resources?

(我打算在不久的将来(即将发布)实现缓存,所以我可能会获得更好的性能)

(I am planning to implement cache in the far future (near release), so I might get better performances)

推荐答案

希望这有助于解决新问题.

Hope this helps re the new issue.

我的新问题是:如何加载多种语言的翻译资源?"

仅需说明一下,我没有写下面的代码,它是从Zend的伙计们组装的示例应用程序中摘录的,但是将其分解成这样可能会有所帮助.

示例应用程序的方法是使用包含翻译的csv文件,并使用配置文件(新项目中的默认文件为application.ini)来指定所有语言翻译资源的路径.

Their approach for the example app was using a csv file containing translations and using your config file (the default one in a new project being application.ini) to specify the path to all your language translation resources.

像这样:

;; Languages
language.file.en = APPLICATION_PATH "/../language/en/translate.csv"
language.file.fr = APPLICATION_PATH "/../language/fr/translate.csv"
language.file.de = APPLICATION_PATH "/../language/de/translate.csv"
language.file.es = APPLICATION_PATH "/../language/es/translate.csv"
language.name.zz = Language
language.name.en = English
language.name.fr = Français
language.name.de = Deutsche
language.name.es = Español

在每个csv文件中,如果要使用Excel或Open Office之类的文件来创建它,则A列将是原始语言,B列将是翻译.

And in each csv file, if you are using something like Excel or Open Office to create it, column A would be the original language and column B the translation.

例如,英语是原始语言,并且需要西班牙语翻译:

As an example where English is the original language and a Spanish translation is required:

A       B
login   entrar
logout  salir

您可以对要翻译的所有单词/短语执行此操作. 如果找不到翻译,则使用默认的原始单词.

You could do that for all the words/phrases you want to translate. If a translation isn't found then the default original word is used.

您的主应用程序引导程序可能具有以下内容:

Your main application bootstrap could have something like this:

protected function _initLanguage()
{
    $options = $this->getOptions();
    Zend_Registry::set('language',$options['language']);
    $langSess = new Zend_Session_Namespace('language');
    if (!isset($langSess->translate)) {
        $translate = new Zend_Translate('csv', $options['language']['file']['en']);
        $langSess->translate = $translate;
    }
    if (!isset($langSess->locale)) {
        $langSess->locale = 'en';
    }
    Zend_Locale::setDefault($langSess->locale);
} 

由于翻译对象保留在会话中,因此您可以在您的任何视图中使用它,例如:

As the translate object is held in the session you can use it in any of your views etc using something like:

$langSess = new Zend_Session_Namespace('language');
$translate = $langSess->translate;

和:

<a href="/user/login"> <?php echo $translate->_('login') ?> </a>

在选择其他语言时要翻译的内容.在字上面的例子中当选择英语似乎并选择西班牙语时entrar登录.

where you want something to be translated on selecting an alternative language. In the example above the word login would appear when English is selected and entrar when Spanish is selected.

Zend_Translate的负载不仅限于此,还有多种实现方式,因此,我并不是说这是最好的方式.

There's loads more to Zend_Translate than this and several ways to implement it so I'm not saying this is the best way by any means.

如果有帮助,或者如果我可以给您更多信息,请告诉我.

If it helps or if I can give you more info let me know.

干杯

戴夫

这篇关于Zend转换URL和语言切换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆