Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Tutorials and WordPress.

Browsing the internet, I noticed the necessity to have a blog in two or more languages. It’s a necessity I’m having too, so the first thing I did was a search into the official WordPress plugin repository, convinced that the solution to my problem was a few clicks away.

I was much, much wrong.

Anyway I observed all tricks and plugins developed in order to solve this WordPress deficiency.
Let’s see them and discover their weak points.
During the following examples I will consider the idea to create a blog in two languages, even if the same concept can be widened to a number of n languages.

Creation of two different blogs, with a homepage showing a language selection
It’s the simplest solution, anyway lacks the capability to switch form one language to another from the inside of the blog.

Creation of a button with a link to Babelfish or another service of online translations
Online translation services aren’t yet that smart to expect of being able to translate posts much more complex than “Hello world” without losing the sense of the post.

“Polyglot” plugin and other plugins inspired by it
It’s the best solution at the moment, even if lacks some options like comment separation based on their language.

No one of those systems however resolved my requirement, consisting in two distinct blogs (even hosted on two different domains), where not necessarily every page of the blog A had a correspondence in blog B, but if it should eventually exists, was reachable by a hypertext link.

Having installed WP two days ago, I do not have the required knowledge (neither the time) in order to develpo a plugin, anyway I promise to make it in a quiet moment of my work (translation: never ever), and I hope however that my small intuition shall open the way to the creation of a well coded plugin.

What do we need for having a blog in two languages?

Ingredients are:
2 images of small flags
2 installations of WordPress completely independent (two different domains, or two different databases)
1 new table in database
A bit of php coding

About the flag icos, I think you can easily draw or find them in the web.
Personally, I love these ones.

As far as the installation of WordPress, being a neophyte I cannot help you very much… just install it and eventually customize it to display the sidebar on every page, because in my example we’ll see how to change sidebar content to insert the language change link.

Let’s start with the new table

CREATE TABLE `wp_language_jump` (
  `id` int(11) NOT NULL auto_increment,
  `page` text NOT NULL,
  `link_to` text NOT NULL,
  PRIMARY KEY  (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1

where the interesting columns are “page” that will contain the address of the page in which inserting the link, and “link_to” that will contain the link (or links) to which page we want to publish the translation.

In a practical example I installed one version of WP in root of http://emanueleferonato.com/ and another in “ita” directory href=”http://emanueleferonato.com/ita”>emanueleferonato.com/ita. I want that from each of two blogs page there is a link to its proper translation, and in the same way I will deal with all future pages.
The row I inserted in the table is:

INSERT INTO `wp_language_jump` VALUES (1, '/index.php/', '/ita/');

Which communicates that from the homepage (index.php) I want to link to the italian blog homepage.
My links should be different than yours because I used “date and name based” permalink structure, please refer to documentation supplied with WP in order to understand what type of links (or permalinks) you used, even if as far as I can see more than 90% of blogs have a links structure like mine.

In the same way I want the italian homepage to link to english one, so the row is:

INSERT INTO wp_language_jump VALUES (2, '/ita/index.php/ita/', '/');

Subsequently you can insert all page link translations you want.
Now let’s see the php script. I want my link to be at the top of sidebar, so I am going to edit sidebar.php from WP administration panel, menu Presentation -> Theme Editor -> WordPress Default theme files.
You are obviously free to choose where to insert the script.
I did it after

that is just at the beginning of the sideba. The script is:

  •  English version
  • Let’s explain it line by line.

    1: I insert an element in the list that composes the sidebar; in this way my link of change language will be the first element displayed in the sidebar. You can insert the script wherever you want, I was working on the theme I have currently installed, Kubrick if I am right… (although I installed WP a few time ago, I begin to practice).

    2: Rock and roll! I open Php tag.

    3: I connect to database. I am sure there are already lot of functions to connect to a WP database, but I don’t know them so I connect with the appropriate command.

    4: Choosing the db… same considerations as line 3

    5: Get the path of the page I am browsing by using predefined php variables. You will find a complete reference at this page. This line may need to be adapted to your permalink type.

    6: Creates a MySql query to verify the presence of the path in the table we created before

    7: Executing the query. Same considerations as line 3

    8: I put the (first) row of the query result in an array. Notice that this simple script does not handle the case that the table does not contain current path. It will be a “future feature”, that sounds like “no way”.

    9: The script is over…

    10: Creation of the graphical/text link for the language exchange, calling at the right time the content of the query I have processed before.

    11: Closing list tag

    As you can see, in a few lines, even if in a “WP extremely UNfriendly” way, I managed to create an automatic link between two blogs in two different languages without modifying post structure with any tag.

    There are a lot of improvements to do, for example a control if a translated pages exists or not, additional columns where to insert link title, or image… and so on (and this “and so on” is HUGE). At the moment, that’s the most important thing to consider, it WORKS. Try the link in the sidebar and believe.

    Now the ball passes to volounteers that want to adapt – or transform – or delete (best way) – this script to make it a real WP plugin.

    That’s all folks, I think that this first post in something more than an “Hello World”.

    Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.