About meDashed CMS RSS
Kuragramming of beard
 
 
 /Root/Articles/

The trick with switching languages

21.10.2011 14:19:39

While I implemented the conclusion on its website tried several options how to replace the mirror and language at the end of all investigations came to one not a bad option ... To start code of . htaccess

RewriteEngine On

RewriteRule ^([^/]*)\.html?$ /index.php?str=$1 [L]

 

ridiculously simple implementation, strictly speaking, I do not really like modrewrite , although a regular expression and not a bad thing I know, still with him is always there zamorochki.

 

In this expression, we have all the addresses from the site root extension html sent to the variable "str" ​​. Sign carats "^" at the beginning of expression indicates that the conversion took place only at the root, it is necessary that he does not mess up the address in other locations such as CMS . Carats in square brackets has a different meaning, it indicates that the searches should be any character except a slash "/", okay we will not go into a regular expression, the main thing that works :).

turn to another issue, namely, we have a variable and then what?

 

And then we think of the divider ... The fact that we have nested on the site may be different, ie, can be articles, news, directory, and most importantly simple pages, and yet we have many languages ​​and all this should be stated URL printing the script and understood by all. I like using dashes as a delimiter, you can still underscore, but unfortunately in my case the emphasis is not suitable as they are replaced with spaces or other characters in the formation of the address in my CMS , so I settled on the dash. Of course it was still possible to use slashes "/" but I do not like them for them to write complex modrewrite .

now getting a variable to make a start so that if she came not need to assign a null value and one for the language, too ...

$str = isset ($_GET['str'])? $_GET['str'] : '';

$lang ='';

 

Next parse the resulting variable, do not be alarmed at the word parse, it is actually easier, there is a great feature explode () , I will not describe I think it will be clear from the code

$url_vars = explode('-',$str);

Now we have an array of arguments, once we define the length of the ...

$url_vars_c = count($url_vars);

And then there is a problem, the first parameter can be passed to the language and can not be transferred. In this case, we need other arguments that do not swim, so that each was responsible for the level of nesting. Then quote the way I did it:

 

if ($url_vars_c > 1) {

$lang = ($url_vars[0] == 'en')||($url_vars[0] == 'kz') ? $url_vars[0]:'';

if ($lang != '') {unset($url_vars[0]); $url_vars = array_values​​($url_vars);}

}

 

If the length is more

One unit means we have more than one argument, which means that some of them may be language, or may not be:) That is the question.

definition of the language in the second line the previous code, but in the third line, we ask whether the language is defined among the arguments, if so then remove the zero element of the array, but in PHP trouble with this case, since even if you delete the array then the indices will still be old and therefore had to use a feature that transfers value from one array to another (without indexes) array_values ​​() , I think it can even convert an associative array into an index. We now have an array starts from zero again and you can easily turn to the right argument on its index.

 

Next will come in handy a few variables are responsible for a prefix to the database fields and inferred URL , and here they found (the secrets of building the base for my site :))

$MLP = $lang != '' ? $lang . '_' : '';

$ULP = $lang != '' ? $lang . '-' : '';

 

Why is it called do not know, I remember that "P" means the prefix, "L" language, "U" = "URL" , and "M" forgot:)

In general $MLP is responsible for the fields prefix database the way in its use must slash before sending it to the database is a function of ->real_escape_string() and mysql_real_escape_string() .

 

$ULP add the generation of options to address when switching languages ​​issue changed.

About

output will not write their own fantasy everyone and everything not raspishesh ...

 

write better about how to make a link to switch languages, and here we come to the aid of used already back in the early (explode()) now we use implode() it will bring together an array of arguments to a string

<a href="<?=implode('-',$url_vars)?>.html "> <img src="media/ru.gif"> </a>

<a href="kz-<?=implode('-',$url_vars)?>.html"> <img src="media/kz.gif">  </a>

<a href="en-<?=implode('-',$url_vars)?>.html"> <img src="media/en.gif">

 

Nothing complicated, but because language is a mirror, and switch on the same page on which we were just in a different language!

 

There is an important observation! This way of implementing a multilingual approach only if all fields are in need of translation into a single table. Ie the structure is not appropriate for sites in which language versions are in different tables and have different id ! Unfortunately these engines, CMS and 99% of sites in Kazakhstan, and between Kazakhstan is a multinational country and still has two official languages ​​Russian and Kazakh, and on government websites is usually done three languages, the third version.

 

I hurt so much to look at these sites, and even more frightening for those webmasters (DTP), which have to fill in such sites, because if the language versions are not in one row of the database, it is usually difficult to find the element corresponding to the other version of the language because of what is spent considerable time and effort to synchronize language versions. Actually this fact and encouraged me to create its CMS without this defect.

But on the other hand, this advantage may become a huge problem, if the owners of the site would be negligent is to fill the other language versions. Such as news on the site is often visited by only one language version, as if to fill in only one then the other will be empty elements. So before you choose this method must be well to discuss the issue with multi-language customer. But as a rule the customer will say one thing and actually get more, so it's good to know the possibility of the perpetrators, those who immediately will gather content for the site.

 

At this until all the ... Good luck to all!

Рейтинг $r-- [2] $r++

Показать коментарии (vkontakte.ru)
}