简体   繁体   English

Zend翻译URL和语言切换器

[英]Zend translate URL and language switcher

I have managed to make my URL i18n compliant using Zend_Controller_Router . 我设法使用Zend_Controller_Router使我的URL i18n兼容。

Ie: en/user/login becomes fr/utilisateur/connexion and both URLs go to the same controller / action . 即: en/user/login变为fr/utilisateur/connexion并且两个URL都到达相同的controller / action

The problem I am facing is the following 我面临的问题如下

I have a language switcher that is displayed as follow : 我有一个语言切换器,显示如下:

Français
English
Italiano
etc.

The currently active language doesn't have an anchor tag, but all others do. 当前使用的语言没有anchor标记,但其他所有语言都有。

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. 对于使用anchor的语言,我正在构建URL,并希望将其翻译为特定的语言。

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') ) 目前,如果我在French所有的网址都会建造在法国,即使我设置@local键作为URL视图助手一个参数( tried "@locale" => 'en'"@locale" => new Zend_Locale('en')

en/utilisateur/connexion
it/utilisateur/connexion

instead of 代替

en/user/login
it/utente/collegamento

because the locale used while building the URL is the one that is defined application wide. 因为在构建URL时使用的语言环境是在整个应用程序范围内定义的。

EDIT 编辑

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. 希望这有助于重新发行新刊物。

"My new issue is : how do I load multiple language translation resources?" “我的新问题是:如何加载多种语言的翻译资源?”

Just a quick caveat to say I didn't write the code below, it was taken from a example app put together by the guys at Zend however it might help to have it broken down like this. 请注意,我没有写下面的代码,它是取自Zend的伙计们编写的示例应用程序,但是将其分解成这样可能会有所帮助。

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. 他们针对示例应用程序的方法是使用包含翻译的csv文件,并使用配置文件(新项目中的默认文件为application.ini)指定所有语言翻译资源的路径。

like this: 像这样:

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

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. 并且在每个csv文件中,如果您使用Excel或Open Office之类的文件来创建它,则A列将是原始语言,B列将是翻译。

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;

and: 和:

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

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. 在上面的示例中,当选择英语时,将显示登录名,而在选择西班牙语时,将输入entrar。

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. Zend_Translate的负载不仅限于此,还有多种实现方式,因此我并不是说这是最好的方法。

If it helps or if I can give you more info let me know. 如果有帮助,或者如果我可以给您更多信息,请告诉我。

Cheers, 干杯,

Dave 戴夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM