Ever wanted to translate your website so that it appeals to a wider audience?
If you have then continue reading as ill take you through the steps in which to set this up. There is an excellent function in PHP called gettext. In simple terms instead of just writing “Hello World” in your document you would write <?=gettext(“Hello World”);?> or shorthand <?=_(“Hello World”);?> then PHP looks up the equivalent of that text in your desired language and displays it on the screen.
To me, the best way to get this working is to work locally then upload to the server afterwards, so thats what I am going to do. So to start with create a new folder on your desktop called “Translation”.
Inside the Translation folder create a file called test.php. Insert the following code into the page:
[sourcecode language='php']echo gettext(“Hello World!”); [/sourcecode]
Next you need to download a program called poEdit – this makes your life much easier when translating to different languages. Once you have downloaded and installed poEdit you need to setup the directories for the files to go into.
Inside the Translation folder create a new folder called: locale.
Inside this folder you create the folder for your desired language, for this example we are going to use german so we create a folder called: de_DE.
Inside this folder you create another folder called: LC_MESSAGES.
If you are getting confused at this point, don’t worry, we are just setting up the necessary directories ready for the translation steps.
With the directories now setup we now need to setup poEdit so that it creates the necessary language files. Open poEdit then go to File -> New catalog.
Insert anything in the Name of the project.
Source code charset: iso-88591
Then go along to the paths tab at the top of the window. Enter the base path to the folder you created on your desktop for example “/Users/Bytewire/Desktop/Translation”. Then in the paths list below click the dotted square and type the path as “.”. Having set this up, press Ok. When asked saved the file as messages.po inside the LC_MESSAGES folder.
poEdit should now have the string “Hello World” displayed in the top half of the screen. Click on the string and it will display the text in the 2nd box down. The 3rd box down will be empty, here is where you enter your translation, so for german enter “Hallo Welt!”. Once you have done this, save the file, which will automatically create a file called messages.po.
The next step is to set the language of the page to german and then view the page again. So create a new page in the same location as test.php and name it “localisation.php”. Inside this file enter the following code:
$locale = "de_DE"; if(isSet($_GET["locale"])) $locale = $_GET["locale"]; putenv("LC_ALL=$locale"); setlocale(LC_ALL, $locale); bindtextdomain("messages", "./locale"); textdomain("messages");
The above code basically sets your language to german, so that when you view the page it will display the german equivalent of the text if available. Finally you need to open up test.php and include the localisation file so at the top of the file add the following code: require_once(“localisation.php”);
Now if you view test.php, you may need to upload all the files you just created to your server if you dont have a local server, you should see the text “Hallo Welt!”.
Although fairly time consuming to setup, gettext is a very good method of translating your website and is very commonly used. Having set this up you should be able to further this and translate in many different languages.
I hope this helps some people translate their websites.