Gettext

Gettext is a set of tools, guidelines, directory naming conventions and a runtime library. Together, they are the standard way to translate applications in the *nix environment. Don't be scared, you can use it on Windows as well. I don't want to describe the whole system here, just the things you need to translate your application - you can read the manual if you want more information.

The gettext system translates replaces whole strings with the translated equivalent in the target language. This means that you don't have to invent array keys but can keep the original string in the code, which makes it a lot more readable. The translations are written down in so-called .po-files, where "po" stands for "portable object". They contain the human-readable and human-editable translation of the strings to the target language. To speed up the live-translation process when the application is running, they are compiled to .mo-files, "machine object" files. These files are required to be in a certain directory structure:
locale
  -language-
    LC_MESSAGES
      -applicationname-.po
      -applicationname-.mo
  de
    LC_MESSAGES
      appwizard.po
      appwizard.mo
      tuxracer.mo
  es
    LC_MESSAGES
      appwizard.mo
      tuxracer.mo
   
Here you see, that there are 2 languages: german (de) and spanish (es). They contain both the machine object files for the applications AppWizard and TuxRacer. The original translation files are not required for running the program, and so only the german translation file for AppWizard is here. If you are on *nix, have a look at /usr/share/locale/. You will find the translations for many programs there.

A .mo translation file looks the following way:
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR Free Software Foundation, Inc.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2002-04-06 21:44-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
#: gettext_example.php:12
msgid "A string to be translated would go here"
msgstr "Ein string der übersetzt werden soll steht hier"

msgid "Really quit?"
msgstr "Wirklich beenden?"

msgid "This is a glade language test script"
msgstr "Dies ist ein Glade Sprach-test script"
The first 15 lines are a sort of header, which can be customized with dates and authors. After the header the translation begins: msgid tells what shall be translated, and msgstr is the translation. Special chars in strings can be escaped as usually with a backslash \.

After translating your strings, you have to compiles the .po to .mo files which is done by using the msgfmt tool. On *nix, you should have it somewhere; on Windows you can get it with mingw32. Go to the LC_MESSAGES directory and just call the compiler:
# msgfmt messages.po
which will result in a messages.mo file.

© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.