EDUBRAIN IT SOLUTION BLOG

How to convert your website into numerous languages using PHP Language?

Aug - 02 - 2022 Admin

How to convert your website into numerous languages using PHP Language?

In today’s world, it has become a necessity that a website provides the language in which the user is comfortable so that he/she can search and understand their required topic.

In today’s world, it has become a necessity that a website provides the language in which the user is comfortable so that he/she can search and understand their required topic. It has been rightly said by Ralph Waldo Emerson that Language is a city to the building of which every human being is brought to a stone. The quote portrays the importance of language and how it beautifully connects communities across the business. There are 195 countries speaking a countless number of languages and what if you want to search for something from a website that follows a different language? You are sure to get puzzled indeed. But, now, we have a solution to it! You can search any website speaking differently.
Suppose your website’s default language is English and you want to convert your whole website into different languages based on the country region and language.

The best way to do that in PHP Language using method :

1st Phase- You will have to fetch the IP address from the system

 

$ip = getenv("HTTP_CLIENT_IP")?: getenv("HTTP_X_FORWARDED_FOR")?: getenv("HTTP_X_FORWARDED")?: getenv("HTTP_FORWARDED_FOR")?: getenv("HTTP_FORWARDED")?: getenv("REMOTE_ADDR");

Match the IP Address using Curl Init Method to retrieve the location and region.

2nd Phase- Convert the location into Json format and execute the Json Curl

$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://ipinfo.io/{$ip}"); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER , true); // json output. $output = curl_exec($curl); // CURL close. curl_close($curl);

3rd Phase- Close Curl after executing the Curl Json

curl_close($curl);

4th Phase- Create an array and define the country and languages. The example is given below-

$countries = array( "Mauritius" =>"fr", "Tanzania"=>"fr", "Kenya"=>"fr","Malawi"=>"fr", "Zambia"=>"fr","Uganda"=>"fr", "Lesotho"=>"fr","Senegal"=>"fr","Cameroon"=>"fr", "Iborikeost"=>"fr", "Ghana"=>"fr","The Gambia"=>"fr","America"=>"en","India"=>"en","UAE"=>"ar","Dubai"=>"ar","Canada"=>"en","New York"=>"en","Australia"=>"en" );

5th Phase- Decode the Jason using-

         $variable = json_decode($output, true);

6th Phase- Get the country language using the code given below-

         $country_name = $countries[$country_code["country"]];

7th Phase- Create a function and pass the above variable inside the function and return the variable using the code given below-
         function lang($country_name) {     return $country_name; }

8th Phase- Create another variable. For example follow the below stated code.

function translate($q, $sl, $tl){     $res= file_get_contents(" https://translate.googleapis.com/translate_a/single?client=gtx& ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&sl= ".$sl."&tl=".$tl."&hl=hl&q=".urlencode($q), $_SERVER['DOCUMENT_ROOT']."/transes.html");     $res=json_decode($res);     return $res[0][0][0]; }

9th Phase- Check your converted website using the code given below-

<?php echo translate("Edubrain IT Solution - Software, Website & Digital Marketing Company- ","en",lang($variable));?>

 

Download the file Download Here