-
Notifications
You must be signed in to change notification settings - Fork 4
LangBuilder
[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 (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 [url=http://www.codeigniter.com/Wiki/CSVReader]CSVReader[/url] library.
Here is an example of CSV file that I use:
[code]line;français;english Checkout;Commander;Checkout Remove item;Retirer l'article;Remove item[/code]
The first column name ([em]line[/em]) must be strictly respected.
[h3]Note[/h3]
You have probably noticed that I do not prefix item names as recommended in the [url=http://www.codeigniter.com/user_guide/libraries/language.html]language[/url] manual page and that I extensively use spaces. In case of missing definition, it allows to display an [em]understandable[/em] (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:
[code]<?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; } } ?>[/code]
[h3]Requirements[/h3]
- CI: tested on 1.5.3
- PHP: tested on 5.2.1
[h3]The library 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
-
<?php
-
$lang['Checkout'] = 'Commander';
-
$lang['Remove item'] = 'Retirer l’rticle';
-
?>
-
<language_directory>/english/generated_lang.php
-
<?php
-
$lang['Checkout'] = 'Checkout';
-
$lang['Remove item'] = 'Remove item';
-
?>
-
@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('/\'/', '’', preg_replace('/"/', '"', 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]
-