language.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. $config = parse_ini_file(__DIR__.'/config/config.ini',true);
  3. if($config["App"]["debug_mode"]) {
  4. ini_set('display_errors',1);
  5. error_reporting(E_ALL);
  6. }
  7. $locale = '';
  8. if(isset($_COOKIE['language'])) $locale = $_COOKIE['language'];
  9. else if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  10. $langcode = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
  11. if(file_exists(__DIR__.'/locale/'.$langcode)) $locale = $langcode;
  12. else if(file_exists(__DIR__.'/locale/'.explode("_",$langcode)[0])) $locale = explode("_",$langcode)[0];
  13. else $locale = $config['App']['default_language'];
  14. }
  15. else $locale = $config['App']['default_language'];
  16. if(function_exists("putenv")) {
  17. putenv('LC_ALL='.$locale);
  18. }
  19. if(!setlocale(LC_ALL,$locale)) {
  20. if(!setlocale(LC_ALL,$locale.".UTF-8")) setlocale(LC_ALL,0);
  21. }
  22. bindtextdomain('messages',__DIR__.'/locale');
  23. bind_textdomain_codeset('messages','UTF-8');
  24. textdomain('messages');
  25. if(!function_exists('pgettext')) {
  26. function pgettext($context,$msgid) {
  27. $contextString = "{$context}\004{$msgid}";
  28. $translation = _($contextString);
  29. if($translation == $contextString) return $msgid;
  30. else return $translation;
  31. }
  32. }
  33. if(strstr($locale,"_")) $lang = explode('_',$locale)[0];
  34. else $lang = $locale;
  35. ?>