将语言添加到所有网站 URL 的智能方法? [英] Smart way to add language to all website URLS?

查看:37
本文介绍了将语言添加到所有网站 URL 的智能方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今年我写了一个非常大的网站,没有 CMS.我是从头开始写的.今天我开始实施一个翻译系统.

In this year i wrote a very big website, with no CMS. I wrote it from scratch. Today i'm starting to implement a translation system.

这个想法基本上是在 URL 中添加 /language/.这意味着如果索引页,当我浏览默认语言时,有 url

The idea is, basically, to add /language/ inside the URL. This means if the index page, when i'm browsing on default language, has the url

http://www.racebooking.net/index.php

一旦用户点击英文标志以查看网站的英文版本,URL 将变为

once the user clicks on the english flag in order to view the english version of the website, the URL will become

http://www.racebooking.net/en/index.php

在 .htaccess 中,我有一个将 /en/ 转换为 ?lang=en 的规则.

in .htaccess, then, i have a rule which converts /en/ into ?lang=en.

问题

每个页面都有大量链接,这意味着我的网站中有数千个 <a href=".."></a> 标签.

Every page has tons of links, which means i have thousands of <a href=".."></a> tags in my website.

例如,在我的 index.php 页面上,我有一个这样的链接

For example, on my index.php page, i have a link like this one

<a href="http://www.racebooking.net/forum">Forum</a>

如果我目前正在浏览英文版,应该指向哪个

which, if i am currently browsing the english version, should point to

http://www.racebooking.net/en/forum

乍一看,是想把它改成

<a href="http://www.racebooking.net/<?php echo $_GET['lang'] . '/';?>forum">Forum</a>

效果很好!但是,问题是我的网站上有数千个 <a> 标签!!!将它们全部更改将是完全疯狂的,只需添加 <?php echo $_GET['lang'] .'/';?> 在他们!这需要几天、几天和几天的工作.

which works pretty good! But, the problem is i have thousands of <a> tags on my website!!! it would be totally insane to change them all, for just adding <?php echo $_GET['lang'] . '/';?> in them!! It would take days and days and days of work.

那么,有没有一种聪明的方法可以将 /language/ 添加到我所有的网站 URL 中?

So, is there a smart way to add /language/ to all my website URLS?

编辑

我宁愿不使用任何 js.所有禁用js的用户在浏览我的网站时都会遇到问题!

I would prefer not using any js. All the users with js disabled will encounter problems while browsing my website!

推荐答案

HTTP 永久重定向状态代码 (301) 完全适合您的情况.指向您网站的所有链接仍然有效,而且所有书签都将更新为您的新网址路径结构.

The HTTP permanent redirect status code (301) is exactly for your case. All links to your site will still work, and additionally any bookmarks will be updated to your new url path construct.

您可以使用 mod_rewrite 在 Apache 中进行设置:

You can set it up in Apache using mod_rewrite:

RewriteRule /(?!en|es|de|fr|it)(/?.*)$ http://www.mydomain.com/en/$1 [R=301,L]

这将重定向每个 url 路径,它不以 enesdefr 开头>, iten(默认)子路径.

This will redirect every url path, which does not start with either en, es, de, fr, it to the en (default) sub path.

编辑评论:

上述解决方案将使您的所有链接(旧链接和新链接)都能正常工作.

The solution above would enable for all your links to work (old and new ones).

如果您还想更改页面上的所有链接,以便搜索引擎能够看到它们,那么您唯一的选择就是在您的 PHP/HTML 代码中重写它们(出于多种原因,使用 JavaScript 是一个坏主意)).

If you additionally want to change all the links on your page, so that search engines will see them, then your only option is to rewrite them in your PHP/HTML code (using JavaScript for this is a bad idea for many reasons).

在这种情况下,最好的选择是编写一个辅助函数,它会根据当前语言为您生成链接.这样,您将有一个单一的点,您可以在其中更改您的链接,如果它们将来再次更改.

The best option in this case is to write a helper function, which generates the links for you depending on current language. This way you will have a single point, where you can change your links, should they change once again in the future.

很遗憾,您无法一次更改所有链接.如果您有成千上万个,并且没有生成所有这些的通用代码,那么您必须一一执行.好消息是,您不是第一个遇到此问题的人,专业 IDE 的开发人员已经实施了工具来帮助您.我个人选择的是商业软件,但其他开源 IDE 也有很好的查找/替换选项.例如,您可以编写一个正则表达式,它会找到您的链接并根据您提供的规则相应地替换它们.编写一个好的正则表达式替换器可能被证明是非常有益的,而不是一个一个地检查所有链接.

Unfortunately, you won't be able to change all of your links at once. If you have thousands of them, and no common code which generates all of them, then you have to do this one by one. Good news is, that you are not the first one with this problem, and the developers of professional IDE's already implement tools to aid you. My personal choice is a commercial software, but other open sourced IDE's also have a pretty good find/replace options. You can for instance write a regex, which will find your links and replace them accordingly to the rules you provide. To write a good regex replacer might prove to be very beneficial as opposed to reviewing all links one by one.

这是 url helper 的可能实现之一:

This is one of the possible implementation of the url helper:

class UrlHelper
{
    public static function make($base, $lang = null)
    {
        if ( $lang === null ) {
            $lang = 'en';
            if ( isset($_GET['lang']) ) {
                $lang = $_GET['lang'];
            }
        }
        $url = "http://www.racebooking.net/{$lang}/{$base}";
        return $url;
    }
}

现在你必须找到所有输出链接的地方并使用帮助器:

Now you have to find all places where links are outputed and use the helper:

<a href="<?php echo UrlHelper::make('/old/url'); ?>">Link</a>

请注意,这只是它如何工作的一个示例.我实际上不建议将 helper 实现为静态方法,因为 静态是纯粹的邪恶.对于您的实际实现,您可能会考虑使用 helper object 而不是 helper method.

Note that this is just an example of how it could work. I actually don't recommend to implement the helper as a static method, because static is pure evil. For your real implementation you might consider a helper object instead of helper method.

这篇关于将语言添加到所有网站 URL 的智能方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆