CodeIgniter Forums
Language Parser Helper - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Language Parser Helper (/showthread.php?tid=7414)

Pages: 1 2 3


Language Parser Helper - El Forum - 04-08-2008

[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;
        }
    }
    
}



Language Parser Helper - El Forum - 04-08-2008

[eluser]wiredesignz[/eluser]
On a second look much of the code wasn't needed. Should work a bit faster now.

Please post your comments, Thanks for your interest as always.


Language Parser Helper - El Forum - 04-08-2008

[eluser]Tom Glover[/eluser]
Looks good Smile, the update will speed up the script.


Language Parser Helper - El Forum - 04-08-2008

[eluser]wiredesignz[/eluser]
It may be of use to pass in the language name to parse with and have the helper load that language file.
Code:
class Language
{
    function parse($view, $data = array(), $lang = 'english')
    {
        $this->load->language($lang);
    ....

EDIT
Update #1 = add language name to load


Language Parser Helper - El Forum - 04-08-2008

[eluser]Tom Glover[/eluser]
Nice Update, it does now work 1.5x times faster on my LAMP server, very use full indeed.


Language Parser Helper - El Forum - 04-08-2008

[eluser]xwero[/eluser]
If i understand it correct you can code something like
Code:
// template.php
<h1>{pagetitle}</h1>
&lt;?php foreach($blogitems as $item){ ?&gt;
<div class="item">
<h2>&lt;?php echo $item['title']; ?&gt;</h2>
<p>{postedby} &lt;?php echo $item['author']; ?&gt;</p>
&lt;?php echo $item['content']; ?&gt;
&lt;?php } ?&gt;
// controller
function blog()
{
    $data['blogitems'] = $this->blog->last_entries();
    return language::parse('template', $data);
}



Language Parser Helper - El Forum - 04-08-2008

[eluser]wiredesignz[/eluser]
Yes, except I used {?pagetitle}, but really after looking at your code, the ? is not even needed. Thanks xwero Wink

Update #2 = remove ?

Of course the view can be echo'd or appended to $this->output as required


Language Parser Helper - El Forum - 04-08-2008

[eluser]xwero[/eluser]
I find it strange you don't have to add files to the load->language method
Code:
function language($file = array(), $lang = '')
    {
        $CI =& get_instance();

        if ( ! is_array($file))
        {
            $file = array($file);
        }

        foreach ($file as $langfile)
        {    
            $CI->lang->load($langfile, $lang);
        }
    }



Language Parser Helper - El Forum - 04-08-2008

[eluser]wiredesignz[/eluser]
What is strange about it xwero? If $file is not an array it is converted to an array


Language Parser Helper - El Forum - 04-08-2008

[eluser]xwero[/eluser]
As you can see the method makes use of the lang->load method which is the method that gets the language file which provides the line but because there is not file it should throw an error or you should have a /language/english/english.php file.

The language is the second parameter.