2.3.4. Supporting multiple languages

This feature is supported by the multiple file structure.

If your module includes text that will be displayed to end users, you should strongly consider making it support multiple languages. This will allow Phorum installations that use a different language(s) to display output of your module in the same language(s), instead of the language you have written the module in.

For supporting multiple languages, the first thing to do is add the following line to your module information file info.txt:

hook: lang|

There is no actual hook function configured here, because the "lang" hook is only used as a marker for Phorum. It tells Phorum that your module supports multiple languages.

Next, you must provide at least one language file with your module. Language files are stored in a subdirectory name "lang" inside your module directory. In our sample module, the full directory would be {phorum dir}/mods/foo/lang/. The language files must be named identical to the main language files that Phorum uses. To include both English and French, your module would require the following file structure:

Example 2.4. Tree structure for a module that supports languages

{phorum dir}/
     |
     +-- mods/
          |
          +-- foo/
               |
               +-- info.txt
               |
               +-- foo.php
               |
               +-- lang/
                    |
                    +-- english.php
                    |
                    +-- french.php 


The structure of your language files will be almost identical to that of the main Phorum language files. However, for your own language files it is advisable to add an extra level in the language variables, to avoid conflicts with language string from other modules or Phorum itself. Here is an example of how you could do that:

Example 2.5. Custom language file for a module

<?php
$PHORUM["DATA"]["LANG"]["mod_foo"] = array(
    "Hello"   => "Hello!",
    "Bye"     => "Good bye!"
);
?> 


Here, the extra inserted level is ["mod_foo"]. To access the "Hello" string from your module code you would use the PHP variable:

$PHORUM["DATA"]["LANG"]["mod_foo"]["Hello"]

When you want to use the language string from a template file, the you would use the following template variable:

{LANG->mod_foo->Hello}

In case a Phorum installation is using a language that your module does not support, Phorum will automatically attempt to fallback to English. So it is highly recommend that you include an english.php language file in all your modules. If both the current language and English are not found, Phorum will be unable to load a language for your module and will display empty space instead of your language strings.

Always try to reuse strings that are already in the main Phorum language files itself. Only create custom strings when there is no alternative available. Having more text to translate is more work for translators and using core language strings helps in keeping the used terminology consistent.