1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
translation.php
См. документацию.
1<?php
2namespace Bitrix\Main\Localization;
3
4use Bitrix\Main;
5use Bitrix\Main\IO\Path;
6use Bitrix\Main\Config\Configuration;
7
9{
11 private static $allowConvertEncoding = null;
13 private static $useTranslationRepository = null;
15 private static $translationRepositoryPath = null;
17 private static $currentEncoding = null;
19 private static $needConvertEncoding = array();
21 private static $map = array();
22
23 const CACHE_ID = 'TranslationLoadMapCache';
24 const CACHE_TTL = 3600;
25
32 public static function isDefaultTranslationLang($lang)
33 {
34 return
35 ($lang === 'ru') ||
36 ($lang === 'en') ||
37 ($lang === 'de') ||
38 ($lang === 'kz');
39 }
40
47 public static function getDefaultTranslationEncoding($lang)
48 {
49 return 'utf-8';
50 }
51
52
59 public static function getSourceEncoding($lang)
60 {
61 $encoding = 'utf-8';
62 if (self::useTranslationRepository() || self::allowConvertEncoding())
63 {
64 if (self::isDefaultTranslationLang($lang))
65 {
67 }
68 }
69
70 if (empty($encoding))
71 {
73 if ($culture)
74 {
75 $encoding = $culture['CHARSET'];
76 }
77 }
78
79 if (empty($encoding))
80 {
81 $encoding = self::getCurrentEncoding();
82 }
83
84 return $encoding;
85 }
86
87
94 public static function setCurrentEncoding($encoding)
95 {
96 self::$currentEncoding = $encoding;
97 }
98
104 public static function getCurrentEncoding()
105 {
106 if (self::$currentEncoding === null)
107 {
108 self::$currentEncoding = 'utf-8';
109 }
110
111 return self::$currentEncoding;
112 }
113
122 public static function needConvertEncoding($language, $targetEncoding = null)
123 {
124 if (!isset(self::$needConvertEncoding[$language]) || self::$needConvertEncoding[$language] === null)
125 {
126 self::$needConvertEncoding[$language] = false;
127
128 if (self::allowConvertEncoding())
129 {
130 if ($targetEncoding === null)
131 {
132 $targetEncoding = self::getCurrentEncoding();
133 }
134 $sourceEncoding = self::getSourceEncoding($language);
135 self::$needConvertEncoding[$language] = ($targetEncoding != $sourceEncoding);
136 }
137 }
138
139 return self::$needConvertEncoding[$language];
140 }
141
142
151 {
152 $needConvert = false;
153 if (self::allowConvertEncoding())
154 {
155 if (self::getDeveloperRepositoryPath() !== null)
156 {
157 $needConvert = (stripos($langFile, self::getDeveloperRepositoryPath()) === 0);
158 }
159 if (!$needConvert && self::useTranslationRepository())
160 {
161 $needConvert = (stripos($langFile, self::getTranslationRepositoryPath()) === 0);
162 }
163 }
164
165 return $needConvert;
166 }
167
173 public static function useTranslationRepository()
174 {
175 if (self::$useTranslationRepository === null)
176 {
177 self::$useTranslationRepository = false;
178
179 if(self::getTranslationRepositoryPath() !== null)
180 {
181 self::$useTranslationRepository = true;
182 }
183 }
184
185 return self::$useTranslationRepository;
186 }
187
193 public static function getTranslationRepositoryPath()
194 {
195 if(self::$translationRepositoryPath === null)
196 {
197 $config = Configuration::getValue('translation');
198
199 if ($config !== null && !empty($config['translation_repository']))
200 {
201 $translationRepositoryPath = realpath($config['translation_repository']);
202 if (file_exists($translationRepositoryPath))
203 {
204 self::$translationRepositoryPath = Path::normalize($translationRepositoryPath);
205 }
206 }
207 }
208
209 return self::$translationRepositoryPath;
210 }
211
217 public static function allowConvertEncoding()
218 {
219 if(self::$allowConvertEncoding === null)
220 {
221 self::$allowConvertEncoding = false;
222
223 $config = Configuration::getValue('translation');
224
225 if ($config !== null && !empty($config['convert_encoding']))
226 {
227 self::$allowConvertEncoding = ($config['convert_encoding'] === true);
228 }
229 }
230
231 return self::$allowConvertEncoding;
232 }
233
239 public static function getDeveloperRepositoryPath()
240 {
241 static $developerRepositoryPath, $wasChecked;
242 if($wasChecked === null)
243 {
244 $wasChecked = true;
245 $config = Configuration::getValue('translation');
246
247 if ($config !== null && !empty($config['developer_repository']))
248 {
249 $developerRepositoryPath = realpath($config['developer_repository']);
250 if (file_exists($developerRepositoryPath))
251 {
252 $developerRepositoryPath = Path::normalize($developerRepositoryPath);
253 }
254 }
255 }
256
257 return $developerRepositoryPath;
258 }
259
267 public static function convertLangPath($langFile, $language)
268 {
269 if (empty($language) || !(self::useTranslationRepository() || self::getDeveloperRepositoryPath() !== null))
270 {
271 return $langFile;
272 }
273
274 static $documentRoot;
275 if ($documentRoot === null)
276 {
278 }
279
280 if (self::useTranslationRepository() && !self::isDefaultTranslationLang($language))
281 {
282 $modulePath = self::getTranslationRepositoryPath().'/'.$language.'/';
283 }
284 elseif (self::getDeveloperRepositoryPath() !== null)
285 {
286 $modulePath = self::getDeveloperRepositoryPath(). '/';
287 }
288 elseif (self::isDefaultTranslationLang($language))
289 {
290 $modulePath = $documentRoot. '/bitrix/modules/';
291 }
292 else
293 {
294 return $langFile;
295 }
296
297 if (str_contains($langFile, '\\'))
298 {
299 $langFile = str_replace('\\', '/', $langFile);
300 }
301 if (str_contains($langFile, '//'))
302 {
303 $langFile = str_replace('//', '/', $langFile);
304 }
305
306 // linked
307 if (self::getDeveloperRepositoryPath() !== null)
308 {
309 if (mb_strpos($langFile, self::getDeveloperRepositoryPath()) === 0)
310 {
311 $langFile = str_replace(
312 self::getDeveloperRepositoryPath(). '/',
313 $modulePath,
315 );
316
317 return $langFile;
318 }
319 }
320
321 // module lang
322 if (str_starts_with($langFile, $documentRoot . '/bitrix/modules/'))
323 {
324 $langFile = str_replace(
325 $documentRoot.'/bitrix/modules/',
326 $modulePath,
328 );
329
330 return $langFile;
331 }
332
334
335 $langPathParts = preg_split('#[/]+#', trim(str_replace($documentRoot, '', $langFile), '/'), 6);
336 if (
337 empty($langPathParts)
338 || $langPathParts[0] !== 'bitrix'
339 || empty($langPathParts[1])
340 || empty($langPathParts[2])
341 || empty($langPathParts[3])
342 )
343 {
344 return $langFile;
345 }
346
347 $testEntry = $langPathParts[1];
348 switch ($testEntry)
349 {
350 // bitrix/mobileapp/[moduleName] -> [moduleName]/install/mobileapp/[moduleName]
351 case 'mobileapp':
352 $moduleName = $langPathParts[2];
353 if (isset(self::$map[$moduleName][$testEntry], self::$map[$moduleName][$testEntry][$moduleName]))
354 {
355 $testEntry = 'mobileapp/'. $moduleName;
356 $langFile = str_replace(
357 $documentRoot.'/bitrix/mobileapp/'. $moduleName. '/',
358 $modulePath.''.$moduleName.'/install/mobileapp/'. $moduleName. '/',
360 );
361 }
362 break;
363
364 // bitrix/templates/[templateName] -> [moduleName]/install/templates/[templateName]
365 case 'templates':
366 $templateName = $langPathParts[2];
367 foreach (self::$map as $moduleName => $moduleEntries)
368 {
369 if (isset(self::$map[$moduleName][$testEntry], self::$map[$moduleName][$testEntry][$templateName]))
370 {
371 $langFile = str_replace(
372 $documentRoot.'/bitrix/templates/'.$templateName.'/',
373 $modulePath.''.$moduleName.'/install/templates/'. $templateName .'/',
375 );
376 break;
377 }
378 }
379 break;
380
381 // bitrix/components/bitrix/[componentName] -> [moduleName]/install/components/bitrix/[componentName]
382 // bitrix/activities/bitrix/[activityName] -> [moduleName]/install/activities/bitrix/[activityName]
383 // bitrix/wizards/bitrix/[wizardsName] -> [moduleName]/install/wizards/bitrix/[wizardsName]
384 // bitrix/gadgets/bitrix/[gadgetName] -> [moduleName]/install/gadgets/bitrix/[gadgetName]
385 case 'components':
386 case 'activities':
387 case 'wizards':
388 case 'gadgets':
389 case 'blocks':
390 if ($langPathParts[2] !== 'bitrix')
391 {
392 break;
393 }
394 $searchEntryName = $langPathParts[3];
395 foreach (self::$map as $moduleName => $moduleEntries)
396 {
397 if (isset(self::$map[$moduleName][$testEntry], self::$map[$moduleName][$testEntry][$searchEntryName]))
398 {
399 $langFile = str_replace(
400 $documentRoot.'/bitrix/'.$testEntry.'/bitrix/'.$searchEntryName.'/',
401 $modulePath.''.$moduleName.'/install/'.$testEntry.'/bitrix/'. $searchEntryName. '/',
403 );
404 break;
405 }
406 }
407 break;
408
409 // bitrix/js/[moduleName]/[smth] -> [moduleName]/install/js/[moduleName]/[smth]
410 // bitrix/js/[moduleName]/[smth] -> [moduleName]/install/public/js/[moduleName]/[smth]
411 case 'js':
412 $libraryNamespace = $langPathParts[2]. '/'. $langPathParts[3];
413
414 foreach (self::$map as $moduleName => $moduleEntries)
415 {
416 if (isset(self::$map[$moduleName][$testEntry], self::$map[$moduleName][$testEntry][$libraryNamespace]))
417 {
418 $langFile = str_replace(
419 $documentRoot.'/bitrix/'.$testEntry.'/'.$libraryNamespace.'/',
420 $modulePath.''.$moduleName.'/install/'.$testEntry.'/'.$libraryNamespace.'/',
422 );
423 break;
424 }
425 if (isset(self::$map[$moduleName]["public/{$testEntry}"], self::$map[$moduleName]["public/{$testEntry}"][$libraryNamespace]))
426 {
427 $langFile = str_replace(
428 $documentRoot.'/bitrix/'.$testEntry.'/'.$libraryNamespace.'/',
429 $modulePath.''.$moduleName.'/install/public/'.$testEntry.'/'.$libraryNamespace.'/',
431 );
432 break;
433 }
434 }
435 break;
436
437 // bitrix/[moduleName]/payment/[paymentHandler] -> [moduleName]/payment/[paymentHandler]
438 case 'payment':
439 $searchEntryName = $langPathParts[3];
440 foreach (self::$map as $moduleName => $moduleEntries)
441 {
442 if (isset(self::$map[$moduleName][$testEntry], self::$map[$moduleName][$testEntry][$searchEntryName]))
443 {
444 $langFile = str_replace(
445 $documentRoot.'/bitrix/modules/'.$moduleName.'/'.$testEntry.'/',
446 $modulePath.''.$moduleName.'/'.$testEntry.'/',
448 );
449 break;
450 }
451 }
452 break;
453 }
454
455 return $langFile;
456 }
457
458
464 public static function loadMap()
465 {
466 if (empty(self::$map))
467 {
468 $cacheManager = Main\Application::getInstance()->getManagedCache();
469 if ($cacheManager->read(static::CACHE_TTL, static::CACHE_ID))
470 {
471 self::$map = $cacheManager->get(static::CACHE_ID);
472 }
473 }
474
475 if (empty(self::$map))
476 {
477 $testForExistence = array(
478 'templates',
479 'components',
480 'activities',
481 'wizards',
482 'gadgets',
483 'js',
484 'public/js',
485 'blocks',
486 'payment',
487 'mobileapp',
488 );
489 $bxRoot = Main\Application::getDocumentRoot(). '/bitrix/modules/';
490 $modulesList = new Main\IO\Directory($bxRoot);
491 foreach ($modulesList->getChildren() as $moduleDirectory)
492 {
493 if ($moduleDirectory->isDirectory())
494 {
495 $moduleName = $moduleDirectory->getName();
496 if (!str_contains($moduleName, '.') || str_starts_with($moduleName, 'bitrix.'))
497 {
498 self::$map[$moduleName] = array();
499 foreach ($testForExistence as $testEntry)
500 {
501 $testPath = $bxRoot. '/'. $moduleName. '/install/'. $testEntry;
502 if ($testEntry === 'templates' || $testEntry === 'mobileapp')
503 {
504 $testPath .= '/';
505 }
506 elseif ($testEntry === 'js' || $testEntry === 'public/js')
507 {
508 $testPath .= '/'. $moduleName. '/';
509 }
510 elseif ($testEntry === 'payment')
511 {
512 $testPath = $bxRoot. '/'. $moduleName. '/'. $testEntry;
513 }
514 else
515 {
516 $testPath .= '/bitrix/';
517 }
518
519 $testDirectory = new Main\IO\Directory($testPath);
520 if ($testDirectory->isExists())
521 {
522 self::$map[$moduleName][$testEntry] = array();
523 foreach ($testDirectory->getChildren() as $testDirectoryEntry)
524 {
525 if ($testDirectoryEntry->isDirectory())
526 {
527 if ($testEntry === 'js' || $testEntry === 'public/js')
528 {
529 self::$map[$moduleName][$testEntry][$moduleName.'/'.$testDirectoryEntry->getName()] = 1;
530 }
531 else
532 {
533 self::$map[$moduleName][$testEntry][$testDirectoryEntry->getName()] = 1;
534 }
535 }
536 }
537 }
538 }
539 }
540 }
541 }
542
543 $cacheManager->set(static::CACHE_ID, static::$map);
544 }
545
546 return self::$map;
547 }
548
554 public static function getEncodings($language, $langFile)
555 {
556 static $encodingCache = array();
557
558 if(isset($encodingCache[$language]))
559 {
560 list($convertEncoding, $targetEncoding, $sourceEncoding) = $encodingCache[$language];
561 }
562 else
563 {
564 $convertEncoding = self::needConvertEncoding($language);
565 $targetEncoding = $sourceEncoding = '';
566 if($convertEncoding)
567 {
568 $targetEncoding = self::getCurrentEncoding();
569 $sourceEncoding = self::getSourceEncoding($language);
570 }
571
572 $encodingCache[$language] = array($convertEncoding, $targetEncoding, $sourceEncoding);
573 }
574
575 if($convertEncoding)
576 {
578 }
579
580 return array($convertEncoding, $targetEncoding, $sourceEncoding);
581 }
582}
static getDocumentRoot()
Определения application.php:736
static getInstance()
Определения application.php:98
static getSourceEncoding($lang)
Определения translation.php:59
static useTranslationRepository()
Определения translation.php:173
static getCurrentEncoding()
Определения translation.php:104
static needConvertEncoding($language, $targetEncoding=null)
Определения translation.php:122
static getDefaultTranslationEncoding($lang)
Определения translation.php:47
static checkPathRestrictionConvertEncoding($langFile)
Определения translation.php:150
static getTranslationRepositoryPath()
Определения translation.php:193
static convertLangPath($langFile, $language)
Определения translation.php:267
static isDefaultTranslationLang($lang)
Определения translation.php:32
static setCurrentEncoding($encoding)
Определения translation.php:94
static allowConvertEncoding()
Определения translation.php:217
static getDeveloperRepositoryPath()
Определения translation.php:239
static getEncodings($language, $langFile)
Определения translation.php:554
static getRow(array $parameters)
Определения datamanager.php:398
$langFile
Определения .description.php:2
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
foreach(['Bitrix\\Main'=> '/lib', 'Psr\\Container'=> '/vendor/psr/container/src', 'Psr\\Log'=> '/vendor/psr/log/src', 'Psr\\Http\\Message'=> '/vendor/psr/http-message/src', 'Psr\\Http\\Client'=> '/vendor/psr/http-client/src', 'Http\\Promise'=> '/vendor/php-http/promise/src', 'PHPMailer\\PHPMailer'=> '/vendor/phpmailer/phpmailer/src', 'GeoIp2'=> '/vendor/geoip2/geoip2/src', 'MaxMind\\Db'=> '/vendor/maxmind-db/reader/src/MaxMind/Db', 'PhpParser'=> '/vendor/nikic/php-parser/lib/PhpParser', 'Recurr'=> '/vendor/simshaun/recurr/src/Recurr',] as $namespace=> $namespacePath) $documentRoot
Определения autoload.php:27
if(!defined('SITE_ID')) $lang
Определения include.php:91
$culture
Определения include.php:61
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$config
Определения quickway.php:69