Another way to deliver the content in various languages is by using message catalogs. In this
case, there is one PHP file for each language. It must be a valid PHP file because the
message catalog is nothing more than a collection of PHP constants, each referring to a
particular message. The reasons why we use constants and not variables are twofold. First, the
message does not change and by definition it is a constant. Second and most important, is
that in PHP, constants are accessible at any place, even within functions. Whereas, if we use
variables for the messages, we would have to resort to using a "global" declaration for each
message used within a function.
The catalogdir class variable specifies the location of all message catalogs. It is an absolute
path within the server, where each application will have its message catalog. The name of
each catalog file has three parts: the application ID, the language code and a valid PHP
extension so that it can be include()ed by CtNls.
Now, assuming we have an application called "myapp," we could use the following code at the
This, as well as LoadPage(), can only be used after you have created a new instance of the
CtNls object. This method expects at least one file (with the default language) in the
message catalog. In this case, myapp.en.php, and inside, it must have valid constant
declarations for each message. For example:
// Language: English
// File: path/to/nls/myapp.en.php
define("MSG_WELCOME", "welcome to our site");
define("MSG_REDIRECTED", "You are being redirected to our new site");
// Language: Spanish
// File: path/to/nls/myapp.es.php
define("MSG_WELCOME", "Bienvenido a nuestras paginas");
define("MSG_REDIRECTED", "Esta siendo redireccionado a nuestro " .
"nuevo sitio de web");
Only one of these will be loaded, depending on the language priority assigned by the user.
Then, somewhere in the PHP application, you can generate output like this:
echo MSG_WELCOME . "<br>";