Jump to content

Browser Language Detection And Site Translating


es131245
 Share

Recommended Posts

What do think about this suggestion for multi lang site with en as default option?$browser_lang=$_SERVER("HTTP_ACCEPT_LANGUAGE");$browder_lang=preg_replace("/*ru*/","ru",$browser_lang);$browder_lang=preg_replace("/*en*/","en",$browser_lang);switch ($browder_lang)case ru:$hello_world="Привет Мир!";$access_log_open_die="Невозможно открыть <i>ACCESS LOG</i>";break;// case en:// $hello_world="Hello World!";// $access_log_open_die="Cant open <i>ACCESS LOG</i>";// break;default:$hello_world="Hello World!";$access_log_open_die="Cant open <i>ACCESS LOG</i>";break;

Link to comment
Share on other sites

I think that you should spell the variables the same way each time. It will be more efficient to have all of your text strings in a file and include the appropriate file for the language. It will probably also be better to store all of the text in an array instead of individual variables so that you don't have problems with variables getting overwritten.

Link to comment
Share on other sites

1 About what variables are we talking about?2 If you are about /*ru*/ & /*en*/ so its because every browser replays differently on $_SERVER("HTTP_ACCEPT_LANGUAGE") for example Primary Language id: ru-ru User Agent: Opera Full Version Info: 9.60 Primary Language id: ru Mozilla/ Firefox 3.0.10 Just tested myself on: http://techpatterns.com/downloads/php_language_detection.php3 Im not good at array maybe ill change it today after some reading or just show me plz4 Here is a more capable way$browser_lang=$_SERVER("HTTP_ACCEPT_LANGUAGE");$browder_lang=preg_replace("/*ru*/","ru",$browser_lang);$browder_lang=preg_replace("/*en*/","en",$browser_lang);switch ($browder_lang){case ru:include "./langs/ru.php";break;default:include "./langs/en.php";break;}

Link to comment
Share on other sites

1 About what variables are we talking about?
$browser_lang=$_SERVER("HTTP_ACCEPT_LANGUAGE");$browder_lang=preg_replace("/*ru*/","ru",$browser_lang);$browder_lang=preg_replace("/*en*/","en",$browser_lang);switch ($browder_lang)
4 Here is a more capable way
Right, that's how you can include the files. The contents of each file should be the language array for that language. e.g.:
<?php$lang = array();$lang['hello_world']="Привет Мир!";$lang['access_log_open_die']="Невозможно открыть <i>ACCESS LOG</i>";?>

<?php$lang = array(  'hello_world' => 'Hello World!',  'access_log_open_die' => "Cant open <i>ACCESS LOG</i>");?>

Link to comment
Share on other sites

Now im using $browser_language=getenv("HTTP_ACCEPT_LANGUAGE");$browder_lang=preg_match("/*ru*|*en*/",$browser_language,$browser_lang);include("..langs/".$browser_lang.".php");but whats better for en.phparray or a list of $vars????

Edited by es131245
Link to comment
Share on other sites

Now im using $browser_language=getenv("HTTP_ACCEPT_LANGUAGE");$browder_lang=preg_match("/*ru*|*en*/",$browser_language,$browser_lang);include("..langs/".$browser_lang.".php");but whats better for en.phparray or a list of $vars????
An array, because you'll have just one variable containing the text data. One variable - less possible naming conflicts when you need to do other things. Besides, an array is supposed to hold variables with the same kind of purpose, and in this case, the same kind of purpose is text to be outputted, so it's reasonable to use an array.Whether you'll use the form
$lang = array(...);

or

$lang = array();$lang[...] = ...;

is irrelevant, as the end result is the same (an array). Use whichever you prefer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...