CodeIgniter Forums

Full Version: Is it possible to load the whole folder of language files?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Let's assume i have created the language files and i want to load each and every one of them at once (with parent contstructor or so).
Note that each text line is checked to have a unique language_key.

Is this possible?

Thanks
This is interesting as I'm considering making my app language friendly, would be interested in hearing the options.
hooks\language_loader.php
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

function 
language_loader()
{
    
$ci =& get_instance();
    
$ci->load->helper('language');
    
// Set default language for system messages
    
$site_lang $ci->session->site_lang $ci->session->site_lang $ci->config->item('language');
    
$ci->config->set_item('language'$site_lang);
    
// What folder have we stored our files in
    
$custom_folder 'intranet';
    
// What language files do we have?
    
chdirAPPPATH 'language' DIRECTORY_SEPARATOR $site_lang DIRECTORY_SEPARATOR $custom_folder );
    
$language_files glob'*_lang.php'GLOB_NOSORT );
    
// Remove _lang.php and append custom folder
    
$language_files array_map( function($filename$custom_folder) {
        return 
$custom_folder DIRECTORY_SEPARATOR substr($filename,0,-9);
    }, 
$language_filesarray_fill(0count($language_files), $custom_folder) );
    
// Load langauge files
    
$ci->lang->load($language_files,$site_lang);


config/hooks.php
PHP Code:
$hook['post_controller_constructor'] = array(
 
   'function' => 'language_loader',
 
   'filename' => 'language_loader.php',
 
   'filepath' => 'hooks'
); 

config/config.php
PHP Code:
/*
|--------------------------------------------------------------------------
| Available Languages
|--------------------------------------------------------------------------
|
| language name => Display name
|
*/
$config['languages'] = array(
    
'english' => 'English',
    
'swedish' => 'Swedish'
); 

config/routes.php
PHP Code:
$route['language/(:any)'          'language/index/$1'


controllers/Language.php
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Language extends CI_Controller
{
 
   public function index($language '')
    {
 
       $site_lang in_array$languagearray_keys$this->config->item('languages') ) ) ? $language $this->config->item('language');
 
       $this->session->site_lang $site_lang;
 
       
        redirect
$this->agent->referrer() );
 
   }


helpers/security_helper.php
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

if ( ! 
function_exists('safe_anchor'))
{
    
/**
     * Safe Anchor Link
     *
     * Creates an anchor based on the local URL.
     *
     * @param    string    the URL
     * @param   string  append id
     * @param    string    the link title
     * @param    mixed    any attributes
     * @return    string
     */
    
function safe_anchor($uri ''$id ''$title ''$attributes '')
    {
        
$id    = (array)  $id;
        
$title = (string) $title;
        
$ids   = array();
        
        if( !empty(
$id) )
        {
            foreach(
$id as $value)
            {
                if( !empty(
$value) )
                {
                    
$ids[] = urlencode($value);
                }
            }
        }
        
        if( !empty(
$ids) )
        {
            
$uri .= '/' implode('/',$ids);
        }
        
        if(
$title !== '')
        {
            
$title html_escape($title);
        }
        
        return 
anchor($uri$title$attributes);
    }
}

if ( ! 
function_exists('icon_anchor'))
{
    
    
/**
     * Icon Anchor Link
     *
     * Creates an anchor based on the local URL.
     *
     * @param    string    the URL
     * @param   string  append id
     * @param    string    the link title
     * @param    mixed    any attributes
     * @return    string
     */
    
function icon_anchor($uri ''$id ''$title ''$attributes '')
    {
        
$id    = (array)  $id;
        
$title = (string) $title;
        
$ids   = array();
        
        if( !empty(
$id) )
        {
            foreach(
$id as $value)
            {
                if( !empty(
$value) )
                {
                    
$ids[] = urlencode($value);
                }
            }
        }
        
        if( !empty(
$ids) )
        {
            
$uri .= '/' implode('/',$ids);
        }
        
        return 
anchor($uri$title$attributes);
    }


config/autoload.php
PHP Code:
/*
| -------------------------------------------------------------------
|  Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
|    $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('security'); 

In your view (with bootstrap and font-awesome 3.7):
PHP Code:
<div class="dropdown pull-right">
    <
a href="#" data-toggle="dropdown" class="dropdown-toggle"><class="fa fa-globe" aria-hidden="true"></i> <class="caret"></b></a>
    <
ul class="dropdown-menu">
        <?
php
        
foreach( $this->config->item('languages') as $lang => $name )
        {
            if( 
$this->config->item('language') == $lang )
            {
                continue;
            }
            echo 
'<li>'safe_anchor('language'$lang$name) . '</li>';
        }
        
?>
    </ul>   
</div>