language.php 1.1 KB

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