Welcome Guest, Not a member yet? Register   Sign In
better language library
#1

[eluser]Tom Vogt[/eluser]
I've done a small extension to the language library, to allow for easier replacement strings.

Namely, this allows you to do either of this:
Code:
echo $this->lang->line('teststring', array('first', 'second'));
echo $this->lang->line('teststring2', array('one'=>'first', 'two'=>'second'));

where the language file looks like this:
Code:
$lang['teststring']    = 'This is a teststring with two replacements: [1] and [2].';
$lang['teststring2']    = 'This is a teststring with two replacements: [one] and [two].';

I wanted this for two reasons:
1.) The %s replacement doesn't allow for re-ordering, but some languages have different sentence structures and would require that or awkword wordings.
2.) because it makes it easier for translators if there is a meaningful placeholder instead of a %s

Note that the placeholder should NOT be translated, even in other language versions it must remain, in the above example, [one] and [two]


Here's the code, it is really simple. Save this in system/application/libraries/MY_Language.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Language extends CI_Language {

    public function __construct() {
        parent::__construct();
    }

    function line($line = '', $vars = array()) {
        $line = ($line == '' OR ! isset($this->language[$line])) ? FALSE : $this->language[$line];
            foreach ($vars as $key=>$val) {
                if (is_int($key)) $key++;
                $line = str_replace("[{$key}]", $val, $line);
            }
        return $line;
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB