Извличане на текушия език на Браузера?

C++ JAVA
Post Reply
User avatar
STaNiMiR
Турист
Турист
Posts: 208
Joined: Sat Apr 29, 2006 9:45 pm
Location: Бургас/София
Contact:

Извличане на текушия език на Браузера?

Post by STaNiMiR » Mon Feb 25, 2008 2:30 pm

Искам да питам как може да стане чрез php ? Ако не може чрез пхп, по какъв друг начин може да стане?

User avatar
lortnoc
Турист
Турист
Posts: 545
Joined: Mon Jul 09, 2007 4:57 am
Answers: 125
Location: Пловдив

Post by lortnoc » Mon Feb 25, 2008 2:38 pm

mozilla ??

<script>
alert(window.navigator.language);
</script>

ie ??

<script>
alert(navigator.browserLanguage);
</script>

?
Last edited by lortnoc on Mon Feb 25, 2008 2:39 pm, edited 1 time in total.

User avatar
jooorooo
Шаман
Шаман
Posts: 4334
Joined: Sat May 06, 2006 10:28 pm
Answers: 576
Location: I-net
Contact:

Post by jooorooo » Mon Feb 25, 2008 2:38 pm

language.class.php

Code: Select all

<?php

  class detect_language &#123;
	var $available_languages, $accepted_language, $detected_language;

	// Constructor
	function detect_language&#40;&#41; &#123;
	  $this->available_languages = array&#40;
		'bg'         => array&#40;'bg|bulgarian', 'bulgarian-win1251'&#41;,
		'ca'         => array&#40;'ca|catalan', 'catala'&#41;,
		'cs-iso'     => array&#40;'cs|czech', 'czech-iso'&#41;,
		'cs-win1250' => array&#40;'cs|czech', 'czech-win1250'&#41;,
		'da'         => array&#40;'da|danish', 'danish'&#41;,
		'de'         => array&#40;'de&#40;&#91;-_&#93;&#91;&#91;&#58;alpha&#58;&#93;&#93;&#123;2&#125;&#41;?|german', 'german'&#41;,
		'en'         => array&#40;'en&#40;&#91;-_&#93;&#91;&#91;&#58;alpha&#58;&#93;&#93;&#123;2&#125;&#41;?|english', 'english'&#41;,
		'es'         => array&#40;'es&#40;&#91;-_&#93;&#91;&#91;&#58;alpha&#58;&#93;&#93;&#123;2&#125;&#41;?|spanish', 'spanish'&#41;,
		'fr'         => array&#40;'fr&#40;&#91;-_&#93;&#91;&#91;&#58;alpha&#58;&#93;&#93;&#123;2&#125;&#41;?|french', 'french'&#41;,
		'it'         => array&#40;'it|italian', 'italian'&#41;,
		'ja'         => array&#40;'ja|japanese', 'japanese'&#41;,
		'ko'         => array&#40;'ko|korean', 'korean'&#41;,
		'nl'         => array&#40;'nl&#40;&#91;-_&#93;&#91;&#91;&#58;alpha&#58;&#93;&#93;&#123;2&#125;&#41;?|dutch', 'dutch'&#41;,
		'no'         => array&#40;'no|norwegian', 'norwegian'&#41;,
		'pl'         => array&#40;'pl|polish', 'polish'&#41;,
		'pt-br'      => array&#40;'pt&#91;-_&#93;br|brazilian portuguese', 'brazilian_portuguese'&#41;,
		'pt'         => array&#40;'pt&#40;&#91;-_&#93;&#91;&#91;&#58;alpha&#58;&#93;&#93;&#123;2&#125;&#41;?|portuguese', 'portuguese'&#41;,
		'ru-koi8r'   => array&#40;'ru|russian', 'russian-koi8'&#41;,
		'ru-win1251' => array&#40;'ru|russian', 'russian-win1251'&#41;,
		'se'         => array&#40;'se|swedish', 'swedish'&#41;,
		'sk'         => array&#40;'sk|slovak', 'slovak-iso'&#41;,
		'th'         => array&#40;'th|thai', 'thai'&#41;,
		'zh-tw'      => array&#40;'zh&#91;-_&#93;tw|chinese traditional', 'chinese_big5'&#41;,
		'zh'         => array&#40;'zh|chinese simplified', 'chinese_gb'&#41;
	  &#41;;

	  $this->accepted_language = explode&#40;',', getenv&#40;'HTTP_ACCEPT_LANGUAGE'&#41;&#41;;

	  $this->detected_language = $this->getLanguage&#40;&#41;;
	&#125;

	/*
	  getLanguage
	  -----------
	  function that look for prefered language by browser.

	  Input&#58; -
	  Output&#58; language detected or default language &#40;en&#41;.
	*/
	function getLanguage&#40;&#41; &#123;
	  if &#40;empty&#40;$this->detected_language&#41;&#41; &#123;
		$this->detected_language = 'en';
		$cnt = 0;

		while &#40;$cnt < sizeof&#40;$this->accepted_language&#41;&#41; &#123;
		  reset&#40;$this->available_languages&#41;;

		  while &#40;list&#40;$key, $value&#41; = each&#40;$this->available_languages&#41;&#41; &#123;
			if &#40;&#40;eregi&#40;'^&#40;' . $value&#91;0&#93; . '&#41;&#40;;q=&#91;0-9&#93;\\.&#91;0-9&#93;&#41;?$', $this->accepted_language&#91;$cnt&#93;&#41;&#41;&#41; &#123;
			  $this->detected_language = $key;
			  break 2;
			&#125;
		  &#125;
		  $cnt++;
		&#125;
	  &#125;

	  return $this->detected_language;
	&#125;
  &#125;
?>
example.php

Code: Select all

<html>
<body>
<?php
  include "language.class.php";
  $dl = new detect_language;
  echo "Prefered language of this browser is ".$dl->detected_language;
?>
</body>
<html>

User avatar
jooorooo
Шаман
Шаман
Posts: 4334
Joined: Sat May 06, 2006 10:28 pm
Answers: 576
Location: I-net
Contact:

Post by jooorooo » Mon Feb 25, 2008 2:44 pm

Code: Select all

<?php

$language = $_SERVER&#91;'HTTP_ACCEPT_LANGUAGE'&#93;;
$language_short = substr&#40;$taal,0,2&#41;;

echo $language_short;
?>

User avatar
STaNiMiR
Турист
Турист
Posts: 208
Joined: Sat Apr 29, 2006 9:45 pm
Location: Бургас/София
Contact:

Post by STaNiMiR » Mon Feb 25, 2008 9:35 pm

jooorooo wrote:

Code: Select all

<?php

$language = $_SERVER&#91;'HTTP_ACCEPT_LANGUAGE'&#93;;
$language_short = substr&#40;$taal,0,2&#41;;

echo $language_short;
?>


jorooooo това е мн добре в сравнение с тоя клас горе дето го написа. Мерси мн.

Post Reply