LangBuilder

Derek Jones edited this page Jul 5, 2012 · 12 revisions
Clone this wiki locally

Category:Libraries::Language

Language Files Builder

This utility class eases the process of translation management for CI language files. The goal is to centralize all translated items in one CSV file. This allows to keep a consistent baseline for translated data and to keep track (thanks to OpenOffice) of those who need to be translated (the blank columns) after a huge amount of change/improvement in your multilingual views.

The data could be retrieved and organized as needed from a CSV file through the use of the CSVReader library.

Here is an example of CSV file that I use:

line;français;english
Checkout;Commander;Checkout
Remove item;Retirer l'article;Remove item

The first column name (line) must be strictly respected.

Note

You have probably noticed that I do not prefix item names as recommended in the language manual page and that I extensively use spaces. In case of missing definition, it allows to display an understandable (I know this is controversial and localization dependent) alternative to the user through the direct use of the item name and thanks to a dedicated helper. Here is the helper:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Internationalization helper
 * 
 * $Id: i18n_helper.php 193 2008-01-12 00:37:41Z Pierre-Jean $
 *  
 * @author        Pierre-Jean Turpeau
 * @link        n/a
 */
function i18n($p_Item, $p_Param = null) {
    $CI =& get_instance();
    $line = $CI->lang->line($p_Item);
    if( $line == null ) {
        log_message('error', 'i18n: unknown item ['.$p_Item.'] in '.$_SERVER['REQUEST_URI']);
        $line = $p_Item;
    }
    if( $p_Param != null ) {
        if( is_array($p_Param) ) {
            return vsprintf($line, $p_Param);
        } else {
            return sprintf($line, $p_Param);
        }
    } else {
        return $line;
    }
}
?>

Requirements

  • CI: tested on 1.5.3
  • PHP: tested on 5.2.1

The library code

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * LangBuilder Class
 * 
 * $Id: langbuilder.php 140 2007-06-03 22:12:19Z Pierre-Jean $
 * 
 * Build languages files from languages data as needed in the language/
 * sub directories. 
 * 
 * The following data organization is assumed:
 * 
 *         Array(
 *             [0] => Array(
 *                     [line] => Checkout
 *                     [français] => Commander
 *                     [english] => Checkout
 *             )
 *             [1] => Array(
 *                     [line] => Remove item
 *                     [français] => Retirer l'article
 *                     [english] => Remove item
 *             )
 *         )
 * 
 * It will produce the following files:
 * 
 *    <language_directory>/français/generated_lang.php
 *         &lt;?php
 *        $lang['Checkout'] = 'Commander';
 *        $lang['Remove item'] = 'Retirer l&rsquo;rticle';
 *         ?&gt;
 * 
 *    <language_directory>/english/generated_lang.php
 *         &lt;?php
 *        $lang['Checkout'] = 'Checkout';
 *        $lang['Remove item'] = 'Remove item';
 *         ?&gt;
 *  
 * @author        Pierre-Jean Turpeau
 * @link        http://www.codeigniter.com/wiki/LangBuilder
 */
class LangBuilder {
    
    /**
     * Build the language files from the specified data.
     * 
     * @param array
     * @param string
     * @param string
     * @return void 
     */
    function build($p_Data, $p_LanguageDirectoryPath, $p_TargetName = 'generated' ) {
        $CI =& get_instance();

        $php = array();
        
        foreach( $p_Data as $item ) {
            foreach( $item as $lang => $value ) {
                if( $lang != 'line' ) { // skip the 'line' column
                    if( !isset($php[$lang]) ) {
                        $php[$lang] = '';
                    }                
                    
                    if( trim($value) != '' ) {
                        $php[$lang] .= "\$lang['".trim($item['line'])."'] = '".preg_replace('/\'/', '&rsquo;', preg_replace('/"/', '&quot;', trim($value)))."';\n";
                    }
                }
            }
        }
        
        foreach( $php as $lang => $text ) {
            $filename = $p_LanguageDirectoryPath.'/'.trim($lang).'/'.$p_TargetName.'_lang.php';
            $f = fopen&#40;$filename, 'w'&#41;;
            fwrite($f, "&lt;?php\n");
            fwrite($f, $text);
            fwrite($f, "?&gt;\n");
            fclose($f);
        }
    }    
}
?&gt;