简体   繁体   English

PHP 多语言 GET 参数

[英]PHP Multilanguage GET parameters

I'm building multi-language support for PHP page.我正在为 PHP 页面构建多语言支持。 Let's say I have a page url:假设我有一个页面网址:

http://mypage.php?user=eric&city=newyork

What is the common solution to switch between get parameters lang=en and lang=es while keeping the get parameters already in the url?在获取参数lang=enlang=es之间切换同时将获取参数保留在 url 中的常见解决方案是什么?

A useful script that returns the relative URL of the current page with the current "GET data".一个有用的脚本,它返回带有当前“GET 数据”的当前页面的相对 URL。

<?php 
function getCurrentURL() { 
    $currentURL = basename($_SERVER["PHP_SELF"]); 
    $i = 0; 
    foreach($_GET as $key => $value) { 
        $i++; 
        if($i == 1) { $currentURL .= "?"; } 
        else { $currentURL .= "&amp;"; } 
        $currentURL .= $key."=".$value; 
    } 
    return $currentURL; 
} 
?> 

modify it and concatenate your language parameters accordingly修改它并相应地连接您的语言参数

You would not keep the GET Params in the url at all times, only when your setting the language, you would use sessions to remember what language that current client is using.您不会一直在 url 中保留 GET 参数,只有在设置语言时,您才会使用会话来记住当前客户端使用的语言。

create the following function in your php在你的 php 中创建以下函数

function getCurrentLanguage()
{
    if(isset($_REQUEST['lang']))
    {
         //Validate that $_REQUEST['lang'] is valid
         return $_SESSION['lang'] = $_REQUEST['lang'];
    }

    if(isset($_SESSION['lang']))
    {
        return $_SESSION['lang'];
    }

    return 'en'; //Default
}

now when the user comes to the site the default language would be English, but if he navigates to index.php?lang=es then the language would be set to es, so when he navigates around the site without the ?lang=es then it would show the Spanish version.现在,当用户访问该站点时,默认语言将是英语,但如果他导航到index.php?lang=es则语言将设置为 es,因此当他在没有?lang=es情况下浏览站点时它会显示西班牙语版本。

You can use my favorite language PHP Multi Language Class - LangQuery 您可以使用我最喜欢的语言PHP多语言类-LangQuery

It is the easiest one I ever saw. 这是我见过的最简单的一个。

You can save your language data to .ini files than just call it by 您可以将语言数据保存到.ini文件中,而不仅仅是通过

include("LangQuery.php");

$L=new LangQuery();

// Write Hello World
echo($L('hello_world'));

// Write Hello World Easier - In-line Echo Feature
// You don't have to write echo. Just add '>'
$L('>hello_world');

// Use in Strings
echo("Hello Universe. {$L('hello_world')} Hello Europe");

// Write my age with parameter
$L(">my_age",25);

// Write my name and age with parameters
$L(">my_name_age","Recep",25);

It's better to use cookie or session/id data to store this kind of information.最好使用 cookie 或 session/id 数据来存储此类信息。 I'll have less things to carry around.我要带的东西会少一些。

If I understand the question correctly, you want a way to add a parameter to a URL or replace its value with a new value if it already exists.如果我正确理解了这个问题,您需要一种向 URL 添加参数或将其值替换为新值(如果该值已存在)的方法。

Here's a function that does so safely in all manner of URLs:这是一个可以在各种 URL 中安全地执行此操作的函数:

function url_with_parameter($url, $name, $value) {
    if(strpos($url, '?') === false) {
        if (strpos($url, '#') === false) {
            // No query and no fragment -- append a new query to the url
            $newurl = $url.'?'.$name.'='.$value;
        }
        else {
            // No query with fragment -- insert query before existing fragment
            $newurl = str_replace('#', '?'.$name.'='.$value.'#', $url);
        }
    }
    else {
        if (strpos($url, $name.'=') === false) {
            // With query, param does not exist -- insert at beginning of query
            $newurl = str_replace('?', '?'.$name.'='.$value.'&', $url);
        }
        else {
            // With query, param exists -- replace it
            $parsed = parse_url($url, PHP_URL_QUERY);
            parse_str($parsed, $vars);
            $newurl = str_replace($name.'='.$vars[$name], $name.'='.$value, $url);
        }
    }

    return $newurl;
}

Remember to call urlencode before using the output of this function to make a link.请记住在使用此函数的输出创建链接之前调用urlencode

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

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