Welcome Guest, Not a member yet? Register   Sign In
Improvement of lang() function
#1

(This post was last modified: 04-14-2016, 11:53 PM by gxgpet. Edit Reason: fixed typo )

Hello!

I'm using a modified version of line() function from system/core/Lang.php, which also deals with numeric parameters within lang lines:

PHP Code:
    public function line($line$log_errors TRUE$parameters = [])
    {
        
$value = isset($this->language[$line]) ? $this->language[$line] : FALSE;

        
// Because killer robots like unicorns!
        
if ($value === FALSE && $log_errors === TRUE)
        {
            
log_message('error''Could not find the language line "'.$line.'"');
        }
        else
        {
            
$i 0;

            foreach (
$parameters as $parameter) {

                
$value str_replace("{" $i "}"$parameter$value);

                
$i++;
            }
        }

        return 
$value;
    } 

In your lang file, you can use it as:

PHP Code:
$lang['greeting'] = "Hello, {0}!"

When outputting the line:

PHP Code:
$username 'John Doe';
echo 
$this->lang->line('greeting'TRUEcompact('username')); // Hello, John Doe! 

I wanted to share it with you, maybe you could add it to CodeIgniter as well  Big Grin Hope this helps!
Reply
#2

Code:
// lang file:
$lang['greeting'] = "Hello, %s";

// Usage
echo sprintf($this->lang->line('greeting'), "John Doe");

We don't add features that are easily accessible through PHP already.
Reply
#3

(04-15-2016, 12:53 AM)Narf Wrote:
Code:
// lang file:
$lang['greeting'] = "Hello, %s";

// Usage
echo sprintf($this->lang->line('greeting'), "John Doe");

We don't add features that are easily accessible through PHP already.

I get your point, but I don't see any reasons why would you output in a view in some places lang() and in some places sprintf(lang()). This would make inconsistencies across the application.

Also, by adding this into line() function, it could lead to further and richer features, such as singular / plural selection:

PHP Code:
$lang['unread_captation'] = 'You have {0|s:unread message|p:unread messages}.';

// ...

$messages 2;
echo 
$this->lang->line('unread_captation'TRUEcompact('messages')); // will output: You have 2 unread messages. 

Big Grin
Reply
#4

(04-15-2016, 02:03 AM)gxgpet Wrote:
(04-15-2016, 12:53 AM)Narf Wrote:
Code:
// lang file:
$lang['greeting'] = "Hello, %s";

// Usage
echo sprintf($this->lang->line('greeting'), "John Doe");

We don't add features that are easily accessible through PHP already.

I get your point, but I don't see any reasons why would you output in a view in some places lang() and in some places sprintf(lang()). This would make inconsistencies across the application.

And I can easily turn it around and say that it is inconsistent to write lang('foo') in some places and lang('foo', TRUE, array('bar')) in other places ... It's a silly argument no matter which one you prefer.

The difference, and what you can't deny, is that what you call "inconsistent" is explicit and unambiguous - two qualities that are 100% positive.

(04-15-2016, 02:03 AM)gxgpet Wrote: Also, by adding this into line() function, it could lead to further and richer features, such as singular / plural selection:

PHP Code:
$lang['unread_captation'] = 'You have {0|s:unread message|p:unread messages}.';

// ...

$messages 2;
echo 
$this->lang->line('unread_captation'TRUEcompact('messages')); // will output: You have 2 unread messages. 

Big Grin

Can != Will
And in this case - won't.

P.S.: compact() is a horrible function
Reply
#5

(04-15-2016, 03:36 AM)Narf Wrote:
(04-15-2016, 02:03 AM)gxgpet Wrote:
(04-15-2016, 12:53 AM)Narf Wrote:
Code:
// lang file:
$lang['greeting'] = "Hello, %s";

// Usage
echo sprintf($this->lang->line('greeting'), "John Doe");

We don't add features that are easily accessible through PHP already.

I get your point, but I don't see any reasons why would you output in a view in some places lang() and in some places sprintf(lang()). This would make inconsistencies across the application.

And I can easily turn it around and say that it is inconsistent to write lang('foo') in some places and lang('foo', TRUE, array('bar')) in other places ... It's a silly argument no matter which one you prefer.

The difference, and what you can't deny, is that what you call "inconsistent" is explicit and unambiguous - two qualities that are 100% positive.

(04-15-2016, 02:03 AM)gxgpet Wrote: Also, by adding this into line() function, it could lead to further and richer features, such as singular / plural selection:

PHP Code:
$lang['unread_captation'] = 'You have {0|s:unread message|p:unread messages}.';

// ...

$messages 2;
echo 
$this->lang->line('unread_captation'TRUEcompact('messages')); // will output: You have 2 unread messages. 

Big Grin

Can != Will
And in this case - won't.

P.S.: compact() is a horrible function
OK, if you say so.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB