• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Access Database in Core class

#1
I'm trying to make a site multilanguage.
We use diffent .json files with the translation of every term according to de language wich can be the set in a cookie or can be the default language.

I changed .htaccess file so when we get something like http://siteurl/en/controller/action it would rewrite to http://siteurl/controller/action?__lang=en, this works perfectly

The problem appeared when i had to set the new base_url, because the available languages can be eddited by an administrator, so I proceed to create a new controller in the application/core folder in wich i changed the base_url with the cookie value or the default language

But i had to use a mysqli object, write user and password again, and I didnt like that but I cant use the DB because core classes instances are created long before $CI is created so no "instance" is available for &get_instance().

Any help or idea to make this cleaner?

Heres my code:

PHP Code:
class MY_Config extends CI_Config {

    public function __construct() {

        $this->config =& get_config();
        $language_code '';
        if(isset($_COOKIE['language']))
        {
            $conn = new mysqli("localhost","root","root","test");
            $stmt $conn->prepare("select * from language l where l.code = ?");
            $stmt->bind_param("s"$code);
            $code $_COOKIE['language'];
            $stmt->execute();
            $result $stmt->get_result();
            
            
if($result->num_rows == 1)
                $language_code $_COOKIE['language'];
        }
        
        
if($language_code == '')
        {
            $conn = new mysqli("localhost","root","root","test");
            $stmt $conn->prepare("select s.value from settings s where s.key = ?");
            $stmt->bind_param("s"$lang);
            $lang 'language';
            $stmt->execute();
            $result $stmt->get_result();
            if($result->num_rows == 1)
                $language_code $result->fetch_assoc()['value'];
        }

        if($language_code != '')        
            $this
->set_item('base_url'$this->config['base_url'] . $language_code '/');
    }

Reply

#2
You should use hooks for this:
https://codeigniter.com/user_guide/general/hooks.html

And load it with pre_controller.

Do you have translations for system messages or are all of them in English?
You should consider setting config "language" in case you want to translate them.
Reply

#3
(01-05-2020, 07:48 AM)LPena Wrote: I'm trying to make a site multilanguage.
We use diffent .json files with the translation of every term according to de language wich can be the set in a cookie or can be the default language.

I changed .htaccess file so when we get something like http://siteurl/en/controller/action it would rewrite to http://siteurl/controller/action?__lang=en, this works perfectly

The problem appeared when i had to set the new base_url, because the available languages can be eddited by an administrator, krogerfeedback so I proceed to create a new controller in the application/core folder in wich i changed the base_url with the cookie value or the default language

But i had to use a mysqli object, write user and password again, and I didnt like that but I cant use the DB because core classes instances are created long before $CI is created so no "instance" is available for &get_instance().

Any help or idea to make this cleaner?

Heres my code:

PHP Code:
class MY_Config extends CI_Config {

    public function __construct() {

        $this->config =& get_config();
        $language_code '';
        if(isset($_COOKIE['language']))
        {
            $conn = new mysqli("localhost","root","root","test");
            $stmt $conn->prepare("select * from language l where l.code = ?");
            $stmt->bind_param("s"$code);
            $code $_COOKIE['language'];
            $stmt->execute();
            $result $stmt->get_result();
            
            
if($result->num_rows == 1)
                $language_code $_COOKIE['language'];
        }
        
        
if($language_code == '')
        {
            $conn = new mysqli("localhost","root","root","test");
            $stmt $conn->prepare("select s.value from settings s where s.key = ?");
            $stmt->bind_param("s"$lang);
            $lang 'language';
            $stmt->execute();
            $result $stmt->get_result();
            if($result->num_rows == 1)
                $language_code $result->fetch_assoc()['value'];
        }

        if($language_code != '')        
            $this
->set_item('base_url'$this->config['base_url'] . $language_code '/');
    }

I have also faced same issue. Did you really find solution??
Reply

#4
Like @jrklund said a hook could solve this issue.

But I am wondering why you are using JSON and htacces for this? Is there a specific reason for it? If not you could easyly use the pre build language files section of CI. Then you just need to log your user language in the session and set a standard language.

In config.php :: $config['language'] = 'english';

And then add your language files in the system.

example


PHP Code:
$lang["hashtag"] = "#";
$lang["thumb"] = "Thumb"

And then to use it is as simple as

PHP Code:
<?php echo $this->lang->line('hashtag'); ?>

I am honestly wondering why you are over complicating things with json and htacces ...
Reply

#5
(01-09-2020, 03:32 PM)ZoeF Wrote: Like @jrklund said a hook could solve this issue.

But I am wondering why you are using JSON and htacces for this? Is there a specific reason for it? If not you could easyly use the bday.info pre build language files section of CI. Then you just need to log your user language in the session and set a standard language.

In config.php :: $config['language'] = 'english';

And then add your language files in the system.

example


PHP Code:
$lang["hashtag"] = "#";
$lang["thumb"] = "Thumb"

And then to use it is as simple as

PHP Code:
<?php echo $this->lang->line('hashtag'); ?>

I am honestly wondering why you are over complicating things with json and htacces ...

same prob...
Reply

#6
(01-06-2020, 10:21 AM)keklein Wrote:
(01-05-2020, 07:48 AM)LPena Wrote: I'm trying to make a site multilanguage.
We use diffent .json files with the translation of every term according to de language wich can be the set in a cookie or can be the default language.

I changed .htaccess file so when we get something like http://siteurl/en/controller/action it would rewrite to http://siteurl/controller/action?__lang=en, this works perfectly

The problem appeared when i had to set the new base_url, because the available languages can be eddited by an administrator, so I proceed to create a new controller in the application/core folder in wich i changed the base_url with the cookie value or the default language

But i had to use a mysqli object, write user and password again, and I didnt like that but I cant use the DB because core classes instances are created long before $CI is created so no "instance" is available for &get_instance().

hey, have you done it successfully, i'm working on client site and struggling to do it? does this work?

Any help or idea to make this cleaner?

Heres my code:

PHP Code:
class MY_Config extends CI_Config {

    public function __construct() {

        $this->config =& get_config();
        $language_code '';
        if(isset($_COOKIE['language']))
        {
            $conn = new mysqli("localhost","root","root","test");
            $stmt $conn->prepare("select * from language l where l.code = ?");
            $stmt->bind_param("s"$code);
            $code $_COOKIE['language'];
            $stmt->execute();
            $result $stmt->get_result();
            
            
if($result->num_rows == 1)
                $language_code $_COOKIE['language'];
        }
        
        
if($language_code == '')
        {
            $conn = new mysqli("localhost","root","root","test");
            $stmt $conn->prepare("select s.value from settings s where s.key = ?");
            $stmt->bind_param("s"$lang);
            $lang 'language';
            $stmt->execute();
            $result $stmt->get_result();
            if($result->num_rows == 1)
                $language_code $result->fetch_assoc()['value'];
        }

        if($language_code != '')        
            $this
->set_item('base_url'$this->config['base_url'] . $language_code '/');
    }

I have also faced same issue. Did you really find solution??
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.