Help Tips « back
Internationalization with PHP2Go


Since the first beta release, the error messages and GUI elements used by the framework were already using a language table loaded by the framework and based on the language chosen by the user in the global configuration array ($P2G_USER_CFG['LANGUAGE']). The LanguageBase class used to play the main role in the internationalization process, while the static method getLangVal, inside the PHP2Go class, was the best way to retrieve messages from this single language table.

Since PHP2Go 0.2.7, the language definitions of the global configuration array have a lot of new features.

a) Configuration settings were improved

1) When a language is choosen, the locale of the entire application is changed, using the PHP function setlocale;

2) The AVAILABLE configuration key accepts an array of language codes. Here you can provide the subset of the languages supported by PHP2Go that you want to offer to you application's users. For example: PHP2Go supports N languages, but all that you want is a website in English and French. So:
<?php
    $P2G_LANGUAGE
['AVAILABLE'] = array('en-us', 'fr-fr');
?>


3) The AUTO_DETECT configuration key allows the framework to detect the user language from the request headers. The detected locale identifier is compared with the available languages (AVAILABLE, if exists, or the complete list of languages supported by PHP2Go). If the detected language is not found in one of this arrays, the DEFAULT configuration entry will be used. Finally, if the DEFAULT key is missing, US english will be the final choice;

4) The REQUEST_PARAM configuration key is the name of the request parameter that will be used by the application to dinamically change the current language. The most simple implementation is to show a list of choices (language codes): when the user chooses one of the options, a page is loaded containing the language code as a GET or POST parameter. Doing this, the language of the application will be changed and stored in the session scope and in a cookie. Then, the next time the user enters the application, the previous language choice will be loaded and preserved.

5) Below, a complete example of how to define all the internationalization settings:
<?php
    
// complete example of language settings
    
$P2G_USER_CFG['LANGUAGE'] = array(
        
// default choice when the provided or auto detected lang is missing or invalid
        
'DEFAULT' => 'en-us',                        
        
// means that our application only supports english and german languages
        
// if omitted, all languages supported by PHP2Go will be assumed as available
        
'AVAILABLE' => array('en-us', 'de-de'),        
        
// enable auto detection based on the request headers
        
'AUTO_DETECT' => TRUE,                        
        
// enable dynamic language change based on the request (POST or GET)
        
'REQUEST_PARAM' => 'lang',                    
        
// stores the path of the user language domains
        
'MESSAGES_PATH' => '/www/myapp/locale/'        
    
);
    
// complete list of language codes supported by PHP2Go:
    // pt-br, en-us, es, fr-fr, de-de, it, cs
);
?>


b) The user can define his own language tables, organized in "domains"

1) First, it's necessary to define a folder where the language tables will be stored. The full path for this folder must be set in the global configuration:
<?php
    $P2G_LANGUAGE
['LANGUAGE']['MESSAGES_PATH'] = '/path/to/the/language/files/';
?>


2) Inside this folder, you must create a folder for each language you want to support in your application. The name of the folders must combine with the language codes used by the framework;

3) You must define one or more "language domains". If you're working on a simple application, only one domain will be necessary. But in a larger system, dividing the message resources in "types" or "categories" is a good practice. Once the domains are defined, each one will represent a file, and each language folder must have a copy of the domain language file. For example: if you have a single domain called "MESSAGES", you must create a file called MESSAGES.php (case sensitive) inside each language folder.

4) The message resources must be defined as an associative array, where the key will be used to retrieve the message and the text is the full localized message. In the end of the domain language file, you must place a return statement for the created array, so that the framework can treat the return of an include operation as an array.
<?php
    
// my_language_folder/en-us/MESSAGES.php
    
$messages = array(
        
'HELLO_WORLD' => 'Hello World!'
    
);
    return
$messages;
?>

5) The associative array can contain multiple dimensions, just like the example below:
<?php
    
// my_language_folder/pt-br/MESSAGES.php
    
$messages = array(
        
'FORM_LABELS' => array(
            
'NAME' => 'Nome',
            
'ADDRESS' => 'Endereço'
        
)
    )
);
?>


c) The developer can query his own language tables

1) Using a static method of the PHP2Go class: PHP2Go::getLangVal("DOMAIN:MESSAGE_KEY");

2) Inside any template (Template and DocumentElement), using the pattern: #i18n:DOMAIN:MESSAGE_KEY#. When using cache in the templates, the internationalization query results will be included in the cache content, because they are built before the template parsing;

3) Inside the forms XML specification, using the same pattern, in a reserved set of attributes:

3.1) *.LABEL
3.2) CHECKFIELD.CAPTION
3.3) LOOKUPFIELD.FIRST
3.4) COMBOFIELD.FIRST
3.5) BUTTON.VALUE
3.6) SECTION.NAME
3.7) RANGEFIELD.SURROUNDTEXT
3.8) RANGEFIELD.RULEMESSAGE
3.9) RULE.MESSAGE

4) When querying language tables with multiple dimensions, each dimension represent a "dot":
4.1) PHP2Go::getLangVal("DOMAIN:FORM_LABELS.NAME");
4.2) #i18n:DOMAIN:FORM_LABELS.NAME#