Welcome Guest, Not a member yet? Register   Sign In
Database change according to language
#1

[eluser]breaddes[/eluser]
I have multilangual website that has for each language a different mysql database. At the moment I switch the databases in every model constructor:

Code:
switch(language){
    default: $this->load->database('english');
    break;
    case "german": $this->load->database('german');
}

I think that could be better done. I actually need the setting autoloaded everytime a page loads, so it's not effective to write it for each pagerequest in every model or controller. How can I avoid the duplication and write it like a general config thing.

The other thing is, if I upload my project I don't want to overwrite my config files. How can I avoid that? It's actually the database connection settings and the routes settings. Can I source out them somewhere? It's often that I overwrite my entire project ...
#2

[eluser]xwero[/eluser]
Is there no common data between the language parts? If there is you have a lot of redundant data, no?

The way i set up multiple language databases is to separate all the text fields from language independent fields.

table pages : id, order, online
table pages_text : pages_id, title, url, content, language

If you want the language parts to function independent form each other. I just add a language field to the pages table.

If you have static values it's best to store them in language files.
#3

[eluser]breaddes[/eluser]
yep both databases have exactly the same tables and fields. I thought it makes sense to seperate them to keep the databases as small as possible. If I add more languages I could organise them better, couldn't I?

I am not sure know, what the best solution is.
#4

[eluser]Isern Palaus[/eluser]
[quote author="xwero" date="1202920529"]If you have static values it's best to store them in language files.[/quote]

And what is the way to switch them without URI segment?

Browser detect? And how I set to take the value in all the application?

Thank you,
-- Isern Palaus
#5

[eluser]xwero[/eluser]
[quote author="breaddes" date="1202920836"]yep both databases have exactly the same tables and fields. I thought it makes sense to seperate them to keep the databases as small as possible. If I add more languages I could organise them better, couldn't I?

I am not sure know, what the best solution is.[/quote]

You have to do at least two queries for almost every change you make. Lets take my example of pages. If a user wants to change the order of the page you need two queries because you need to update the two databases, i only need one.

If you are adding a language you have to copy one of the databases and clear all text fields. I just have to check if the language row exists and do an insert if the language line doesn't exist.

The size of the database doesn't affect the speed of the queries if you set correct limitations.
#6

[eluser]xwero[/eluser]
[quote author="Isern Palaus" date="1202921542"]
And what is the way to switch them without URI segment? And how I set to take the value in all the application?
[/quote]
You could use a session or a cookie if you want. I've written a switch_to method for the language library to make it easier to switch languages.
Code:
// controller
function Controllername()
{
$this->lang->load('custom_file');
$this->lang->switch_to($this->session->userdata('language'));
}

function langswitch()
{
   $this->session->set_userdata('language',$this->uri->segment(3));
   redirect(str_replace('_','/',$this->uri->segment(4)));
}
// view
<a href="&lt;?php echo site_url('Controllername/langswitch/en/'.$returnurl); ?&gt;">English</a>
<a href="&lt;?php echo site_url('Controllername/langswitch/de/'.$returnurl); ?&gt;">Deutsch</a>
[quote author="Isern Palaus" date="1202921542"]
Browser detect?
[/quote]
Browser detect depends on which data the browser sends along with the request so it will not always be correct.
Code:
function acceptlang_check()
    {
        $this->load->library('user_agent');
        $query = $this->db->get('sitelanguages');
        $sitelangs = $query->result_array();
        $httplangs = $this->agent->languages();
        return (in_array($httplangs[0],$sitelangs))?$httplangs[0]:false;
    }
This function checks if the first language that the browser sends along is present as a language on the site. And if the language is present you can set it the session or in a cookie and load your language files accordingly.
#7

[eluser]Isern Palaus[/eluser]
Thanks xwero,

I'll try to add this to my project. Probably I'll try first to check the browsers language, if it's not supported show the 'en' one and show the language selection too.

Thank you another time,
-- Isern Palaus
#8

[eluser]breaddes[/eluser]
Code:
You have to do at least two queries for almost every change you make. Lets take my example of pages. If a user wants to change the order of the page you need two queries because you need to update the two databases, i only need one.

That's not really true. The datesbases have different content. I update only one database if a user does an entry for example.
#9

[eluser]xwero[/eluser]
So the language parts are independent?
#10

[eluser]breaddes[/eluser]
yep. the databases have the same structure, but the content is independent.




Theme © iAndrew 2016 - Forum software by © MyBB