asdsadasd - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of asdsadasd.php

  1. <?php
  2.  
  3. /**
  4.  *  Типо Куки)
  5.  */
  6. $COOKIE = array(
  7.     'lang' => 'ru'
  8. );
  9.  
  10. /**
  11.  * Параметры?
  12.  */
  13. $GLOBALS = array(
  14.     'LANG' => array(
  15.         'RU' => array(
  16.             'TITLE_TECHNICAL' => 'Техника',
  17.             'TOURNAMENT_BLOCK_LETF_HEADER' => 'Блок турниров',
  18.         ),
  19.         'EN' => array(
  20.             'TITLE_TECHNICAL' => 'Technic'
  21.         )
  22.     )
  23. );
  24.  
  25. /**
  26.  * Класс локализатора
  27.  */
  28. class Locale
  29. {
  30.     /* Массив строк локализации */
  31.         protected $_MESSAGES = array();
  32.     /* Текущий язык */
  33.         protected $locale;
  34.         protected $locale_fallback = 'RU';
  35.  
  36.      public function __construct($lang = 'ru')
  37.      {
  38.          $this->locale = strtoupper($lang);
  39.         $this->loadMessages();
  40.     }
  41.  
  42.     protected function loadMessages()
  43.     {
  44.         if (isset($GLOBALS['LANG'][$this->locale])) {
  45.             $this->_MESSAGES = $GLOBALS['LANG'][$this->locale];
  46.         } else {
  47.             $this->_MESSAGES = $GLOBALS['LANG'][$this->locale_fallback];
  48.         }
  49.     }
  50.  
  51.     public function get($key = '')
  52.     {
  53.         if (isset($this->_MESSAGES[$key])) {
  54.             return $this->_MESSAGES[$key];
  55.         }
  56.         return strtoupper($key);
  57.     }
  58. }
  59.  
  60. /**
  61.  * Функция для получения текущего языка (передаю сюда массив куки из за ограниченной области видимости) 
  62.  */
  63. function getAppLocale($default = 'ru', $COOKIE)
  64. {
  65.     return isset($COOKIE['lang']) ? $COOKIE['lang'] : $default;
  66. }
  67.  
  68. /**
  69.  * Получаем текущий язык юзера и инициализируем класс локализатора
  70.  */ 
  71. $currentLang = getAppLocale('ru', $COOKIE);
  72. $locale = new Locale($currentLang);
  73.  
  74. /**
  75.  * Получаем нужные нам строку локали по ключам
  76.  */ 
  77. $myCustomLocaleString = $locale->get('TITLE_TECHNICAL');
  78. $myCustomLocaleStringTwo = $locale->get('TEST_STRING');
  79. $tournamentBlockLeftHeader = $locale->get('TOURNAMENT_BLOCK_LETF_HEADER');
  80.  
  81. print_r(array(
  82.     $myCustomLocaleString,
  83.     $myCustomLocaleStringTwo,
  84.     $tournamentBlockLeftHeader
  85. ));
File Description
  • asdsadasd
  • PHP Code
  • 09 Jan-2021
  • 2.09 Kb
You can Share it: