如何根据域默认使用另一种语言

发布于 2024-10-14 04:25:17 字数 262 浏览 4 评论 0原文

我正在使用 Joomla 和 JoomFish 翻译组件。该网站有德文和中文翻译。我想弄清楚的是如何让 .de 域默认为德语翻译(如果以domain.com/de 或domain.com/cn 访问,则会加载)。

有谁知道如何使用 .htaccess (某种重定向)来做到这一点?或者可能是 PHP?也许根据域(PHP_URL_HOST)设置某种会话变量?

现在我有 apache2 设置,以 wwww.domain.com 作为主要虚拟主机,以 .de 和 .cn 作为别名。

i'm using Joomla with the JoomFish translation component. This website has german and chinese translations. What i'm trying to figure out is how to get the .de domain to default to the german language translation ( which would load if visited as domain.com/de or domain.com/cn ).

Does anyone know a way to do this with maybe .htaccess ( some kind of redirect )? Or possibly PHP? Maybe set some kind of session variable based on the domain (PHP_URL_HOST) ?

Right now i have apache2 setup with the wwww.domain.com as the main virtual host, and the .de and .cn as aliases.

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

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

发布评论

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

评论(3

双手揣兜 2024-10-21 04:25:17

欢迎来到SO!

如果您的 Apache 启用了 mod_setenvif,请将其添加到您的 .htaccess 文件中:

# Site accessed via "example.de" or "example.cn"
SetEnvIf Host "\.de$" SITE_LANGUAGE=de
SetEnvIf Host "\.cn$" SITE_LANGUAGE=zh

# URL dependent
SetEnvIf Request_URI "^/de/" SITE_LANGUAGE=de
SetEnvIf Request_URI "^/cn/" SITE_LANGUAGE=zh

然后,在您的 PHP 脚本中您可以查询 SITE_LANGUAGE

switch($_SERVER['SITE_LANGUAGE']) {
   case 'de':
      // german stuff
   case 'zh':
      // chinese stuff
}

Welcome to SO!

Provided, your Apache has mod_setenvif enabled, add this to your .htaccess file:

# Site accessed via "example.de" or "example.cn"
SetEnvIf Host "\.de$" SITE_LANGUAGE=de
SetEnvIf Host "\.cn$" SITE_LANGUAGE=zh

# URL dependent
SetEnvIf Request_URI "^/de/" SITE_LANGUAGE=de
SetEnvIf Request_URI "^/cn/" SITE_LANGUAGE=zh

Then, in your PHP script you can query SITE_LANGUAGE:

switch($_SERVER['SITE_LANGUAGE']) {
   case 'de':
      // german stuff
   case 'zh':
      // chinese stuff
}
奶气 2024-10-21 04:25:17

谢谢你!添加 htaccess 代码后,我所要做的就是在大约 145 处编辑 /plugins/system/jfrouter.php

if (isset($_SERVER['SITE_LANGUAGE'])) {
    switch($_SERVER['SITE_LANGUAGE']) {
        case 'de':
            $client_lang = 'de';
            $lang_known = true; 
            JRequest::setVar('lang', 'de' );
            break;
        case 'zh':
            $client_lang = 'zh';
            $lang_known = true;
            JRequest::setVar('lang', 'zh' );
            break;
    }
}

Thank you! After adding the htaccess code, all I had to do is edit /plugins/system/jfrouter.php at about around 145:

if (isset($_SERVER['SITE_LANGUAGE'])) {
    switch($_SERVER['SITE_LANGUAGE']) {
        case 'de':
            $client_lang = 'de';
            $lang_known = true; 
            JRequest::setVar('lang', 'de' );
            break;
        case 'zh':
            $client_lang = 'zh';
            $lang_known = true;
            JRequest::setVar('lang', 'zh' );
            break;
    }
}
同展鸳鸯锦 2024-10-21 04:25:17

如果您在 php 中有一个基于 get 请求的选择案例怎么办?

诸如 domain.com/?lang=de 之类的内容与

$lang = "default";
if (isset($_GET['lang'])) {
    $lang = $_GET['lang'];
}   
if ($lang != "default") {
    if($lang == "de") {
        ....

您的代码结合使用。 .... 将是您加载翻译器或语言资源的位置。

What if you had a select case in php based on a get request?

Something like domain.com/?lang=de in conjunction with

$lang = "default";
if (isset($_GET['lang'])) {
    $lang = $_GET['lang'];
}   
if ($lang != "default") {
    if($lang == "de") {
        ....

in your code. .... would be where you load your translator or language resource.

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