Language localization plugin


This plugin allows two things:

  • Manage URLs that are prefixed by language. The plugin will therefore automatically redirect the user to the requested page with /fr/ (for example) at the beginning of the URL.
  • Automatically load the translations corresponding to the language used, to make the translated strings available to the templates.


In the temma.json file, add the pre-plugin and configure it:

    // loading the plugin
    "plugins": {
        "_pre": [
    // plugin configuration
    "x-language": {
        // list of languages supported by the site
        "supported": ["fr", "en", "de"],
        // default language, if the browser is not compatible with
        // any of the languages listed above
        "default": "fr"
    // configuration of the Smarty view
    "x-smarty-view": {
        "pluginsDir": "/path/to/project/lib/smarty-plugins"
  • Lines 3 to 7: Plugins configuration.
    • Line 5: Activation of the pre-plugin that supports language management.
  • Lines 9 to 15: Plugin configuration.
    • Line 11: List of languages supported on the site.
    • Line 14: Definition of the default language (used if the browser is not compatible with any of the languages listed.
  • Lines 17 to 19: Configuration of the Smarty view.
    • Line 18: Addition of the directory containing the Smarty plugins provided by Temma, so that the Smarty interpreter is able to find the extension (see below).


In the example of the configuration above, we indicate that the site has translations in French, English and German.

When a browser requests a page, two cases can occur:

  • The requested URL does not start with /fr/, /en/ nor /de/. In this case, the plugin will redirect the user to the requested URL, adding the language prefix. The language chosen will be the first one supported by the browser which is also supported by the site (if none is suitable, the default language of the site will be used).
  • The requested URL begins with a language prefix. In this case, the plugin will check that the requested language is well supported by the site (if this is not the case, we are back in the previous case). If the language is well supported, the plugin will extract the language, and modify all the information relating to the controller, the action and the parameters, to pretend that this prefix did not exist in the URL. Beside that, the plugin loads the corresponding translation file, and adds a prefix to the path leading to the template files.

For example, if the user requests page /page/list/date/5, with a browser that supports French, the plugin will redirect her to /fr/page/list/date/5.

When arriving on /fr/page/list/date/5, the plugin will extract the language (fr), will load the translation file in French. Then it will modify several things:

  • The requested controller becomes page (and no longer fr).
  • The requested action becomes list (and no longer page).
  • The list of parameters becomes ['date', '5'] (and no longer ['list', 'date', '5']).

The template variables $CONTROLLER, $ACTION and $URL are also modified accordingly.

Prefix to templates path

The plugin will modify the path leading to the template files, adding at the beginning a directory corresponding to the requested language.

Thus, if the URL is /fr/article/list, the template used will be (by default) the file templates/fr/article/list.tpl

If the controller uses another file, the prefix will be added anyway.
For example, if the controller contains the following line:


Temma will use the file templates/fr/cms/content/article_list.tpl

Translation files

The plugin loads files containing the translations. These are files in INI format, placed in the etc/ directory of the project, and named according to the language (fr.ini, en.ini, etc.).

Here is an example of an fr.ini file:


title="Temma : framework PHP simple et performant"

Designed by="Conception par"

As you can see, the translated strings are sorted by sections (here default, header and footer). Here again, the basic strings are in English, and each one provides their translation into French.

Important: If you try to translate a string, but it cannot be found in the translation file, it will be used as is.
In the example above, we can see that the base strings are in English, and that a French translation has been provided. It will therefore not be necessary to provide an English translation file.

Use in templates

In Smarty templates, two additional variables are created by the plugin:

  • $lang: Contains the current language (fr, en, etc.).
  • $l10n: Contains the table of translations. Must not be used.

To translate strings, we will use the l10n modifier:



Displayed in French, this will give:


  • Lines 1 and 2: The strings listed in the default section of the translation file can be used directly.
  • Lines 4 and 5: Strings listed in other sections must be prefixed with the section name, followed by the hash character (#).
  • Les chaînes listées dans les autres sections doivent être préfixées avec le nom de la section,

Prefix to error files

When this plugin is used, it is necessary to translate the error files defined in the errorPages section of the temma.json file (see the documentation).

The paths defined in the configuration then take a prefix consisting of an error-pages directory, followed by a directory corresponding to the language used.

For example, for the following configuration file:

    "errorPages": {
        "404": "error404.html"

If we ask for a page that does not exist in French (for example the URL /fr/sdsjnzeoizoueh), Temma will look for the file www/ error-pages/fr/error404.html
If we ask for the same page in English (for example the URL /en/sdsjnzeoizoueh), Temma will look for the file www/error-pages/en/error404.html

Previous: Cache plugin
Next: Execution flow

Table of Contents