简体   繁体   English

更改多语言网站的 URL 中的路径

[英]Changing the path in a URL for a multilingual website

I am coming to you to find out if it is possible to change the path in a URL using a PHP variable.我来找您是为了了解是否可以使用 PHP 变量更改 URL 中的路径。 My goal would be to organize my website with 2 folders (one for each language), choose the language on a random page in one of the two folders and be able to change the language by modifying the URL.我的目标是用 2 个文件夹(每种语言一个)组织我的网站,在两个文件夹之一的随机页面上选择语言,并能够通过修改 URL 来更改语言。

For example, I'm on http://truc.com/fr/test.php ;例如,我在http://truc.com/fr/test.php 上 I change the language and I get http://truc.com/en/test.php .我改变了语言,我得到了http://truc.com/en/test.php

I've done a test that works but I'd prefer something more dynamic, that will work on all the pages of the website: fichier index.php我已经做了一个有效的测试,但我更喜欢更动态的东西,它适用于网站的所有页面:fichier index.php

<?php
session_start();
if (!isset($_SESSION['lang']))
    if (!isset($_SESSION['lang']))
    $_SESSION['lang'] = "fr";
else if (isset($_GET['lang']) && $_SESSION['lang'] != $_GET['lang'] && !empty($_GET['lang'])) 
    {
        if ($_GET['lang'] == "fr")
            $_SESSION['lang'] = "fr";
        else if ($_GET['lang'] == "en")
            $_SESSION['lang'] = "en";
    }
if($_SESSION['lang']==fr)
    {
        header('Location: http://localhost/site/languages/fr/index.php');
    }
else if($_SESSION['lang']==en)
    {
        header('Location: http://localhost/site/languages/en/index.php');
    }
?>

Adjusted your code and added a new variable $page_language that handles the page language and redirects to specific link for various languages and before redirecting to the page a session language is set to remember it on next page .调整了您的代码并添加了一个新变量 $page_language 来处理页面语言并重定向到各种语言的特定链接,在重定向到页面之前,会话语言设置为在下一页记住它。 Good luck祝你好运

if (isset($_SESSION['lang'])) {
  $page_language = $_SESSION['lang'];
} elseif (isset($_GET['lang']) && !empty($_GET['lang'])) {
  $page_language = $_GET['lang'] ;
} else {
  $page_language = "fr" ; //set default language if no session and url has language
}

if ($page_language == "fr") {
  $_SESSION['lang'] = "fr"; //set session language for later visits
  header('Location: http://localhost/site/languages/fr/index.php');
  exit;
} elseif ($page_language == "en") {
  $_SESSION['lang'] = "en"; //set session language for later visits
  header('Location: http://localhost/site/languages/fr/index.php');
  exit;
}

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

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