PageRenderTime 22ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/translate/lib/config.php

https://gitlab.com/alexprowars/bitrix
PHP | 518 lines | 352 code | 48 blank | 118 comment | 41 complexity | 09f3b578c2aedb38c8c27416ea76e9d4 MD5 | raw file
  1. <?php declare(strict_types = 1);
  2. namespace Bitrix\Translate;
  3. use Bitrix\Main;
  4. use Bitrix\Translate;
  5. use Bitrix\Main\Localization\Loc;
  6. final class Config
  7. {
  8. public const OPTION_INIT_FOLDERS = 'INIT_FOLDERS';
  9. public const OPTION_BUTTON_LANG_FILES = 'BUTTON_LANG_FILES';
  10. public const OPTION_BACKUP_FILES = 'BACKUP_FILES';
  11. public const OPTION_SORT_PHRASES = 'SORT_PHRASES';
  12. public const OPTION_DONT_SORT_LANGUAGES = 'DONT_SORT_LANGUAGES';
  13. public const OPTION_BACKUP_FOLDER = 'BACKUP_FOLDER';
  14. public const OPTION_EXPORT_CSV_DELIMITER = 'EXPORT_CSV_DELIMITER';
  15. public const OPTION_EXPORT_FOLDER = 'EXPORT_FOLDER';
  16. private const CACHE_TTL = 3600;
  17. /**
  18. * Returns an module option.
  19. *
  20. * @param string $optionName Name of option.
  21. *
  22. * @return string|null
  23. */
  24. public static function getOption(string $optionName): ?string
  25. {
  26. static $options = [];
  27. if (!isset($options[$optionName]))
  28. {
  29. $options[$optionName] = (string)Main\Config\Option::get(
  30. 'translate',
  31. $optionName,
  32. self::getModuleDefault($optionName)
  33. );
  34. }
  35. return $options[$optionName] ?: null;
  36. }
  37. /**
  38. * Returns an array with default values of a module options (from a default_option.php file).
  39. *
  40. * @param string $optionName Name of option.
  41. *
  42. * @return string|null
  43. */
  44. public static function getModuleDefault(string $optionName): ?string
  45. {
  46. static $defs;
  47. if ($defs === null)
  48. {
  49. $defs = Main\Config\Option::getDefaults('translate');
  50. }
  51. return $defs[$optionName] ?: null;
  52. }
  53. /**
  54. * Returns the default translation language list.
  55. *
  56. * @return string[]
  57. */
  58. public static function getDefaultLanguages(): array
  59. {
  60. return ['ru', 'en', 'de', 'ua'];
  61. }
  62. /**
  63. * Returns true id server is in utf-8 mode. False - otherwise.
  64. *
  65. * @return boolean
  66. */
  67. public static function isUtfMode(): bool
  68. {
  69. static $flag;
  70. if ($flag === null)
  71. {
  72. $flag = Main\Application::isUtfMode() || defined('BX_UTF');
  73. }
  74. return $flag;
  75. }
  76. /**
  77. * Returns list of allowed to operate charset encodings.
  78. *
  79. * @return string[]
  80. */
  81. public static function getAllowedEncodings(): array
  82. {
  83. return Translate\ENCODINGS;
  84. }
  85. /**
  86. * Returns list of allowed to operate charset encodings.
  87. *
  88. * @param string $encoding Encoding to check.
  89. *
  90. * @return string
  91. */
  92. public static function getEncodingName(string $encoding): string
  93. {
  94. static $mess;
  95. if ($mess === null)
  96. {
  97. $mess = Loc::loadLanguageFile(__FILE__);
  98. if (empty($mess))
  99. {
  100. $mess = Loc::loadLanguageFile(__FILE__, 'en');
  101. }
  102. }
  103. $encTitle = Loc::getMessage('TRANSLATE_ENCODING_'.mb_strtoupper(str_replace('-', '_', $encoding)));
  104. if (!empty($encTitle))
  105. {
  106. $encTitle .= " ($encoding)";
  107. }
  108. else
  109. {
  110. $encTitle = $encoding;
  111. $encAlias = self::getAliasEncoding($encoding);
  112. if ($encAlias)
  113. {
  114. $encTitle .= " ($encAlias)";
  115. }
  116. }
  117. return $encTitle;
  118. }
  119. /**
  120. * Returns alias of encoding.
  121. *
  122. * @param string $encoding Encoding to check.
  123. *
  124. * @return string|null
  125. */
  126. public static function getAliasEncoding(string $encoding): ?string
  127. {
  128. static $aliasEncoding = array(
  129. 'windows-1250' => 'iso-8859-2',
  130. 'windows-1252' => 'iso-8859-1',
  131. );
  132. if(isset($aliasEncoding[$encoding]))
  133. {
  134. return $aliasEncoding[$encoding];
  135. }
  136. $alias = array_search($encoding, $aliasEncoding);
  137. if ($alias !== false)
  138. {
  139. return $alias;
  140. }
  141. return null;
  142. }
  143. /**
  144. * Returns language encoding from site settings.
  145. *
  146. * @param string $languageId Language id to check.
  147. *
  148. * @return string|null
  149. */
  150. public static function getCultureEncoding(string $languageId): ?string
  151. {
  152. static $cultureEncoding;
  153. if ($cultureEncoding === null)
  154. {
  155. $cultureEncoding = [];
  156. $iterator = Main\Localization\CultureTable::getList([
  157. 'select' => ['ID', 'CODE', 'CHARSET'],
  158. 'cache' => ['ttl' => self::CACHE_TTL],
  159. ]);
  160. while ($row = $iterator->fetch())
  161. {
  162. $cultureEncoding[mb_strtolower($row['CODE'])] = mb_strtolower($row['CHARSET']);
  163. }
  164. }
  165. return $cultureEncoding[$languageId] ?: null;
  166. }
  167. /**
  168. * Returns list of all languages in the site settings.
  169. *
  170. * @return string[]
  171. */
  172. public static function getLanguages(): array
  173. {
  174. static $languages;
  175. if ($languages === null)
  176. {
  177. $languages = [];
  178. $iterator = Main\Localization\LanguageTable::getList([
  179. 'select' => ['ID', 'SORT'],
  180. 'order' => ['SORT' => 'ASC'],
  181. 'cache' => ['ttl' => self::CACHE_TTL],
  182. ]);
  183. while ($row = $iterator->fetch())
  184. {
  185. $languages[] = $row['ID'];
  186. }
  187. }
  188. return $languages;
  189. }
  190. /**
  191. * Returns list of enabled languages in the site settings.
  192. *
  193. * @return string[]
  194. */
  195. public static function getEnabledLanguages(): array
  196. {
  197. static $languages;
  198. if ($languages === null)
  199. {
  200. $languages = [];
  201. $iterator = Main\Localization\LanguageTable::getList([
  202. 'select' => ['ID', 'SORT'],
  203. 'filter' => ['=ACTIVE' => 'Y'],
  204. 'order' => ['SORT' => 'ASC'],
  205. 'cache' => ['ttl' => self::CACHE_TTL],
  206. ]);
  207. while ($row = $iterator->fetch())
  208. {
  209. $languages[] = $row['ID'];
  210. }
  211. }
  212. return $languages;
  213. }
  214. /**
  215. * Returns list of language names from the site settings.
  216. *
  217. * @param string[] $languageIds Languages list to get name.
  218. *
  219. * @return array
  220. */
  221. public static function getLanguagesTitle($languageIds): array
  222. {
  223. static $cache = [];
  224. $cacheId = implode('-', $languageIds);
  225. if (!isset($cache[$cacheId]))
  226. {
  227. $cache[$cacheId] = [];
  228. $iterator = Main\Localization\LanguageTable::getList([
  229. 'select' => ['ID', 'NAME'],
  230. 'filter' => [
  231. 'ID' => $languageIds,
  232. '=ACTIVE' => 'Y'
  233. ],
  234. 'order' => ['SORT' => 'ASC'],
  235. 'cache' => ['ttl' => self::CACHE_TTL],
  236. ]);
  237. while ($row = $iterator->fetch())
  238. {
  239. $cache[$cacheId][$row['ID']] = $row['NAME'];
  240. }
  241. }
  242. return $cache[$cacheId];
  243. }
  244. /**
  245. * Returns list of available in system translation languages.
  246. *
  247. * @return string[]
  248. */
  249. public static function getAvailableLanguages(): array
  250. {
  251. static $languages;
  252. if ($languages === null)
  253. {
  254. $languages = array_unique(array_merge(
  255. self::getAvailableDefaultLanguages(),
  256. self::getTranslationRepositoryLanguages()
  257. ));
  258. }
  259. return $languages;
  260. }
  261. /**
  262. * Returns list of available default translation languages.
  263. *
  264. * @return string[]
  265. */
  266. public static function getAvailableDefaultLanguages(): array
  267. {
  268. static $languages;
  269. if ($languages === null)
  270. {
  271. $languages = [];
  272. $langDirList = new Main\IO\Directory(Main\Application::getDocumentRoot(). '/bitrix/modules/main/lang/');
  273. foreach ($langDirList->getChildren() as $langDir)
  274. {
  275. $langId = $langDir->getName();
  276. if (in_array($langId, Translate\IGNORE_FS_NAMES, true) || !$langDir->isDirectory())
  277. {
  278. continue;
  279. }
  280. $languages[] = $langId;
  281. }
  282. }
  283. return $languages;
  284. }
  285. /**
  286. * Returns list of available in system translation languages.
  287. *
  288. * @return string[]
  289. */
  290. public static function getTranslationRepositoryLanguages(): array
  291. {
  292. static $languages;
  293. if ($languages === null)
  294. {
  295. $languages = [];
  296. if (Main\Localization\Translation::useTranslationRepository())
  297. {
  298. $langDirList = new Main\IO\Directory(Main\Localization\Translation::getTranslationRepositoryPath());
  299. foreach ($langDirList->getChildren() as $langDir)
  300. {
  301. $langId = $langDir->getName();
  302. if (in_array($langId, Translate\IGNORE_FS_NAMES, true) || !$langDir->isDirectory())
  303. {
  304. continue;
  305. }
  306. $languages[] = $langId;
  307. }
  308. }
  309. }
  310. return $languages;
  311. }
  312. /**
  313. * Folders list allowed to translation edit.
  314. *
  315. * @return string[]
  316. */
  317. public static function getInitPath(): array
  318. {
  319. /** @var string[] */
  320. static $initFolders;
  321. if ($initFolders === null)
  322. {
  323. $initFolders = [];
  324. $folders = (string)Main\Config\Option::get(
  325. 'translate',
  326. self::OPTION_INIT_FOLDERS,
  327. Translate\Config::getModuleDefault(Translate\Config::OPTION_INIT_FOLDERS)
  328. );
  329. $folders = explode(',', trim($folders));
  330. foreach ($folders as $oneFolder)
  331. {
  332. if (!empty($oneFolder))
  333. {
  334. $oneFolder = Translate\IO\Path::normalize($oneFolder);
  335. $initFolders[] = '/'.ltrim($oneFolder, '/');
  336. }
  337. }
  338. }
  339. return $initFolders;
  340. }
  341. /**
  342. * Returns the default translation folder path.
  343. *
  344. * @return string
  345. */
  346. public static function getDefaultPath(): string
  347. {
  348. static $defaultPath;
  349. if ($defaultPath === null)
  350. {
  351. $folders = explode(',', self::getModuleDefault(self::OPTION_INIT_FOLDERS));
  352. $defaultPath = $folders[0];
  353. }
  354. return $defaultPath;
  355. }
  356. /**
  357. * Returns setting of necessity to backup language files.
  358. *
  359. * @return bool
  360. */
  361. public static function needToBackUpFiles(): bool
  362. {
  363. static $needToBackUpFiles;
  364. if ($needToBackUpFiles === null)
  365. {
  366. $def = self::getModuleDefault(self::OPTION_BACKUP_FILES);
  367. $needToBackUpFiles = (bool)(Main\Config\Option::get('translate', self::OPTION_BACKUP_FILES, $def) === 'Y');
  368. }
  369. return $needToBackUpFiles;
  370. }
  371. /**
  372. * Returns disposition of backup folder for backuped language files.
  373. *
  374. * @return string
  375. */
  376. public static function getBackupFolder(): string
  377. {
  378. static $backupFolder;
  379. if ($backupFolder === null)
  380. {
  381. $confOption = Main\Config\Option::get('translate', self::OPTION_BACKUP_FOLDER, '');
  382. if (!empty($confOption))
  383. {
  384. if (mb_strpos($confOption, '/') === 0)
  385. {
  386. $backupFolder = $confOption;
  387. }
  388. elseif (strncasecmp(PHP_OS, 'WIN', 3) === 0 && preg_match("#^[a-z]{1}:/#i", $confOption))
  389. {
  390. $backupFolder = $confOption;
  391. }
  392. else
  393. {
  394. $backupFolder = Main\Application::getDocumentRoot(). '/'. $confOption;
  395. }
  396. }
  397. else
  398. {
  399. $defOption = self::getModuleDefault(self::OPTION_BACKUP_FOLDER);
  400. if (empty($defOption))
  401. {
  402. $defOption = 'bitrix/backup/translate/';
  403. }
  404. $backupFolder = Main\Application::getDocumentRoot(). '/'. $defOption;
  405. }
  406. }
  407. return $backupFolder;
  408. }
  409. /**
  410. * Returns setting of necessity to sort phrases in language files.
  411. *
  412. * @return bool
  413. */
  414. public static function needToSortPhrases(): bool
  415. {
  416. static $needToSortPhrases;
  417. if ($needToSortPhrases === null)
  418. {
  419. $def = self::getModuleDefault(self::OPTION_SORT_PHRASES);
  420. $needToSortPhrases = (bool)(Main\Config\Option::get('translate', self::OPTION_SORT_PHRASES, $def) === 'Y');
  421. }
  422. return $needToSortPhrases;
  423. }
  424. /**
  425. * Returns list of languages in that is unnecessary to sort phrases.
  426. *
  427. * @return string[]
  428. */
  429. public static function getNonSortPhraseLanguages(): array
  430. {
  431. static $nonSortPhraseLanguages;
  432. if ($nonSortPhraseLanguages === null)
  433. {
  434. $nonSortPhraseLanguages = [];
  435. $def = self::getModuleDefault(self::OPTION_DONT_SORT_LANGUAGES);
  436. $nonSortPhraseLanguages = Main\Config\Option::get('translate', self::OPTION_DONT_SORT_LANGUAGES, $def);
  437. if (!is_array($nonSortPhraseLanguages))
  438. {
  439. $nonSortPhraseLanguages = explode(',', $nonSortPhraseLanguages);
  440. }
  441. }
  442. return $nonSortPhraseLanguages;
  443. }
  444. /**
  445. * Returns disposition of the asset or export folder.
  446. *
  447. * @return string|null
  448. */
  449. public static function getExportFolder(): ?string
  450. {
  451. static $exportFolder = -1;
  452. if ($exportFolder === -1)
  453. {
  454. $exportFolder = null;// '/bitrix/updates/_langs/';
  455. $confOption = Main\Config\Option::get('translate', self::OPTION_EXPORT_FOLDER, '');
  456. if (!empty($confOption))
  457. {
  458. if (mb_strpos($confOption, Main\Application::getDocumentRoot()) === 0)
  459. {
  460. $exportFolder = $confOption;
  461. }
  462. else
  463. {
  464. $exportFolder = Main\Application::getDocumentRoot(). '/'. $confOption;
  465. }
  466. }
  467. }
  468. return $exportFolder;
  469. }
  470. }