on Sep 27th, 2008i18n with PHP and XML

This is a simple internationalization (henceforth i18n) library of 80 or so lines that I put together, I’m well aware that gettext plus numerous other i18n solutions already exist for PHP - but not everyone that is translating sites have the knowledge of how to use gettext or other more advanced tools and XML is a good compromise.

You can download the library here: i18n.zip

And here’s a quick “manual” of the *very* simple API:

Everything is initialized with the function i18n_create($class, [$path=”./langs”, $langs=”en”]), which takes three parameters:

  • $class string - Which class we should use for i18n, atm. only i18n_XML is available.
  • $path string - Directory where the language files are located, such as: /home/fredrik/public_html/lang, default: ./langs.
  • $langs string|array - Comma separated string or array of languages, when searching for a phrase the library will check in the order the languages are entered here, so if you want to use swedish as the main language and english as the fall back you would put “sv, en” or array(”en”, “sv”) here.

The i18n-classes themselves have two methods intended for public use:

get($path, $format = array())
  • $path string|array - Dot separated string or array of a path to look for the translation in.
  • $format string - Array of arguments for interpolation into the returned translation string, see php.net/sprintf for details.
plural($path, $number, $format = array())
  • $path string|array - Dot separated string or array of a path to look for the translation in, note that the path specified should contain one singular and one plural entry as children.
  • $number integer|Countable|array - An integer, class implementing Countable or an array representing the plural / singular status, 1 being singular and all others being plural. The integer vale collected here will be pushed on the $format at index 0.
  • $format string - Array of arguments for interpolation into the returned translation string, see php.net/sprintf for details.

The code below is taken directly from the example included in the .zip-package above:

<?php
$i18n = i18n_create("i18n_XML", "./langs/xml", "sv, en");
echo $i18n->get("welcome", array("Fredrik"));
echo $i18n->plural('news/comments', 2);

First we create an instance of i18n_XML and initialize it with the langs/xml directory and our selected languages, the first phrase will print “Hej Fredrik, och välkommen” which means “Hi Fredrik, and Welcome!” in Swedish - however the second phrase will be printed in English “2 comments” since there exists no Swedish translation for it.

If you look inside the examples/lang/xml/en directory in the .zip-package above you will see that the translation is split up into several files, this allows you to categorize files into separate files for better organization.

2 Responses to “i18n with PHP and XML”

  1. Mat_on 09 Oct 2008 at 8:43 am

    I’m going to try this library.
    I will maybe use it in my project, don’t know.

    Thx for your work!

    Mat_

  2. China manon 01 Jan 2009 at 3:43 pm

    hey…

    disagree…

Trackback URI | Comments RSS

Leave a Reply