Sahana I18N/L10N

GNU Gettext

“The gettext functions implement an NLS (Native Language Support) API which can be used to internationalize your PHP applications”

Usage

gettext can use either the gettext() or _() function. In Sahana we use the _() function. (Even if you use gettext() it will still work)

 
e.g:
non localized
<title><?php print "DVR Home"; ?></title>

localization enabled
  - <title><?php print _("DVR Home"); ?></title>

Please remember to localize all the strings. It's very easy to localize in Sahana. example:

<?=_("Organization Name")?>

You can't use un-escaped double quotes (“”) inside of the string. This usually happens when you use single quotes (``) to enclose a string.

Try to always use double quotes to enclose strings and escape (\“) any other double quotes inside the string

e.g:
_('Home page "http://sahana.lk" ')
 
this should be,
_("Home page \"http://sahana.lk\" ")

If there are a number of quotation marks inside the gettext function you can follow the following coding convention

e.g:
_('To do this in the navigation bar click on "Multiple Incidents" followed by <a href="?mod=admin&act=ims_level1">
  "Manage Disaster"</a> and add the disaster name');
 
this should be,
 
_('To do this in the navigation bar click on ').'&#34;'._('Multiple Incidents').'&#34;'._(' followed by ').
'<a href="?mod=admin&act=ims_level1">'.'&#34;'._('Manage Disaster').'&#34;'.'</a> '._(' and add the disaster name.');

Above coding shows rather than using quotation marks you can use the html special character available for quotation mark, which is &#34; . Make sure to keep a space between words.

Don't localize variables. example: we don't want to see code similar to the following:

<?=_($org)?>

gettext Best Practices

  • 1. Please make sure gettext contain only word phrases.
e.g:
_('<h2>Welcome to the Sahana FOSS Disaster Management System</h2>');
 
this should be,
echo "<h2>"._('Welcome to the Sahana FOSS Disaster Management System')."</h2>";
  • 2. If there are html tags which we have to put within word phrase make sure to separate it from gettext. (see above example)
  • 3. When writing paragraph within gettext make sure line brakes are not there (In this case line breaks are behave in different ways in different editors for instance Eclipse put line break when you press enter but Qunata won't put line break but it'll wrap it).
e.g:
Eclipse behave as follows
_("The SAHANA Disaster Victim Registry is a central online repository " .
"where information on all the disaster victims can be stored. Information like name, age" .
", contact number, id card number, religion, race, displaced location, current location and other ")
 
This paragraph appears in sahana.po file in separate lines,not in a paragraph.Solution for Eclipse IDE users is  
writing the paragraph in same line.
 
Quanta will wrap this paragraph and appears in sahana.po file as a paragraph,which is allright :-) .
  • 4. Html special characters should separate from gettext.
  • 5. Beware of quotation marks, please put html special characters for quotation marks(Quotation marks are always bother developers :-))

Rule of thumb is to keep contain of gettext as simple as possible

Database Localization

Gettext framework doesn't support localization of strings stored in the database directly. Because of that Sahana uses a separate table named 'lc_fields'. This table holds the name of the table and field where the data in that field should be translated along with the Sahana web-interface translation.

The Structure of 'lc_fields'

id table field
1 rms_req_category category
2 rms_req_category description

The database fields that need to be translated in to other languages should be added to this table (lc_fields).

Database Localization Usage

To display a value retrieved from the database in some other language, the developer has to use use the function _lc(). This function takes a single string as the parameter and will return the translated value of that string. If a translation is not available this will return the same value.

_lc() function
[ string ] _lc ( $string )
example
    $sql = "SELECT * FROM rms_req_priority";
    $recordSet_prio = $global['db']->Execute($sql);

    $rs = $recordSet_prio->getArray();

    ....

    foreach($rs as $r)
    {
    ?>
    <tr>
        <td><?php print  $r['priority_id'] ?></td>
        <td><?php print  _lc($r['priority']) ?></td>
        <td><?php print  _lc($r['description']) ?></td>
    </tr>
    <?php
    }

This will display the translated values of Priority, Description.


More information on gettext:

Sahana Translation Web Interface

Sahana now has a web interface for the language translation using the GNU Gettext framework. Using this interface the user can [ extract ] the strings that needs to be translated and create the template file for the translation, add the translation to the PO file and compile the PO file to create the MO file. Using this interface the user can also add strings manually (Database values etc..) to be translated.

For details, see the User Docs on Activating a Locale & Translation into a new language.


Navigation
QR Code
QR Code dev:l10n (generated for current page)