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.
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
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
in .htaccess, then, i have a rule which converts /en/
into ?lang=en
.
The problem
Every page has tons of links, which means i have thousands of <a href=".."></a>
tags in my website.
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
At first sight, the idea was to change it to
<a href="http://www.racebooking.net/<?php echo $_GET['lang'] . '/';?>forum">Forum</a>
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.
So, is there a smart way to add /language/
to all my website URLS?
EDIT
I would prefer not using any js. All the users with js disabled will encounter problems while browsing my website!
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.
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]
This will redirect every url path, which does not start with either en
, es
, de
, fr
, it
to the en
(default) sub path.
Edit to comments:
The solution above would enable for all your links to work (old and new ones).
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.
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.
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>
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.
You can try storing the language in a session and then check for it on your pages, and if one isn't found you can default to English.
<?
start_sesssion();
$_SESSION["lang"]="en";
Other File
/*****************\
start_session();
if($_SESSION["lang"]=="en")
english();
Write a small Java program (or any other language that you like the most) that iterates over all your website files and changes :
<a href="http://www.racebooking.net/ ...>
in
<a href="http://www.racebooking.net/<?php echo $_GET['lang'] . '/';?>
or use jQuery and include a function like this in every page :
$('a').click(function () {
var oldhref = $(this).attr('href');
// change href string here, e.g. insert /language/
$(this).attr('href', newhref);
});
This function will change the href of every anchor - tag on the page.
caveat, this is just an idea...someone else can improve this answer:
You could use jquery to modify the links on the client side. So you'd have a JS function to rebuild urls on existing tags using jquery. You'd only have to send the variable to JS at the top of each page. I'm assuming you have some sort of include function on each page so you an add some code without modifying every page.
eg (not tested)...
//top of page
var lang = <?php echo $lang?>; //this is the language
//this function takes a url and adds a language variable to the end
function updateurl(url) {
url = url + '?lang=' + lang;
}
//this loops through each anchor on the page and adds the language to the querystring
$( "a" ).each(function( index ) {
jquery(this).attr('href') = update(jQuery(this).attr('href'))
});
If you don't want to pick out every link, you can use my JS-code.
Javascript is currently not having any built-in possibility to output a GET-value, so you first need to put the lang-value in a little HTML-element, from which JS then takes the content:
<div class="current-lang"><?php echo $_GET['lang']; ?></div>
If you like, you can hide the element with the help of CSS. (As shown in the example.)
JS-Example: http://jsfiddle.net/leo/BH9nf/ (jQuery needed)
It was a lot of work, so I would appreciate a positive vote.