I am trying to come up with an efficient way to implement language selection in my site.
I have some flags at the top right which when clicked I want all the text to change into the selected language, where the translations are stored in my database.
Should I do this with a parameter in the url like:
www.myside.com?lang=3
The only issue I have with this, is that it might things complicated as far as the way I route urls and it doesn't make the url look clean either.
Would a better way, be to have it stored in a session and the translations are only fetched from the database when the language is changed. The translations would be kept in a session array, so users don't hit the database on every page load if you know what I mean.
I was wondering if something like the following would be a good way of achieving what I want:
Session::set('langarray', array(
'id' => $languageId,
'cake' => $this->model->getLanguagesNavigation('cake', $languageId),
'login' => $this->model->getLanguagesNavigation('login', $languageId),
'register' => $this->model->getLanguagesNavigation('register', $languageId),
'share' => $this->model->getLanguagesNavigation('share', $languageId),
'galleries' => $this->model->getLanguagesNavigation('galleries', $languageId),
'decorator' => $this->model->getLanguagesNavigation('decorator', $languageId),
'find' => $this->model->getLanguagesContent('find', $languageId),
'headertext' => $this->model->getLanguagesContent('headerText', $languageId),
));
header('Location: ' . $_SERVER['HTTP_REFERER']);
and in my view:
...
public function render($viewFile, $data = NULL) {
if(!Session::get('langarray'))
{
$this->Language = new Language;
$this->Language->setLanguage(1);
}
if (is_array($data)) {
extract($data);
}
include Vs . $viewFile . '.php';
}
...
Which is simply set the language to 1 (English) if the session var hasn't been set i.e. a language hasn't been picked.
In my HTML I would just echo the corresponding element in the array to get the word:
...
<p><?PHP echo $_SESSION['langarray']['headertext'];?></p>
...
Is this a good method? Or is there a standard way of implementing languages into a site?
My old site currently uses an url method like the one I mentioned (?lang=3) and the foreign variants do quite well in the SEs. I like the idea of using subdomains, but how would I get it to display the correct content on my pages based on whatever come before the first . in the url? E.g. fr. de. etc