我正在开发一个多语言的利基社交网站.这意味着我们当前的URL结构很快将需要开始使用翻译的单词,如下所示:
www.example.com/home成为www.example.com/inicio
www.example.com/profile成为www.example.com/perfil
www.example.com/help成为www.example.com/ayuda
等等.我的问题是:在PHP应用程序中支持这个的最佳方法是什么?对于传入的请求,我认为我的router.php文件中的字典就足够了:
$request = explode("/", trim($_SERVER['REQUEST_URI'], "/"));
// Dictionaries of page slugs.
$slugs = array(
'es' => array(
'inicio' => 'home',
'perfil' => 'profile',
'ayuda' => 'help',
)
// additional languages can be added here
);
// Rewrite any incoming (foreign) requests
if ($host=="www.example.es") { // to be made programmatic
$lang = "es"; // pick up from locale constant rather being hard-coded
if (array_key_exists($request[0], $slugs[$lang])) {
$request[0] = $slugs[$lang][$request[0]];
}
}
...
这基本上采用URL段并将它们与英语对应部分匹配(如果存在).如果没有,那么它将正常进行,并且很可能导致404作为控制器不存在URL段.
虽然这句话,我也需要它向后兼容.例如,在我的应用程序中构建URL时.
当然,由于应用程序目前只是英语,因此只是硬编码.所以说,在获取User对象时,我会执行以下操作:
class User {
function __construct($id) {
// fetch user details
$this->profile_url = ROOT . "/profile/" . $this->username;
}
}
然后,将“/ profile /”实例替换为获取翻译版本的硬编码的最佳方法是什么,即西班牙语网站中的“/ perfil /”?