Part of the EllisLab Network
   
1 of 3
1
Language Parser Helper
Posted: 08 April 2008 07:31 AM   [ Ignore ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  2634
Joined  06-10-2007

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.

<?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://codeigniter.com/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;
        
}
    }
    
}
 Signature 

URI Language Identifier | Modular Extensions - PHP5 | Modular Separation - PHP5 | Widget plugin | Access Control library

Profile
 
 
Posted: 08 April 2008 07:39 AM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  2634
Joined  06-10-2007

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.

 Signature 

URI Language Identifier | Modular Extensions - PHP5 | Modular Separation - PHP5 | Widget plugin | Access Control library

Profile
 
 
Posted: 08 April 2008 07:42 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  754
Joined  01-13-2008

Looks good smile, the update will speed up the script.

 Signature 

Yonti - I am Currently looking for a business partner, to create the best developer hosting out there. If your interested PM me.

Fluxity Lighting - My other company.


I’m a Proud Supporter and Sponser of Tomorrows Web.

Profile
 
 
Posted: 08 April 2008 07:53 AM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  2634
Joined  06-10-2007

It may be of use to pass in the language name to parse with and have the helper load that language file.

class Language
{
    
function parse($view, $data = array(), $lang = 'english')
    
{
        $this
->load->language($lang);
    ....

EDIT
Update #1 = add language name to load

 Signature 

URI Language Identifier | Modular Extensions - PHP5 | Modular Separation - PHP5 | Widget plugin | Access Control library

Profile
 
 
Posted: 08 April 2008 07:55 AM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  754
Joined  01-13-2008

Nice Update, it does now work 1.5x times faster on my LAMP server, very use full indeed.

 Signature 

Yonti - I am Currently looking for a business partner, to create the best developer hosting out there. If your interested PM me.

Fluxity Lighting - My other company.


I’m a Proud Supporter and Sponser of Tomorrows Web.

Profile
 
 
Posted: 08 April 2008 07:59 AM   [ Ignore ]   [ # 5 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

If i understand it correct you can code something like

// template.php
<h1>{pagetitle}</h1>
<?php foreach($blogitems as $item){ ?>
<div class="item">
<
h2><?php echo $item['title']; ?></h2>
<
p>{postedby} <?php echo $item['author']; ?></p>
<?php echo $item['content']; ?>
<?php } ?>
// controller
function blog()
{
    $data[
'blogitems'] = $this->blog->last_entries();
    return
language::parse('template', $data);
}
Profile
 
 
Posted: 08 April 2008 08:03 AM   [ Ignore ]   [ # 6 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  2634
Joined  06-10-2007

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

 Signature 

URI Language Identifier | Modular Extensions - PHP5 | Modular Separation - PHP5 | Widget plugin | Access Control library

Profile
 
 
Posted: 08 April 2008 08:17 AM   [ Ignore ]   [ # 7 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

I find it strange you don’t have to add files to the load->language method

function language($file = array(), $lang = '')
    
{
        $CI
=& get_instance();

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

        
foreach ($file as $langfile)
        
{    
            $CI
->lang->load($langfile, $lang);
        
}
    }
Profile
 
 
Posted: 08 April 2008 08:22 AM   [ Ignore ]   [ # 8 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  2634
Joined  06-10-2007

What is strange about it xwero? If $file is not an array it is converted to an array

 Signature 

URI Language Identifier | Modular Extensions - PHP5 | Modular Separation - PHP5 | Widget plugin | Access Control library

Profile
 
 
Posted: 08 April 2008 08:29 AM   [ Ignore ]   [ # 9 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

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.

Profile
 
 
Posted: 08 April 2008 08:39 AM   [ Ignore ]   [ # 10 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  2634
Joined  06-10-2007

Ok, but lang->load will use $config[‘language’] if none is supplied, I guess you see that.

Is there something I need to change in the helper class?

 Signature 

URI Language Identifier | Modular Extensions - PHP5 | Modular Separation - PHP5 | Widget plugin | Access Control library

Profile
 
 
   
1 of 3
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120466 Total Logged-in Users: 30
Total Topics: 126544 Total Anonymous Users: 4
Total Replies: 665387 Total Guests: 286
Total Posts: 791931    
Members ( View Memberlist )
Newest Members:  DistrikerGerd Pansenneilgroomart.maniaakoloans5kmotion1jelevinlizandermagnosismx2428