PHP Language Detector


SUBMITTED BY: Guest

DATE: July 21, 2014, 11:59 p.m.

FORMAT: Text only

SIZE: 10.0 kB

HITS: 1119

  1. <?php
  2. /*
  3. Script Name: Full Operating system language detection
  4. Author: Harald Hope, Website: http://techpatterns.com/
  5. Script Source URI: http://techpatterns.com/downloads/php_language_detection.php
  6. Version 0.3.6
  7. Copyright (C) 8 December 2008
  8. This program is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free Software
  10. Foundation; either version 3 of the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  14. Get the full text of the GPL here: http://www.gnu.org/licenses/gpl.txt
  15. Coding conventions:
  16. http://cvs.sourceforge.net/viewcvs.py/phpbb/phpBB2/docs/codingstandards.htm?rev=1.3
  17. */
  18. /*
  19. Changes:
  20. 0.3.6 - added possible $feature values to comment header section
  21. */
  22. /******************************************
  23. Script is currently set to accept 2 parameters, triggered by $feature value.
  24. for example, get_languages( 'data' ):
  25. 1. 'header' - sets header values, for redirects etc. No data is returned
  26. 2. 'data' - for language data handling, ie for stats, etc.
  27. Returns an array of the following 4 item array for each language the os supports:
  28. 1. full language abbreviation, like en-ca
  29. 2. primary language, like en
  30. 3. full language string, like English (Canada)
  31. 4. primary language string, like English
  32. *******************************************/
  33. // choice of redirection header or just getting language data
  34. // to call this you only need to use the $feature parameter
  35. function get_languages( $feature, $spare='' )
  36. {
  37. // get the languages
  38. $a_languages = languages();
  39. $index = '';
  40. $complete = '';
  41. $found = false;// set to default value
  42. //prepare user language array
  43. $user_languages = array();
  44. //check to see if language is set
  45. if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) )
  46. {
  47. $languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
  48. // $languages = ' fr-ch;q=0.3, da, en-us;q=0.8, en;q=0.5, fr;q=0.3';
  49. // need to remove spaces from strings to avoid error
  50. $languages = str_replace( ' ', '', $languages );
  51. $languages = explode( ",", $languages );
  52. //$languages = explode( ",", $test);// this is for testing purposes only
  53. foreach ( $languages as $language_list )
  54. {
  55. // pull out the language, place languages into array of full and primary
  56. // string structure:
  57. $temp_array = array();
  58. // slice out the part before ; on first step, the part before - on second, place into array
  59. $temp_array[0] = substr( $language_list, 0, strcspn( $language_list, ';' ) );//full language
  60. $temp_array[1] = substr( $language_list, 0, 2 );// cut out primary language
  61. //place this array into main $user_languages language array
  62. $user_languages[] = $temp_array;
  63. }
  64. //start going through each one
  65. for ( $i = 0; $i < count( $user_languages ); $i++ )
  66. {
  67. foreach ( $a_languages as $index => $complete )
  68. {
  69. if ( $index == $user_languages[$i][0] )
  70. {
  71. // complete language, like english (canada)
  72. $user_languages[$i][2] = $complete;
  73. // extract working language, like english
  74. $user_languages[$i][3] = substr( $complete, 0, strcspn( $complete, ' (' ) );
  75. }
  76. }
  77. }
  78. }
  79. else// if no languages found
  80. {
  81. $user_languages[0] = array( '','','','' ); //return blank array.
  82. }
  83. // print_r($user_languages);
  84. // return parameters
  85. if ( $feature == 'data' )
  86. {
  87. return $user_languages;
  88. }
  89. // this is just a sample, replace target language and file names with your own.
  90. elseif ( $feature == 'header' )
  91. {
  92. switch ( $user_languages[0][1] )// get default primary language, the first one in array that is
  93. {
  94. case 'en':
  95. $location = 'english.php';
  96. $found = true;
  97. break;
  98. case 'sp':
  99. $location = 'spanish.php';
  100. $found = true;
  101. break;
  102. default:
  103. break;
  104. }
  105. if ( $found )
  106. {
  107. header("Location: $location");
  108. }
  109. else// make sure you have a default page to send them to
  110. {
  111. header("Location: default.php");
  112. }
  113. }
  114. }
  115. function languages()
  116. {
  117. // pack abbreviation/language array
  118. // important note: you must have the default language as the last item in each major language, after all the
  119. // en-ca type entries, so en would be last in that case
  120. $a_languages = array(
  121. 'af' => 'Afrikaans',
  122. 'sq' => 'Albanian',
  123. 'ar-dz' => 'Arabic (Algeria)',
  124. 'ar-bh' => 'Arabic (Bahrain)',
  125. 'ar-eg' => 'Arabic (Egypt)',
  126. 'ar-iq' => 'Arabic (Iraq)',
  127. 'ar-jo' => 'Arabic (Jordan)',
  128. 'ar-kw' => 'Arabic (Kuwait)',
  129. 'ar-lb' => 'Arabic (Lebanon)',
  130. 'ar-ly' => 'Arabic (libya)',
  131. 'ar-ma' => 'Arabic (Morocco)',
  132. 'ar-om' => 'Arabic (Oman)',
  133. 'ar-qa' => 'Arabic (Qatar)',
  134. 'ar-sa' => 'Arabic (Saudi Arabia)',
  135. 'ar-sy' => 'Arabic (Syria)',
  136. 'ar-tn' => 'Arabic (Tunisia)',
  137. 'ar-ae' => 'Arabic (U.A.E.)',
  138. 'ar-ye' => 'Arabic (Yemen)',
  139. 'ar' => 'Arabic',
  140. 'hy' => 'Armenian',
  141. 'as' => 'Assamese',
  142. 'az' => 'Azeri',
  143. 'eu' => 'Basque',
  144. 'be' => 'Belarusian',
  145. 'bn' => 'Bengali',
  146. 'bg' => 'Bulgarian',
  147. 'ca' => 'Catalan',
  148. 'zh-cn' => 'Chinese (China)',
  149. 'zh-hk' => 'Chinese (Hong Kong SAR)',
  150. 'zh-mo' => 'Chinese (Macau SAR)',
  151. 'zh-sg' => 'Chinese (Singapore)',
  152. 'zh-tw' => 'Chinese (Taiwan)',
  153. 'zh' => 'Chinese',
  154. 'hr' => 'Croatian',
  155. 'cs' => 'Czech',
  156. 'da' => 'Danish',
  157. 'div' => 'Divehi',
  158. 'nl-be' => 'Dutch (Belgium)',
  159. 'nl' => 'Dutch (Netherlands)',
  160. 'en-au' => 'English (Australia)',
  161. 'en-bz' => 'English (Belize)',
  162. 'en-ca' => 'English (Canada)',
  163. 'en-ie' => 'English (Ireland)',
  164. 'en-jm' => 'English (Jamaica)',
  165. 'en-nz' => 'English (New Zealand)',
  166. 'en-ph' => 'English (Philippines)',
  167. 'en-za' => 'English (South Africa)',
  168. 'en-tt' => 'English (Trinidad)',
  169. 'en-gb' => 'English (United Kingdom)',
  170. 'en-us' => 'English (United States)',
  171. 'en-zw' => 'English (Zimbabwe)',
  172. 'en' => 'English',
  173. 'us' => 'English (United States)',
  174. 'et' => 'Estonian',
  175. 'fo' => 'Faeroese',
  176. 'fa' => 'Farsi',
  177. 'fi' => 'Finnish',
  178. 'fr-be' => 'French (Belgium)',
  179. 'fr-ca' => 'French (Canada)',
  180. 'fr-lu' => 'French (Luxembourg)',
  181. 'fr-mc' => 'French (Monaco)',
  182. 'fr-ch' => 'French (Switzerland)',
  183. 'fr' => 'French (France)',
  184. 'mk' => 'FYRO Macedonian',
  185. 'gd' => 'Gaelic',
  186. 'ka' => 'Georgian',
  187. 'de-at' => 'German (Austria)',
  188. 'de-li' => 'German (Liechtenstein)',
  189. 'de-lu' => 'German (Luxembourg)',
  190. 'de-ch' => 'German (Switzerland)',
  191. 'de' => 'German (Germany)',
  192. 'el' => 'Greek',
  193. 'gu' => 'Gujarati',
  194. 'he' => 'Hebrew',
  195. 'hi' => 'Hindi',
  196. 'hu' => 'Hungarian',
  197. 'is' => 'Icelandic',
  198. 'id' => 'Indonesian',
  199. 'it-ch' => 'Italian (Switzerland)',
  200. 'it' => 'Italian (Italy)',
  201. 'ja' => 'Japanese',
  202. 'kn' => 'Kannada',
  203. 'kk' => 'Kazakh',
  204. 'kok' => 'Konkani',
  205. 'ko' => 'Korean',
  206. 'kz' => 'Kyrgyz',
  207. 'lv' => 'Latvian',
  208. 'lt' => 'Lithuanian',
  209. 'ms' => 'Malay',
  210. 'ml' => 'Malayalam',
  211. 'mt' => 'Maltese',
  212. 'mr' => 'Marathi',
  213. 'mn' => 'Mongolian (Cyrillic)',
  214. 'ne' => 'Nepali (India)',
  215. 'nb-no' => 'Norwegian (Bokmal)',
  216. 'nn-no' => 'Norwegian (Nynorsk)',
  217. 'no' => 'Norwegian (Bokmal)',
  218. 'or' => 'Oriya',
  219. 'pl' => 'Polish',
  220. 'pt-br' => 'Portuguese (Brazil)',
  221. 'pt' => 'Portuguese (Portugal)',
  222. 'pa' => 'Punjabi',
  223. 'rm' => 'Rhaeto-Romanic',
  224. 'ro-md' => 'Romanian (Moldova)',
  225. 'ro' => 'Romanian',
  226. 'ru-md' => 'Russian (Moldova)',
  227. 'ru' => 'Russian',
  228. 'sa' => 'Sanskrit',
  229. 'sr' => 'Serbian',
  230. 'sk' => 'Slovak',
  231. 'ls' => 'Slovenian',
  232. 'sb' => 'Sorbian',
  233. 'es-ar' => 'Spanish (Argentina)',
  234. 'es-bo' => 'Spanish (Bolivia)',
  235. 'es-cl' => 'Spanish (Chile)',
  236. 'es-co' => 'Spanish (Colombia)',
  237. 'es-cr' => 'Spanish (Costa Rica)',
  238. 'es-do' => 'Spanish (Dominican Republic)',
  239. 'es-ec' => 'Spanish (Ecuador)',
  240. 'es-sv' => 'Spanish (El Salvador)',
  241. 'es-gt' => 'Spanish (Guatemala)',
  242. 'es-hn' => 'Spanish (Honduras)',
  243. 'es-mx' => 'Spanish (Mexico)',
  244. 'es-ni' => 'Spanish (Nicaragua)',
  245. 'es-pa' => 'Spanish (Panama)',
  246. 'es-py' => 'Spanish (Paraguay)',
  247. 'es-pe' => 'Spanish (Peru)',
  248. 'es-pr' => 'Spanish (Puerto Rico)',
  249. 'es-us' => 'Spanish (United States)',
  250. 'es-uy' => 'Spanish (Uruguay)',
  251. 'es-ve' => 'Spanish (Venezuela)',
  252. 'es' => 'Spanish (Traditional Sort)',
  253. 'sx' => 'Sutu',
  254. 'sw' => 'Swahili',
  255. 'sv-fi' => 'Swedish (Finland)',
  256. 'sv' => 'Swedish',
  257. 'syr' => 'Syriac',
  258. 'ta' => 'Tamil',
  259. 'tt' => 'Tatar',
  260. 'te' => 'Telugu',
  261. 'th' => 'Thai',
  262. 'ts' => 'Tsonga',
  263. 'tn' => 'Tswana',
  264. 'tr' => 'Turkish',
  265. 'uk' => 'Ukrainian',
  266. 'ur' => 'Urdu',
  267. 'uz' => 'Uzbek',
  268. 'vi' => 'Vietnamese',
  269. 'xh' => 'Xhosa',
  270. 'yi' => 'Yiddish',
  271. 'zu' => 'Zulu' );
  272. return $a_languages;
  273. }
  274. ?>

comments powered by Disqus