0

请帮助我处理必须重定向到正确语言的 php 文件。网站架构是 site.com/en site.com/fr

我的鳕鱼:

<?php
$sites = array(
"en" => "/en/index",
);

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

if (!in_array($lang, array_keys($sites))){
$lang = 'en';
}
// перенаправление на субдомен
header('Location: ' . $sites[$lang]);
?>
4

2 回答 2

1

您可以将$_SERVER['PHP_SELF']包含相对于文档根目录的路径的变量附加到您的语言文件夹。

如果您的路径是site.com/fr/file,则此变量将包含/fr/file. 当您删除此部分的第一部分时,您将获得被调用脚本的“语言无关”路径,您可以将其附加到所需的语言目录。

如果您的路径与您描述的一样,您可以尝试以下解决方案(未经测试!):

<?php
$sites = array(
"en" => "/en",
);

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

if (!in_array($lang, array_keys($sites))){
$lang = 'en';
}

$path_to_script = substr($_SERVER['PHP_SELF'], 3);

header('Location: ' . $sites[$lang] . $path_to_script);
?>

编辑:澄清方法:如果站点是site.com/fr/123/index.php,则变量$path_to_script将包含/123/index.php,没有前导语言目录的脚本目录。然后,您可以将其附加到所需的语言目录并获得所需的路径:/en/123/index.php

于 2012-06-24T15:55:41.517 回答
0

/en/index should be /en/index.php except if you are using Apache's mod_rewrite (not by default). Please provide more details of what is not working so we can help you more.

EDIT: Furthermore, you could give the user the option of choosing language as 'HTTP_ACCEPT_LANGUAGE' might not be set. In that case, the only choice would be English, which would limit your public.

Some people even recommend about geolocalization, but I find it good enough to add a little drop down menu with languages.

于 2012-06-24T15:33:06.060 回答