Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 12 revisions

[category:libraries::language]

[h3]Language Files Builder[/h3]

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 of those who need to be translated (the blank columns) after a huge amount of change/improvement in the multilingual views.

The CSV formatted data could be retrieve through the use of the [url=http://www.codeigniter.com/Wiki/CSVReader]CSVReader[/url]

[h3]Requirements[/h3]

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

[h3]The code[/h3]

[code]<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /**

  • LangBuilder Class

  • $Id: langbuilder.php 138 2007-05-30 16:05:59Z 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($filename, 'w'); fwrite($f, "<?php\n"); fwrite($f, $text); fwrite($f, "?>\n"); fclose($f); } }
      } ?> [/code]

Clone this wiki locally