Skip to content
Single-file php class to help building multilingual websites.
Branch: master
Clone or download
felladrin Merge pull request #1 from felladrin/master
Added API Documentation.
Latest commit 551180e Nov 9, 2015
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs Added API Documentation. Nov 9, 2015
example Updated example Sep 29, 2015
.gitignore Added .gitignore file Sep 28, 2015
LICENSE Initial commit Sep 28, 2015 Update Nov 6, 2015
Translation.php Create Translation.php Sep 28, 2015

PHP Translation Class

Single-file php class to help building multilingual websites.


  • client language autodetection
  • all in a single fully docummented file
  • allows passing variables to the messages
  • allows to force a different language for an specific block of the site
  • loads language data only once and only if needed - no memory waste
  • full-static class with methods to customize settings

Author: Victor Nogueira

Copyright: Copyright © 2015 Interativa Digital

License: The MIT License (MIT)


Consider you have the following file structure:

├── lib
│   └── php-translation-class
│       └── Translation.php
├── locales
│   ├── en-US.php
│   ├── es-ES.php
│   └── pt-BR.php
└── index.php

So, if you want to use the translation class on your index.php, you should do the following:

Request the script on the top of the file you'll use it:

require_once __DIR__ . '/lib/php-translation-class/Translation.php';

Below that line, define the namespace to use:

use Translation\Translation;

Now you are ready to use the translation class. It's as simple as that:

<?= Translation::of('greetings'); ?>

See? You just need to indicate the keyword for a string from the locales files, and voilá! The string will appear on the language of the visitors browser if the locale is available. Otherwise, it shows the string from the default language.

You can also pass variables to the translation strings, like this:

<?= Translation::of('greetings', ['username' => $username, 'email' => $userEmail]); ?>

And you can use any valid character for the keywords on the locale files, so you can, for example, use dots to organize the translations, and then you could use it like that:

<?= Translation::of('home.welcome'); ?>
<?= Translation::of('home.title'); ?>
<?= Translation::of('home.subtitle'); ?>
<?= Translation::of('home.subtitle.quote', ['quote' => $quoteOfTheDay, 'author' => $quoteAuthor]); ?>
<?= Translation::of('home.subtitle.time', ['time' => date("F j, Y, g:i a")]); ?>
You can’t perform that action at this time.