PHP Language Changing Website

charlie_82

Expert Member
Joined
Dec 11, 2007
Messages
2,993
Reaction score
1
Location
Johannesburg
Hi there,

I need a bit of help with this...
As per the client's request, all the site's text needs to be held in external files with variables for the different items.

That's not issue, it's getting the languages to change that's the problem.
I realise I can set a session variable or something to specify which language files to use but how does one implement this?

And also, getting the languages change without changing the current view page?
 
Yikes you're right... Not pretty at all.
I just want the variable to update when the button is clicked and stay on the same page...

Is it not possible to use PHP to get the current URL? When the button is clicked, it will to that URL with a variable in ?lang=eng sort of format?
 
Not too bad really... Is the data stored in a database? SQL? If so you need something like the following:

Code:
function getTranslatedForm($lang, $word)
{
// Some operations to retrieve the correct translation of the word
// => $translated_form = query("SELECT $lang FROM WORD_TABLE WHERE WORD='$word'");
// => return $translated_form;
}

Then it's a simple matter of a few javascript operations that check which language was selected, and in the page, instead of writing "hello" you write something like

Code:
<?php echo getTranslatedForm("english","hello"); ?>

where "english" should obviously point to a dynamic variable of sorts ($selected_lang, say), possibly a session variable:

Code:
session_start();
$_SESSION['selected_lang'] = getLanguageSomeHow();

You could even modify it to do entire sentences (although, the grammar will be horrible unless you provide a means to handle that, but natural language processing != easy).

If it's not in a SQL database, then this still applies, but it will obviously need to be modified (in the getTranslatedForm() function).

All this is assuming, of course, that I know what you actually want? From what I understand, you have a page that needs to have the ability to change language. You want the visitor to be able to change the language (I guess in a form or something) and the page should update accordingly (without completely reloading). Is that correct?
 
Ok I managed to figure this out...
I used a bit PHP to get the page's current URL. When a user clicks on a language button, a session variable is set for that language and then redirects the user to the same page but now has the new language from the stored session variable...

Again: how is the data stored?
It isn't stored in a database...
I have two external PHP files (one for each language) that contain defined variables with the content
 
Using a database for your language (eng/afr) is by far a better thing. Not only is it easier, it is less problematic and easier to change/maintain.
As mentioned above, using a session variable or url parameter will work best- this is what several multilingual sites do if they don't want language clones.
 
Top
Sign up to the MyBroadband newsletter
X