Internationalisation Tips

practical tips on building an international presence

Replacing static text strings with references

By Isofarro on March 24th, 2009 - No comments

The first major step to localising a website or web application is to translate all the static text strings into the preferred language. Static text being text that doesn’t change from request to request.

The typical approach to translating this text is to replace every piece of static text with a variable reference that points to a translation dictionary. For example the simple code:


echo "Hello world";

The static text “Hello World” should be replaced with a variable reference. In this example, let’s create a simple lookup array in PHP (in its own external file, and include it in):


<?php
$translations = array(
    'hello_world' => 'Hello World'
    // Other translation strings
);
?>

We can now replace our original text with the reference $translations['hello_world'], like so:


echo $translations['hello_world'];

With the $translations array being in an external PHP file we can have one of these files per locale or language and include the right one at request time.

This is the first step at separating the locale specific information from our code. Adding a new locale means creating a new translations file for that locale and more importantly, no code changes.

A large chunk of internationalising code for localisation involves just replacing existing text with translated versions. It is an important step, and the first one to get right. The next step will involve dealing with strings that are dynamically altered at request time.

Add a comment or reply





Copyright © 2007 - 2009, isolani