language.php 1.0 KB

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