Simple PHP i18n internationalization and localization class

Josh Sherman
3 min read
Software Development PHP

As sites and system grow and scale, the need for internationalization and localization (i18n and L10n respectively) of content becomes a necessary task. There’s not much to it on the surface, you need to be able to serve up the same content in different languages. This includes abstracting out words and phrases of content into variables that can then be populated with the the proper local text. Take for example, this very basic homepage:

echo <<<HTML
    <!DOCTYPE html>
    <html>
        <head>
            <title>Awesome Website!</title>
        </head>
        <body>
            <h1>Welcome to my awesome website!</h1>
            <p>
                This website was built by me. I did not use FrontPage or
                Dreamweaver to build it. When I grow up, I want to be a video game
                tester for Valve.
            </p>
        </body>
    </html>
HTML;

This is all well and good (especially the part about not using FrontPage or Dreamweaver ;) but what if someone from Spain that only speaks Spanish comes to your site? Sure, you could expect them to run it through Google Translate, but then you’d draw scrutiny from guys like Justin Davis that know how to take UX seriously.

To solve this quandary, we will need to first create a class that will hold our textual content. We will index the data by some arbitrary variable names as well as the language codes that are are going to support. We will use that we can use when we want to recall the data. First, let’s build out our class with our variables:

class i18n
{
    private static $strings = [
        'title' => [
            'en' => 'Awesome Website!',
            'es' => 'Impresionante sitio web!',
        ],
        'welcome' => [
            'en' => 'Welcome to my awesome website!',
            'es' => 'Bienvenidos a mi sitio web impresionante!',
        ],
        'about' => [
            'en' => '
                This website was built by me. I did not use FrontPage or
                Dreamweaver to build it. When I grow up, I want to be a video
                game tester for Valve.
            ',
            'es' => '
                Este sitio web fue construido por mí. No hice uso de FrontPage
                o Dreamweaver para construirlo. Cuando sea grande , quiero ser
                un vídeo probador de juego de Valve.
            ',
        ],
    ];

    public static function get($string)
    {
        if (isset(self::$strings[$string][$_SESSION['lang']]))
        {
            return self::$strings[$string][$_SESSION['lang']];
        }

        return '';
    }
}

Feel free to send corrections if Google Translate didn’t handle that correctly ;) Now that we have our class with the translations, we can update your HTML to utilize it:

if (!isset($_SESSION['lang']))
{
    $_SESSION['lang'] = 'en';
}

if (isset($_GET['lang']))
{
    $_SESSION['lang'] = $_GET['lang'];
}

echo <<<HTML
    <!DOCTYPE html>
    <html>
        <head>
            <title><?= i18n::get('title'); ?></title>
        </head>
        <body>
            <h1><?= i18n::get('welcome'); ?></h1>
            <p><?= i18n::get('about'); ?></p>
            <footer>
                <a href="?lang=en">En</a> | <a href="?lang=es">Es</a>
            </footer>
        </body>
    </html>
HTML;

That’s all there is to it! Keep in mind this is a very rudimentary example and probably lose maintainability after 10 or so strings. At that point you may want to move the language information off to a database.

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.

Currently Reading

Parasie Eve

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles