Zend 翻译 URL 和语言切换器

发布于 2024-12-28 14:57:29 字数 956 浏览 4 评论 0原文

我已成功使用 Zend_Controller_Router 使我的 URL 兼容 i18n

IE: en/user/login 变为 fr/utilisateur/connexion 并且两个 URL 都转到同一个 controller/action

我面临的问题如下:

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

Français
English
Italiano
etc.

当前活动语言没有 anchor 标记,但所有其他语言都有。

对于带有锚点的语言,我正在构建 URL,并且希望将它们翻译成特定语言。

目前,如果我使用法语,所有网址都会用法语构建,即使我将 @local 键设置为 URL 视图助手中的参数(尝试过“ @locale" => 'en', "@locale" => new Zend_Locale('en'))

en/utilisateur/connexion
it/utilisateur/connexion

而不是

en/user/login
it/utente/collegamento

因为构建 URL 时使用的语言环境是是定义的应用范围广泛。

编辑

我对我的问题进行了更深入的研究,我发现只有当前区域设置才会添加其加载的资源,这意味着我无法以正确的语言获取路线,以便以正确的方式构建路线language

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

(我计划在不久的将来(接近发布)实现缓存,这样我可能会获得更好的性能)

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

Ie:
en/user/login becomes fr/utilisateur/connexion and both URLs go to the same 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.

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.

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

instead of

en/user/login
it/utente/collegamento

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

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)

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

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

发布评论

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

评论(1

一桥轻雨一伞开 2025-01-04 14:57:29

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

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

只是简单说明一下,下面的代码不是我编写的,它取自示例应用程序的组合由 Zend 的人设计,但是像这样分解可能会有所帮助。

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

像这样:

;; 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 列将是翻译。

举个例子,英语是原始语言,需要西班牙语翻译:

A       B
login   entrar
logout  salir

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

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

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

由于翻译对象保存在会话中,因此您可以在任何视图等中使用它,使用类似以下内容的内容:

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

和:

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

您希望在选择替代语言时翻译某些内容。在上面的示例中,当选择英语时,单词“login”将出现;当选择西班牙语时,单词“entrar”将出现。

Zend_Translate 的负载比这更多,并且有多种实现它的方法,所以我并不是说这是最好的方法。

如果有帮助或者我可以为您提供更多信息,请告诉我。

干杯,

戴夫

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.

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.

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.

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.

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.

Cheers,

Dave

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