Welcome Guest, Not a member yet? Register   Sign In
using config variables in language files
#1

[eluser]jbowman[/eluser]
I have a working solution for this, but something tells me it's not the best solution and I'm creating too much overhead. So I thought I'd throw it out there and see if anyone else has a better solution.

Here's the situation. I'm creating my own user auth system. Yea I know there's plenty out there, none quite fit what I wanted, and I needed something that was adaptable for several sites I want to build.

One thing I am doing is age verification on registration. However, some of the sites have different requirements. So.. I create a new config file site_config.php and have it autoloaded. It includes

Code:
$config['min_age']    = 14;

I use formdate, and some playing with post variables afterwards to get a date string that I pass to my validation library (MY_validation.php, extending the CI one) through this function

Code:
/**
     * validate_age
     *
     * Used for age validation. Requires $config['min_age'] to be set.
     *
     * @access    public
     * @param string
     * @return    bool
     */    
    
    function validate_age($date)
    {
        $CI =& get_instance();
        list($year, $month, $day) = split('-', $date);
        $age = date("Y") - $year;
        if(($month > date("m")) || ($month == date("m") && date("d") < $day))
        {
                $age -= 1;
        }
        if ( $age < $CI->config->item('min_age') )
        {
            return false;            
        } else {
            return true;
        }
    }

Now if that returns false, it goes to my language file (which I'm going to be following this post up with another one to ask a question about that :) ) where I have

Code:
$lang['validate_age'] = 'You must be ' . get_instance()->config->item('min_age') . ' or older to complete this request.';

That get_instance is creating a whole new block of memory instead of referencing the existing one, isn't it? Is there a better way to handle this?
#2

[eluser]maffo[/eluser]
Did you ever find a solution for this? Might save me some time if it has been done before as I need exactly the same functionality.
#3

[eluser]smith[/eluser]
Code:
$this->config->item['min_age'];
#4

[eluser]maffo[/eluser]
If only it were that easy my friend, you cant use config or session data within the language files.
#5

[eluser]smith[/eluser]
Yes, i didn't read carefuly Smile
#6

[eluser]Armchair Samurai[/eluser]
Considering the original poster posted this about three months ago, I'm assuming they've found a solution to this problem. But since the thread was pulled from its grave like a member of the undead, this works for me:

Try using the pseudo-variables. You'd set your variables in the language files to
Code:
$lang['validate_age'] = 'You must be {age} or older to complete this request.';

After that, you could write a very simple parser (if you need it constantly, make it a helper or plugin that you can autoload) to replace the variable:
Code:
function parse_line($var, $config_item, $lang_line)
{
/*
   I'm assuming you've already defined your language - if not modify code as needed
*/
     $var = "{$var}";
     $line = $this->lang->line($lang_line);

     return str_replace($var, $this->config->item($config_item), $lang_line);
}
So when your error is called, you'd do something like:
Code:
$foo = parse_line('age', 'min_age', 'validate_age');
#7

[eluser]maffo[/eluser]
Thanks for that. Will have a good play with it. ;-)
#8

[eluser]Arjen van Bochoven[/eluser]
Why not use print_f:

Code:
$format = 'There are %d monkeys in the %s';
printf($format, $num, $location);

http://nl3.php.net/manual/en/function.sprintf.php

Arjen
#9

[eluser]Armchair Samurai[/eluser]
[quote author="Arjen van Bochoven" date="1192238367"]Why not use print_f:[/quote]
Why not, indeed? I wasn't even aware of that function 'til now....

Thanks for that. It'd be too much work to clean up some past sites, but I'll be keeping this in mind for the future.
#10

[eluser]Phil Sturgeon[/eluser]
You mean sprintf() surely? print_f() would have little use in a CI setup.




Theme © iAndrew 2016 - Forum software by © MyBB