简体   繁体   中英

How to dynamically change website's language using PHP-MySQL?

I have PHP-MySQL Website with admin panel. I can enter data from admin panel and they display on user side web pages. Now, I want that I should be able to insert text data in English language from admin panel and they should display in 3 different languages at user side.

At user side, I want to give options for visitors to select language preference and user sides pages should display in that selected language. Right now I am completely unaware for what programming related changes I will need to make on user sides pages to make this possible.

I have searched and read some articles on internet but frankly could not find solution that will work for my case. So please help me.

Following things are used / set in my working environment.

  • Windows Server 2003
  • PHP 5.2.17
  • MySQL 5.0.51a (UTF-8 Unicode (utf8))
  • XHTML 1.0 Transitional
  • meta charset=iso-8859-1

Thank you in advance, KRA

Assuming that you want the translations performed on the server side (PHP) you can use file_get_contents to fetch data from Google Translate API. Then you need to parse the response and get translated text. You need to get API KEY to access the Translate service.

<?php
$string = 'Hello World';
$source_lang = 'en';
$target_lang = 'zh-CN'

header ( "Content-Type: text/html;charset=utf-8" );
$data = file_get_contents ( 'https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&q='.urlencode($string).'&source='.$source_lang.'&target='.$target_lang ); 
$data = json_decode ( $data );  
$translated = $data->data->translations->[0]->translatedText;
echo $translated;

?>

Server responses are JSON objects with that structure:

{
    "data": {
        "translations": [
            {
                "translatedText": "Hallo Welt",
                "detectedSourceLanguage": "en"
            }
        ]
    }
}

More info about basic concept is avaliable on: http://baris.aydinoglu.info/coding/google-translate-api-in-php .

Documentation of Google Translate API queries: http://code.google.com/apis/language/translate/v2/using_rest.html

You may use this and save the relevant data to your database. You may have separate columns for each language in your database table. Once the text / data is entered by the admin, you have to convert them using the solution and save them to relevant columns on the SAME row. The user may be given data from the relevant column according to their language selection.

Assuming you have all your language variables and/or html views separated out into some sort of structure, you could always just use a cookie.

Have your site check for the presence of said cookie and if it doesn't exist, then have the site revert to the default language, otherwise adjust the language accordingly.

Ex:

Have a form with dropdown values for each language and when it is submitted, set the language cookie variable:

setcookie('userLanguage',$language_value,strtotime("+1 year"));

The first parameter is the name of the cookie variable, the second is the value (in this case, the user's language selection, and the third is the expiry date of the cookie (I made it one year, but you can set it to whatever you like).

The next step really depends on how your site is setup, but I'll assume that you have your language text in a db or handled in some intelligent way. Either way, you need to retrieve the cookie:

if ($_COOKIE['userLanguage'] <> '')
  // Do some language stuff here based on the value of $_COOKIE['userLanguage']
else
  // Do some stuff here for default language

That's about it. There are a lot of different ways to handle the actual language conversion, but the act of getting the user's preference and remembering it is pretty much as basic as that.

Well this depends alot on how you store your textual data. If you have it in a db (mysql) then i suggest that you have a collumn that stands for the language. Then you could have either a session variable or a specific url or something else that gives you some indication of which language the user is requesting.

What you need is some way to figure out what kind of language your user wants, after that it's quite easy.

Have you tried anything yet?

Use language Files in your website.

For example, For english -en.php For French -fr.php

This files will have variables that will be used in front User end.User can select any language and on selecting, your code will load the following language

In your case, you are using database, so you can have this thing in your tables instead of Database

so your Front files will consist of variables instead of Text and this variables will be replace by language variables(Either from database or from files, its your choice) when user select a language

And yes, you need to put a default language.

Use following links to implement :-

http://scriptdigital.com/divers/phplocalization.html
http://www.mind-it.info/2010/02/22/a-simple-approach-to-localization-in-php/
http://code.google.com/p/slsphp/

Use this method.

Its working for me.

Switching languages on a website with PHP

I've done it in oyutrade.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM