[eluser]wiredesignz[/eluser]
I originally developed this helper class to allow functions to be parsed in views, but in a fit of brilliance I realise it could also solve a lot of issues for people using languages in their views.
Please note this is completely untested, but may be beneficial to someone.
Please try it and modify it as needed, your interest could benefit the CI community.
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Language Parser Helper Class
*
* Adapted from the CodeIgniter Parser Class
* @copyright Copyright (c) 2006, EllisLab, Inc.
* @license http://ellislab.com/codeigniter/user-guide/license.html
*
* Allows the use of languages in parsed templates ie:
* {welcome} = $this->lang->line("welcome")
*
* Place this file in application/helpers as language_helper.php
* and load as needed.
*
* Usage:
* $view = language::parse($view, $data, $language_files, $language);
*
* Version 0.5 (c) Wiredesignz 2008-04-08
**/
class Language
{
function parse($view, $data = array(), $lang_files = array(), $lang = 'english')
{
$this->load->language($lang_files, $lang); // added by xwero
if ($template = $this->load->view($view, $data, TRUE)) //build the view normally
{
while(preg_match('/\{(\w*)\}/siU', $template, $match)) //parse the language variables
{
//if no translation is found use the variable as a literal
if (($line = $this->lang->line("$match[1]")) === FALSE) $line = $match[1];
$template = str_replace($match[0], $line, $template);
}
return $template;
}
}
}