Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
config.php
1<?php
2
3namespace Bitrix\Translate;
4
8
9final class Config
10{
11 public const OPTION_INIT_FOLDERS = 'INIT_FOLDERS';
12 public const OPTION_BUTTON_LANG_FILES = 'BUTTON_LANG_FILES';
13 public const OPTION_BACKUP_FILES = 'BACKUP_FILES';
14 public const OPTION_SORT_PHRASES = 'SORT_PHRASES';
15 public const OPTION_DONT_SORT_LANGUAGES = 'DONT_SORT_LANGUAGES';
16 public const OPTION_BACKUP_FOLDER = 'BACKUP_FOLDER';
17 public const OPTION_EXPORT_CSV_DELIMITER = 'EXPORT_CSV_DELIMITER';
18 public const OPTION_EXPORT_FOLDER = 'EXPORT_FOLDER';
19
20 private const CACHE_TTL = 3600;
21
29 public static function getOption(string $optionName): ?string
30 {
31 static $options = [];
32 if (!isset($options[$optionName]))
33 {
34 $options[$optionName] = (string)Main\Config\Option::get(
35 'translate',
36 $optionName,
37 self::getModuleDefault($optionName)
38 );
39 }
40
41 return $options[$optionName] ?: null;
42 }
43
51 public static function getModuleDefault(string $optionName): ?string
52 {
53 static $defs;
54 if ($defs === null)
55 {
56 $defs = Main\Config\Option::getDefaults('translate');
57 }
58
59 return $defs[$optionName] ?: null;
60 }
61
67 public static function getDefaultLanguages(): array
68 {
69 return ['ru', 'en', 'de'];
70 }
71
77 public static function isUtfMode(): bool
78 {
79 static $flag;
80 if ($flag === null)
81 {
82 $flag = Main\Application::isUtfMode() || defined('BX_UTF');
83 }
84
85 return $flag;
86 }
87
93 public static function getAllowedEncodings(): array
94 {
96 }
97
105 public static function getEncodingName(string $encoding): string
106 {
107 static $mess;
108 if ($mess === null)
109 {
110 $mess = Loc::loadLanguageFile(__FILE__);
111 if (empty($mess))
112 {
113 $mess = Loc::loadLanguageFile(__FILE__, 'en');
114 }
115 }
116 $encTitle = Loc::getMessage('TRANSLATE_ENCODING_'.\mb_strtoupper(\str_replace('-', '_', $encoding)));
117 if (!empty($encTitle))
118 {
119 $encTitle .= " ($encoding)";
120 }
121 else
122 {
123 $encTitle = $encoding;
124 $encAlias = self::getAliasEncoding($encoding);
125 if ($encAlias)
126 {
127 $encTitle .= " ($encAlias)";
128 }
129 }
130
131 return $encTitle;
132 }
133
141 public static function getAliasEncoding(string $encoding): ?string
142 {
143 static $aliasEncoding = [
144 'windows-1250' => 'iso-8859-2',
145 'windows-1252' => 'iso-8859-1',
146 ];
147 if (isset($aliasEncoding[$encoding]))
148 {
149 return $aliasEncoding[$encoding];
150 }
151
152 $alias = array_search($encoding, $aliasEncoding);
153 if ($alias !== false)
154 {
155 return $alias;
156 }
157
158 return null;
159 }
160
168 public static function getCultureEncoding(string $languageId): ?string
169 {
170 static $cultureEncoding;
171 if ($cultureEncoding === null)
172 {
173 $cultureEncoding = [];
174 $iterator = Main\Localization\CultureTable::getList([
175 'select' => ['ID', 'CODE', 'CHARSET'],
176 'cache' => ['ttl' => self::CACHE_TTL],
177 ]);
178 while ($row = $iterator->fetch())
179 {
180 $cultureEncoding[\mb_strtolower($row['CODE'])] = \mb_strtolower($row['CHARSET']);
181 }
182 }
183
184 return ($cultureEncoding[$languageId] ?? null) ?: null;
185 }
186
192 public static function getLanguages(): array
193 {
194 static $languages;
195 if ($languages === null)
196 {
197 $languages = [];
198 $iterator = Main\Localization\LanguageTable::getList([
199 'select' => ['ID', 'SORT'],
200 'order' => ['SORT' => 'ASC'],
201 'cache' => ['ttl' => self::CACHE_TTL],
202 ]);
203 while ($row = $iterator->fetch())
204 {
205 $languages[] = mb_strtolower($row['ID']);
206 }
207 }
208
209 return $languages;
210 }
211
217 public static function getEnabledLanguages(): array
218 {
219 static $languages;
220 if ($languages === null)
221 {
222 $languages = [];
223 $iterator = Main\Localization\LanguageTable::getList([
224 'select' => ['ID', 'SORT'],
225 'filter' => ['=ACTIVE' => 'Y'],
226 'order' => ['SORT' => 'ASC'],
227 'cache' => ['ttl' => self::CACHE_TTL],
228 ]);
229 while ($row = $iterator->fetch())
230 {
231 $languages[] = mb_strtolower($row['ID']);
232 }
233 }
234
235 return $languages;
236 }
237
245 public static function getLanguagesTitle(array $languageIds): array
246 {
247 static $cache = [];
248
249 $cacheId = \implode('-', $languageIds);
250 if (!isset($cache[$cacheId]))
251 {
252 $cache[$cacheId] = [];
253
254 $iterator = Main\Localization\LanguageTable::getList([
255 'select' => ['ID', 'NAME'],
256 'filter' => [
257 '=ID' => $languageIds,
258 '=ACTIVE' => 'Y'
259 ],
260 'order' => ['SORT' => 'ASC'],
261 'cache' => ['ttl' => self::CACHE_TTL],
262 ]);
263 while ($row = $iterator->fetch())
264 {
265 $cache[$cacheId][mb_strtolower($row['ID'])] = $row['NAME'];
266 }
267 }
268
269 return $cache[$cacheId];
270 }
271
277 public static function getAvailableLanguages(): array
278 {
279 static $languages;
280 if ($languages === null)
281 {
282 $languages = \array_unique(\array_merge(
283 self::getAvailableDefaultLanguages(),
284 self::getTranslationRepositoryLanguages()
285 ));
286 }
287
288 return $languages;
289 }
290
296 public static function getAvailableDefaultLanguages(): array
297 {
298 static $languages;
299 if ($languages === null)
300 {
301 $languages = [];
302 $langDirList = new Main\IO\Directory(Main\Application::getDocumentRoot(). '/bitrix/modules/main/lang/');
303 foreach ($langDirList->getChildren() as $langDir)
304 {
305 $langId = $langDir->getName();
306 if (\in_array($langId, Translate\IGNORE_FS_NAMES, true) || !$langDir->isDirectory())
307 {
308 continue;
309 }
310 $languages[] = $langId;
311 }
312 }
313
314 return $languages;
315 }
316
322 public static function getTranslationRepositoryLanguages(): array
323 {
324 static $languages;
325 if ($languages === null)
326 {
327 $languages = [];
328 if (Main\Localization\Translation::useTranslationRepository())
329 {
330 $langDirList = new Main\IO\Directory(Main\Localization\Translation::getTranslationRepositoryPath());
331 foreach ($langDirList->getChildren() as $langDir)
332 {
333 $langId = $langDir->getName();
334 if (\in_array($langId, Translate\IGNORE_FS_NAMES, true) || !$langDir->isDirectory())
335 {
336 continue;
337 }
338 $languages[] = $langId;
339 }
340 }
341 }
342
343 return $languages;
344 }
345
351 public static function getInitPath(): array
352 {
354 static $initFolders;
355 if ($initFolders === null)
356 {
357 $initFolders = [];
358 $folders = (string)Main\Config\Option::get(
359 'translate',
360 self::OPTION_INIT_FOLDERS,
361 Translate\Config::getModuleDefault(Translate\Config::OPTION_INIT_FOLDERS)
362 );
363 $folders = \explode(',', \trim($folders));
364 foreach ($folders as $oneFolder)
365 {
366 if (!empty($oneFolder))
367 {
368 $oneFolder = Translate\IO\Path::normalize($oneFolder);
369 $initFolders[] = '/'.\ltrim($oneFolder, '/');
370 }
371 }
372 }
373
374 return $initFolders;
375 }
376
382 public static function getDefaultPath(): string
383 {
384 static $defaultPath;
385 if ($defaultPath === null)
386 {
387 $folders = \explode(',', self::getModuleDefault(self::OPTION_INIT_FOLDERS));
388 $defaultPath = $folders[0];
389 }
390
391 return $defaultPath;
392 }
393
394
400 public static function needToBackUpFiles(): bool
401 {
402 static $needToBackUpFiles;
403 if ($needToBackUpFiles === null)
404 {
405 $def = self::getModuleDefault(self::OPTION_BACKUP_FILES);
406 $needToBackUpFiles = (bool)(Main\Config\Option::get('translate', self::OPTION_BACKUP_FILES, $def) === 'Y');
407 }
408
409 return $needToBackUpFiles;
410 }
411
417 public static function getBackupFolder(): string
418 {
419 static $backupFolder;
420 if ($backupFolder === null)
421 {
422 $confOption = Main\Config\Option::get('translate', self::OPTION_BACKUP_FOLDER, '');
423 if (!empty($confOption))
424 {
425 if (\mb_strpos($confOption, '/') === 0)
426 {
427 $backupFolder = $confOption;
428 }
429 elseif (\strncasecmp(\PHP_OS, 'WIN', 3) === 0 && \preg_match("#^[a-z]{1}:/#i", $confOption))
430 {
431 $backupFolder = $confOption;
432 }
433 else
434 {
435 $backupFolder = Main\Application::getDocumentRoot(). '/'. $confOption;
436 }
437 }
438 else
439 {
440 $defOption = self::getModuleDefault(self::OPTION_BACKUP_FOLDER);
441 if (empty($defOption))
442 {
443 $defOption = 'bitrix/backup/translate/';
444 }
445 $backupFolder = Main\Application::getDocumentRoot(). '/'. $defOption;
446 }
447 }
448
449 return $backupFolder;
450 }
451
457 public static function needToSortPhrases(): bool
458 {
459 static $needToSortPhrases;
460 if ($needToSortPhrases === null)
461 {
462 $def = self::getModuleDefault(self::OPTION_SORT_PHRASES);
463 $needToSortPhrases = (bool)(Main\Config\Option::get('translate', self::OPTION_SORT_PHRASES, $def) === 'Y');
464 }
465
466 return $needToSortPhrases;
467 }
468
474 public static function getNonSortPhraseLanguages(): array
475 {
476 static $nonSortPhraseLanguages;
477 if ($nonSortPhraseLanguages === null)
478 {
479 $nonSortPhraseLanguages = [];
480 $def = self::getModuleDefault(self::OPTION_DONT_SORT_LANGUAGES);
481 $nonSortPhraseLanguages = Main\Config\Option::get('translate', self::OPTION_DONT_SORT_LANGUAGES, $def);
482 if (!is_array($nonSortPhraseLanguages))
483 {
484 $nonSortPhraseLanguages = \explode(',', $nonSortPhraseLanguages);
485 }
486 }
487
488 return $nonSortPhraseLanguages;
489 }
490
496 public static function getExportFolder(): ?string
497 {
498 static $exportFolder = -1;
499 if ($exportFolder === -1)
500 {
501 $exportFolder = null;// '/bitrix/updates/_langs/';
502 $confOption = Main\Config\Option::get('translate', self::OPTION_EXPORT_FOLDER, '');
503 if (!empty($confOption))
504 {
505 if (\mb_strpos($confOption, Main\Application::getDocumentRoot()) === 0)
506 {
507 $exportFolder = $confOption;
508 }
509 else
510 {
511 $exportFolder = Main\Application::getDocumentRoot(). '/'. $confOption;
512 }
513 }
514 }
515
516 return $exportFolder;
517 }
518}
static get($moduleId, $name, $default="", $siteId=false)
Definition option.php:30
static loadLanguageFile($file, $language=null, $normalize=true)
Definition loc.php:224
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getAvailableDefaultLanguages()
Definition config.php:296
static getCultureEncoding(string $languageId)
Definition config.php:168
static getEncodingName(string $encoding)
Definition config.php:105
static getAvailableLanguages()
Definition config.php:277
const OPTION_EXPORT_CSV_DELIMITER
Definition config.php:17
static needToSortPhrases()
Definition config.php:457
static getNonSortPhraseLanguages()
Definition config.php:474
static getEnabledLanguages()
Definition config.php:217
static getModuleDefault(string $optionName)
Definition config.php:51
static getDefaultLanguages()
Definition config.php:67
static getAllowedEncodings()
Definition config.php:93
static needToBackUpFiles()
Definition config.php:400
static getAliasEncoding(string $encoding)
Definition config.php:141
const OPTION_BUTTON_LANG_FILES
Definition config.php:12
static getTranslationRepositoryLanguages()
Definition config.php:322
const OPTION_DONT_SORT_LANGUAGES
Definition config.php:15
static getLanguagesTitle(array $languageIds)
Definition config.php:245
static getOption(string $optionName)
Definition config.php:29