Translated using Weblate (Portuguese)
[phpmyadmin.git] / src / Language.php
blob30c40bbb6b54987b8d62ce2882941689a40706d1
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin;
7 use function __;
8 use function _bindtextdomain;
9 use function _setlocale;
10 use function _textdomain;
11 use function addcslashes;
12 use function function_exists;
13 use function in_array;
14 use function preg_match;
15 use function setlocale;
16 use function str_contains;
17 use function str_replace;
18 use function strcmp;
20 /**
21 * Language object
23 class Language
25 protected string $regex;
27 /**
28 * Constructs the Language object
30 * @param string $code Language code
31 * @param string $name English name
32 * @param string $native Native name
33 * @param string $regex Match regular expression
34 * @param string $mysql MySQL locale code
36 public function __construct(
37 protected string $code,
38 protected string $name,
39 protected string $native,
40 string $regex,
41 protected string $mysql,
42 ) {
43 if (! str_contains($regex, '[-_]')) {
44 $regex = str_replace('|', '([-_][[:alpha:]]{2,3})?|', $regex);
47 $this->regex = $regex;
50 /**
51 * Returns native name for language
53 public function getNativeName(): string
55 return $this->native;
58 /**
59 * Returns English name for language
61 public function getEnglishName(): string
63 return $this->name;
66 /**
67 * Returns verbose name for language
69 public function getName(): string
71 if ($this->native !== '') {
72 return $this->native . ' - ' . $this->name;
75 return $this->name;
78 /**
79 * Returns language code
81 public function getCode(): string
83 return $this->code;
86 /**
87 * Returns MySQL locale code, can be empty
89 public function getMySQLLocale(): string
91 return $this->mysql;
94 /**
95 * Compare function used for sorting
97 * @param Language $other Other object to compare
99 * @return int same as strcmp
101 public function cmp(Language $other): int
103 return strcmp($this->name, $other->name);
107 * Checks whether language is currently active.
109 public function isActive(): bool
111 return $GLOBALS['lang'] == $this->code;
115 * Checks whether language matches HTTP header Accept-Language.
117 * @param string $header Header content
119 public function matchesAcceptLanguage(string $header): bool
121 $pattern = '/^('
122 . addcslashes($this->regex, '/')
123 . ')(;q=[0-9]\\.[0-9])?$/i';
125 return (bool) preg_match($pattern, $header);
129 * Checks whether language matches HTTP header User-Agent
131 * @param string $header Header content
133 public function matchesUserAgent(string $header): bool
135 $pattern = '/(\(|\[|;[[:space:]])('
136 . addcslashes($this->regex, '/')
137 . ')(;|\]|\))/i';
139 return (bool) preg_match($pattern, $header);
143 * Checks whether language is RTL
145 public function isRTL(): bool
147 return in_array($this->code, ['ar', 'fa', 'he', 'ur'], true);
151 * Activates given translation
153 public function activate(): void
155 $GLOBALS['lang'] = $this->code;
157 // Set locale
158 _setlocale(0, $this->code);
159 _bindtextdomain('phpmyadmin', LOCALE_PATH);
160 _textdomain('phpmyadmin');
161 // Set PHP locale as well
162 if (function_exists('setlocale')) {
163 setlocale(0, $this->code);
166 /* Text direction for language */
167 LanguageManager::$textDir = $this->isRTL() ? 'rtl' : 'ltr';
169 /* TCPDF */
170 $GLOBALS['l'] = [];
172 /* TCPDF settings */
173 $GLOBALS['l']['a_meta_charset'] = 'UTF-8';
174 $GLOBALS['l']['a_meta_dir'] = LanguageManager::$textDir;
175 $GLOBALS['l']['a_meta_language'] = $this->code;
177 /* TCPDF translations */
178 $GLOBALS['l']['w_page'] = __('Page number:');
180 /* Show possible warnings from language selection */
181 LanguageManager::getInstance()->showWarnings();