Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
translation.php
1<?php
3
8
10{
12 private static $allowConvertEncoding = null;
14 private static $useTranslationRepository = null;
16 private static $translationRepositoryPath = null;
18 private static $currentEncoding = null;
20 private static $needConvertEncoding = array();
22 private static $map = array();
23
24 const CACHE_ID = 'TranslationLoadMapCache';
25 const CACHE_TTL = 3600;
26
33 public static function isDefaultTranslationLang($lang)
34 {
35 return
36 ($lang === 'ru') ||
37 ($lang === 'en') ||
38 ($lang === 'de');
39 }
40
47 public static function getDefaultTranslationEncoding($lang)
48 {
49 static $sourceEncoding = [
50 'ru' => 'windows-1251',
51 'en' => 'iso-8859-1',
52 'de' => 'iso-8859-15',
53 ];
54 if (isset($sourceEncoding[$lang]))
55 {
56 return $sourceEncoding[$lang];
57 }
58
59 return 'utf-8';
60 }
61
62
69 public static function getSourceEncoding($lang)
70 {
71 $encoding = '';
72 if (self::useTranslationRepository() || self::allowConvertEncoding())
73 {
74 if (self::isDefaultTranslationLang($lang))
75 {
76 $encoding = self::getDefaultTranslationEncoding($lang);
77 }
78 else
79 {
80 $encoding = 'utf-8';
81 }
82 }
83 elseif (Main\Application::isUtfMode() || defined('BX_UTF'))
84 {
85 $encoding = 'utf-8';
86 }
87
88 if (empty($encoding))
89 {
90 $culture = Main\Localization\CultureTable::getRow(array('filter' => array('=CODE' => $lang)));
91 if ($culture)
92 {
93 $encoding = $culture['CHARSET'];
94 }
95 }
96
97 if (empty($encoding))
98 {
99 $encoding = self::getCurrentEncoding();
100 }
101
102 return $encoding;
103 }
104
105
112 public static function setCurrentEncoding($encoding)
113 {
114 self::$currentEncoding = $encoding;
115 }
116
122 public static function getCurrentEncoding()
123 {
124 if (self::$currentEncoding === null)
125 {
126 $encoding = null;
127 // site settings
128 if (Main\Application::isUtfMode() || defined('BX_UTF'))
129 {
130 $encoding = 'utf-8';
131 }
132 elseif (defined('SITE_CHARSET') && (SITE_CHARSET <> ''))
133 {
134 $encoding = SITE_CHARSET;
135 }
136 elseif (defined('LANG_CHARSET') && (LANG_CHARSET <> ''))
137 {
138 $encoding = LANG_CHARSET;
139 }
140 else
141 {
142 $context = Context::getCurrent();
143 if ($context instanceof Main\Context)
144 {
145 $culture = $context->getCulture();
146 if ($culture instanceof Context\Culture)
147 {
148 $encoding = $culture->getCharset();
149 }
150 }
151 }
152 // default settings
153 if ($encoding === null)
154 {
155 if (Configuration::getValue('default_charset') !== null)
156 {
157 $encoding = Configuration::getValue('default_charset');
158 }
159 elseif (defined('BX_DEFAULT_CHARSET') && (BX_DEFAULT_CHARSET <> ''))
160 {
161 $encoding = BX_DEFAULT_CHARSET;
162 }
163 else
164 {
165 $encoding = 'windows-1251';
166 }
167 }
168
169 self::$currentEncoding = mb_strtolower($encoding);
170 }
171
172 return self::$currentEncoding;
173 }
174
183 public static function needConvertEncoding($language, $targetEncoding = null)
184 {
185 if (!isset(self::$needConvertEncoding[$language]) || self::$needConvertEncoding[$language] === null)
186 {
187 self::$needConvertEncoding[$language] = false;
188
189 if (self::allowConvertEncoding())
190 {
191 if ($targetEncoding === null)
192 {
193 $targetEncoding = self::getCurrentEncoding();
194 }
195 $sourceEncoding = self::getSourceEncoding($language);
196 self::$needConvertEncoding[$language] = ($targetEncoding != $sourceEncoding);
197 }
198 }
199
200 return self::$needConvertEncoding[$language];
201 }
202
203
211 public static function checkPathRestrictionConvertEncoding($langFile)
212 {
213 $needConvert = false;
214 if (self::allowConvertEncoding())
215 {
216 if (self::getDeveloperRepositoryPath() !== null)
217 {
218 $needConvert = (stripos($langFile, self::getDeveloperRepositoryPath()) === 0);
219 }
220 if (!$needConvert && self::useTranslationRepository())
221 {
222 $needConvert = (stripos($langFile, self::getTranslationRepositoryPath()) === 0);
223 }
224 }
225
226 return $needConvert;
227 }
228
234 public static function useTranslationRepository()
235 {
236 if (self::$useTranslationRepository === null)
237 {
238 self::$useTranslationRepository = false;
239
240 if(self::getTranslationRepositoryPath() !== null)
241 {
242 self::$useTranslationRepository = true;
243 }
244 }
245
246 return self::$useTranslationRepository;
247 }
248
254 public static function getTranslationRepositoryPath()
255 {
256 if(self::$translationRepositoryPath === null)
257 {
258 $config = Configuration::getValue('translation');
259
260 if ($config !== null && !empty($config['translation_repository']))
261 {
262 $translationRepositoryPath = realpath($config['translation_repository']);
263 if (file_exists($translationRepositoryPath))
264 {
265 self::$translationRepositoryPath = Path::normalize($translationRepositoryPath);
266 }
267 }
268 }
269
270 return self::$translationRepositoryPath;
271 }
272
278 public static function allowConvertEncoding()
279 {
280 if(self::$allowConvertEncoding === null)
281 {
282 self::$allowConvertEncoding = false;
283
284 $config = Configuration::getValue('translation');
285
286 if ($config !== null && !empty($config['convert_encoding']))
287 {
288 self::$allowConvertEncoding = ($config['convert_encoding'] === true);
289 }
290 }
291
292 return self::$allowConvertEncoding;
293 }
294
300 public static function getDeveloperRepositoryPath()
301 {
302 static $developerRepositoryPath, $wasChecked;
303 if($wasChecked === null)
304 {
305 $wasChecked = true;
306 $config = Configuration::getValue('translation');
307
308 if ($config !== null && !empty($config['developer_repository']))
309 {
310 $developerRepositoryPath = realpath($config['developer_repository']);
311 if (file_exists($developerRepositoryPath))
312 {
313 $developerRepositoryPath = Path::normalize($developerRepositoryPath);
314 }
315 }
316 }
317
318 return $developerRepositoryPath;
319 }
320
328 public static function convertLangPath($langFile, $language)
329 {
330 if (empty($language) || !(self::useTranslationRepository() || self::getDeveloperRepositoryPath() !== null))
331 {
332 return $langFile;
333 }
334
335 static $documentRoot;
336 if ($documentRoot === null)
337 {
338 $documentRoot = Path::normalize(Main\Application::getDocumentRoot());
339 }
340
341 if (self::useTranslationRepository() && !self::isDefaultTranslationLang($language))
342 {
343 $modulePath = self::getTranslationRepositoryPath().'/'.$language.'/';
344 }
345 elseif (self::getDeveloperRepositoryPath() !== null)
346 {
347 $modulePath = self::getDeveloperRepositoryPath(). '/';
348 }
349 elseif (self::isDefaultTranslationLang($language))
350 {
351 $modulePath = $documentRoot. '/bitrix/modules/';
352 }
353 else
354 {
355 return $langFile;
356 }
357
358 if (strpos($langFile, '\\') !== false)
359 {
360 $langFile = str_replace('\\', '/', $langFile);
361 }
362 if (strpos($langFile, '//') !== false)
363 {
364 $langFile = str_replace('//', '/', $langFile);
365 }
366
367 // linked
368 if (self::getDeveloperRepositoryPath() !== null)
369 {
370 if (mb_strpos($langFile, self::getDeveloperRepositoryPath()) === 0)
371 {
372 $langFile = str_replace(
373 self::getDeveloperRepositoryPath(). '/',
374 $modulePath,
375 $langFile
376 );
377
378 return $langFile;
379 }
380 }
381
382 // module lang
383 if (strpos($langFile, $documentRoot. '/bitrix/modules/') === 0)
384 {
385 $langFile = str_replace(
386 $documentRoot.'/bitrix/modules/',
387 $modulePath,
388 $langFile
389 );
390
391 return $langFile;
392 }
393
395
396 $langPathParts = preg_split('#[/]+#', trim(str_replace($documentRoot, '', $langFile), '/'), 6);
397 if (
398 empty($langPathParts)
399 || $langPathParts[0] !== 'bitrix'
400 || empty($langPathParts[1])
401 || empty($langPathParts[2])
402 || empty($langPathParts[3])
403 )
404 {
405 return $langFile;
406 }
407
408 $testEntry = $langPathParts[1];
409 switch ($testEntry)
410 {
411 // bitrix/mobileapp/[moduleName] -> [moduleName]/install/mobileapp/[moduleName]
412 case 'mobileapp':
413 $moduleName = $langPathParts[2];
414 if (isset(self::$map[$moduleName][$testEntry], self::$map[$moduleName][$testEntry][$moduleName]))
415 {
416 $testEntry = 'mobileapp/'. $moduleName;
417 $langFile = str_replace(
418 $documentRoot.'/bitrix/mobileapp/'. $moduleName. '/',
419 $modulePath.''.$moduleName.'/install/mobileapp/'. $moduleName. '/',
420 $langFile
421 );
422 }
423 break;
424
425 // bitrix/templates/[templateName] -> [moduleName]/install/templates/[templateName]
426 case 'templates':
427 $templateName = $langPathParts[2];
428 foreach (self::$map as $moduleName => $moduleEntries)
429 {
430 if (isset(self::$map[$moduleName][$testEntry], self::$map[$moduleName][$testEntry][$templateName]))
431 {
432 $langFile = str_replace(
433 $documentRoot.'/bitrix/templates/'.$templateName.'/',
434 $modulePath.''.$moduleName.'/install/templates/'. $templateName .'/',
435 $langFile
436 );
437 break;
438 }
439 }
440 break;
441
442 // bitrix/components/bitrix/[componentName] -> [moduleName]/install/components/bitrix/[componentName]
443 // bitrix/activities/bitrix/[activityName] -> [moduleName]/install/activities/bitrix/[activityName]
444 // bitrix/wizards/bitrix/[wizardsName] -> [moduleName]/install/wizards/bitrix/[wizardsName]
445 // bitrix/gadgets/bitrix/[gadgetName] -> [moduleName]/install/gadgets/bitrix/[gadgetName]
446 case 'components':
447 case 'activities':
448 case 'wizards':
449 case 'gadgets':
450 case 'blocks':
451 if ($langPathParts[2] !== 'bitrix')
452 {
453 break;
454 }
455 $searchEntryName = $langPathParts[3];
456 foreach (self::$map as $moduleName => $moduleEntries)
457 {
458 if (isset(self::$map[$moduleName][$testEntry], self::$map[$moduleName][$testEntry][$searchEntryName]))
459 {
460 $langFile = str_replace(
461 $documentRoot.'/bitrix/'.$testEntry.'/bitrix/'.$searchEntryName.'/',
462 $modulePath.''.$moduleName.'/install/'.$testEntry.'/bitrix/'. $searchEntryName. '/',
463 $langFile
464 );
465 break;
466 }
467 }
468 break;
469
470 // bitrix/js/[moduleName]/[smth] -> [moduleName]/install/js/[moduleName]/[smth]
471 // bitrix/js/[moduleName]/[smth] -> [moduleName]/install/public/js/[moduleName]/[smth]
472 case 'js':
473 $libraryNamespace = $langPathParts[2]. '/'. $langPathParts[3];
474
475 foreach (self::$map as $moduleName => $moduleEntries)
476 {
477 if (isset(self::$map[$moduleName][$testEntry], self::$map[$moduleName][$testEntry][$libraryNamespace]))
478 {
479 $langFile = str_replace(
480 $documentRoot.'/bitrix/'.$testEntry.'/'.$libraryNamespace.'/',
481 $modulePath.''.$moduleName.'/install/'.$testEntry.'/'.$libraryNamespace.'/',
482 $langFile
483 );
484 break;
485 }
486 if (isset(self::$map[$moduleName]["public/{$testEntry}"], self::$map[$moduleName]["public/{$testEntry}"][$libraryNamespace]))
487 {
488 $langFile = str_replace(
489 $documentRoot.'/bitrix/'.$testEntry.'/'.$libraryNamespace.'/',
490 $modulePath.''.$moduleName.'/install/public/'.$testEntry.'/'.$libraryNamespace.'/',
491 $langFile
492 );
493 break;
494 }
495 }
496 break;
497
498 // bitrix/[moduleName]/payment/[paymentHandler] -> [moduleName]/payment/[paymentHandler]
499 case 'payment':
500 $searchEntryName = $langPathParts[3];
501 foreach (self::$map as $moduleName => $moduleEntries)
502 {
503 if (isset(self::$map[$moduleName][$testEntry], self::$map[$moduleName][$testEntry][$searchEntryName]))
504 {
505 $langFile = str_replace(
506 $documentRoot.'/bitrix/modules/'.$moduleName.'/'.$testEntry.'/',
507 $modulePath.''.$moduleName.'/'.$testEntry.'/',
508 $langFile
509 );
510 break;
511 }
512 }
513 break;
514 }
515
516 return $langFile;
517 }
518
519
525 public static function loadMap()
526 {
527 if (empty(self::$map))
528 {
529 $cacheManager = Main\Application::getInstance()->getManagedCache();
530 if ($cacheManager->read(static::CACHE_TTL, static::CACHE_ID))
531 {
532 self::$map = $cacheManager->get(static::CACHE_ID);
533 }
534 }
535
536 if (empty(self::$map))
537 {
538 $testForExistence = array(
539 'templates',
540 'components',
541 'activities',
542 'wizards',
543 'gadgets',
544 'js',
545 'public/js',
546 'blocks',
547 'payment',
548 'mobileapp',
549 );
550 $bxRoot = Main\Application::getDocumentRoot(). '/bitrix/modules/';
551 $modulesList = new Main\IO\Directory($bxRoot);
552 foreach ($modulesList->getChildren() as $moduleDirectory)
553 {
554 if ($moduleDirectory->isDirectory())
555 {
556 $moduleName = $moduleDirectory->getName();
557 if (strpos($moduleName, '.') === false || strpos($moduleName, 'bitrix.') === 0)
558 {
559 self::$map[$moduleName] = array();
560 foreach ($testForExistence as $testEntry)
561 {
562 $testPath = $bxRoot. '/'. $moduleName. '/install/'. $testEntry;
563 if ($testEntry === 'templates' || $testEntry === 'mobileapp')
564 {
565 $testPath .= '/';
566 }
567 elseif ($testEntry === 'js' || $testEntry === 'public/js')
568 {
569 $testPath .= '/'. $moduleName. '/';
570 }
571 elseif ($testEntry === 'payment')
572 {
573 $testPath = $bxRoot. '/'. $moduleName. '/'. $testEntry;
574 }
575 else
576 {
577 $testPath .= '/bitrix/';
578 }
579
580 $testDirectory = new Main\IO\Directory($testPath);
581 if ($testDirectory->isExists())
582 {
583 self::$map[$moduleName][$testEntry] = array();
584 foreach ($testDirectory->getChildren() as $testDirectoryEntry)
585 {
586 if ($testDirectoryEntry->isDirectory())
587 {
588 if ($testEntry === 'js' || $testEntry === 'public/js')
589 {
590 self::$map[$moduleName][$testEntry][$moduleName.'/'.$testDirectoryEntry->getName()] = 1;
591 }
592 else
593 {
594 self::$map[$moduleName][$testEntry][$testDirectoryEntry->getName()] = 1;
595 }
596 }
597 }
598 }
599 }
600 }
601 }
602 }
603
604 $cacheManager->set(static::CACHE_ID, static::$map);
605 }
606
607 return self::$map;
608 }
609
615 public static function getEncodings($language, $langFile)
616 {
617 static $encodingCache = array();
618
619 if(isset($encodingCache[$language]))
620 {
621 list($convertEncoding, $targetEncoding, $sourceEncoding) = $encodingCache[$language];
622 }
623 else
624 {
625 $convertEncoding = self::needConvertEncoding($language);
626 $targetEncoding = $sourceEncoding = '';
627 if($convertEncoding)
628 {
629 $targetEncoding = self::getCurrentEncoding();
630 $sourceEncoding = self::getSourceEncoding($language);
631 }
632
633 $encodingCache[$language] = array($convertEncoding, $targetEncoding, $sourceEncoding);
634 }
635
636 if($convertEncoding)
637 {
638 $convertEncoding = self::checkPathRestrictionConvertEncoding($langFile);
639 }
640
641 return array($convertEncoding, $targetEncoding, $sourceEncoding);
642 }
643}
static getCurrent()
Definition context.php:241
static needConvertEncoding($language, $targetEncoding=null)
static checkPathRestrictionConvertEncoding($langFile)
static convertLangPath($langFile, $language)
static getEncodings($language, $langFile)