Bitrix-D7 22.2
Загрузка...
Поиск...
Не найдено
Класс Loc

Открытые статические члены

static getMessage ($code, $replace=null, $language=null)
 
static loadMessages ($file)
 
static getCurrentLang ()
 
static setCurrentLang ($language)
 
static loadLanguageFile ($file, $language=null, $normalize=true)
 
static loadCustomMessages ($file, $language=null)
 
static getDefaultLang ($lang)
 
static getIncludedFiles ()
 
static getMessagePlural (string $code, int $value, array $replace=null, string $language=null)
 
static getPluralForm ($value, $language='')
 

Подробное описание

Примеры
C:/Bitrix/modules/main/lib/ui/copyright.php.

См. определение в файле loc.php строка 10

Методы

◆ getCurrentLang()

static getCurrentLang ( )
static
Возвращает
string

См. определение в файле loc.php строка 78

79 {
80 if(self::$currentLang === null)
81 {
82 $context = Context::getCurrent();
83 if($context !== null)
84 {
85 $language = $context->getLanguage();
86 if($language !== null)
87 {
88 self::$currentLang = $language;
89 }
90 }
91 }
92 return (self::$currentLang !== null? self::$currentLang : 'en');
93 }
static getCurrent()
Definition: context.php:245

◆ getDefaultLang()

static getDefaultLang (   $lang)
static

Returns default language for specified language. Default language is used when translation is not found.

Аргументы
string$lang
Возвращает
string

См. определение в файле loc.php строка 453

454 {
455 static $subst = ['ua' => 'en', 'kz' => 'ru', 'by' => 'ru', 'ru' => 'ru', 'en' => 'en', 'de' => 'en'];
456 if(isset($subst[$lang]))
457 {
458 return $subst[$lang];
459 }
460
461 $options = Configuration::getValue("default_language");
462 if(isset($options[$lang]))
463 {
464 return $options[$lang];
465 }
466
467 return 'en';
468 }

◆ getIncludedFiles()

static getIncludedFiles ( )
static

Returns previously included lang files.

Возвращает
array

См. определение в файле loc.php строка 474

475 {
476 return self::$includedFiles;
477 }

◆ getMessage()

static getMessage (   $code,
  $replace = null,
  $language = null 
)
static

Returns translation by message code. Loc::loadMessages(FILE) should be called first once per php file

Аргументы
string$code
array$replacee.g. array("#NUM#"=>5)
string$language
Возвращает
string|null
Примеры
C:/Bitrix/modules/main/lib/ui/copyright.php.

См. определение в файле loc.php строка 29

30 {
31 if($language === null)
32 {
33 //function call optimization
34 if(self::$currentLang === null)
35 {
36 $language = self::getCurrentLang();
37 }
38 else
39 {
40 $language = self::$currentLang;
41 }
42 }
43
44 if(!isset(self::$messages[$language][$code]))
45 {
46 self::loadLazy($code, $language);
47 }
48
49 $s = self::$messages[$language][$code] ?? null;
50
51 if (is_array($replace) && $s !== null)
52 {
53 $s = strtr($s, $replace);
54 }
55
56 return $s;
57 }

◆ getMessagePlural()

static getMessagePlural ( string  $code,
int  $value,
array  $replace = null,
string  $language = null 
)
static

Gets plural message by id and number

Аргументы
{string}messageId
{number}value
{object}[replacements]
Возвращает
{?string} Returns plural message by message code and number. Loc::loadMessages(FILE) should be called first once per php file
Аргументы
string$code
int$value
array | null$replacee.g. array("#NUM#"=>5)
string | null$language
Возвращает
string|null

См. определение в файле loc.php строка 497

497 : ?string
498 {
499 $language = (string)$language;
500 if ($language === '')
501 {
502 $language = LANGUAGE_ID;
503 }
504
505 $result = self::getMessage($code . '_PLURAL_' . self::getPluralForm($value, $language), $replace);
506 if ($result === null)
507 {
508 $result = self::getMessage($code . '_PLURAL_1', $replace);
509 }
510
511 return $result;
512 }
static getMessage($code, $replace=null, $language=null)
Definition: loc.php:29

◆ getPluralForm()

static getPluralForm (   $value,
  $language = '' 
)
static

Return language plural form id by number see http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html

Аргументы
{number}value
{string}languageId
Возвращает
integer

См. определение в файле loc.php строка 521

521 : int
522 {
523 $value = (int)$value;
524 $language = (string)$language;
525 if ($language === '')
526 {
527 $language = LANGUAGE_ID;
528 }
529
530 if ($value < 0)
531 {
532 $value = (-1) * $value;
533 }
534
535 switch ($language)
536 {
537 case 'ar':
538 $pluralForm = (($value !== 1) ? 1 : 0);
539/*
540 if ($value === 0)
541 {
542 $pluralForm = 0;
543 }
544 else if ($value === 1)
545 {
546 $pluralForm = 1;
547 }
548 else if ($value === 2)
549 {
550 $pluralForm = 2;
551 }
552 else if (
553 $value % 100 >= 3
554 && $value % 100 <= 10
555 )
556 {
557 $pluralForm = 3;
558 }
559 else if ($value % 100 >= 11)
560 {
561 $pluralForm = 4;
562 }
563 else
564 {
565 $pluralForm = 5;
566 }
567*/
568 break;
569
570 case 'br':
571 case 'fr':
572 case 'tr':
573 $pluralForm = (($value > 1) ? 1 : 0);
574 break;
575
576 case 'de':
577 case 'en':
578 case 'hi':
579 case 'it':
580 case 'la':
581 $pluralForm = (($value !== 1) ? 1 : 0);
582 break;
583
584 case 'ru':
585 case 'ua':
586 if (
587 ($value % 10 === 1)
588 && ($value % 100 !== 11)
589 )
590 {
591 $pluralForm = 0;
592 }
593 else if (
594 ($value % 10 >= 2)
595 && ($value % 10 <= 4)
596 && (
597 ($value % 100 < 10)
598 || ($value % 100 >= 20)
599 )
600 )
601 {
602 $pluralForm = 1;
603 }
604 else
605 {
606 $pluralForm = 2;
607 }
608 break;
609
610 case 'pl':
611 if ($value === 1)
612 {
613 $pluralForm = 0;
614 }
615 else if (
616 $value % 10 >= 2
617 && $value % 10 <= 4
618 && (
619 $value % 100 < 10
620 || $value % 100 >= 20
621 )
622 )
623 {
624 $pluralForm = 1;
625 }
626 else
627 {
628 $pluralForm = 2;
629 }
630 break;
631
632 case 'id':
633 case 'ja':
634 case 'ms':
635 case 'sc':
636 case 'tc':
637 case 'th':
638 case 'vn':
639 $pluralForm = 0;
640 break;
641
642 default:
643 $pluralForm = 1;
644 break;
645 }
646
647 return $pluralForm;
648
649 }

◆ loadCustomMessages()

static loadCustomMessages (   $file,
  $language = null 
)
static

Loads custom messages from the file to overwrite messages by their IDs.

Аргументы
string$file
string | null$language

См. определение в файле loc.php строка 282

283 {
284 if($language === null)
285 {
286 $language = self::getCurrentLang();
287 }
288
289 if(!isset(self::$customMessages[$language]))
290 {
291 self::$customMessages[$language] = array();
292 }
293
294 //let's find language folder and include lang files
295 $mess = self::includeLangFiles(Path::normalize($file), $language, $langFile);
296
297 if (!empty($mess))
298 {
299 [$convertEncoding, $targetEncoding, $sourceEncoding] = Translation::getEncodings($language, $langFile);
300
301 foreach ($mess as $key => $val)
302 {
303 if ($convertEncoding)
304 {
305 $val = $mess[$key] = Encoding::convertEncoding($val, $sourceEncoding, $targetEncoding);
306 }
307
308 self::$customMessages[$language][$key] = $val;
309 }
310 }
311 }
static getEncodings($language, $langFile)

◆ loadLanguageFile()

static loadLanguageFile (   $file,
  $language = null,
  $normalize = true 
)
static

Loads language messages for specified file

Аргументы
string$file
string$language
bool$normalize
Возвращает
array

См. определение в файле loc.php строка 224

225 {
226 if($language === null)
227 {
228 $language = self::getCurrentLang();
229 }
230
231 if($normalize)
232 {
233 $file = Path::normalize($file);
234 }
235
236 if(!isset(self::$messages[$language]))
237 {
238 self::$messages[$language] = array();
239 }
240
241 //first time call only for lang
242 if(!isset(self::$userMessages[$language]))
243 {
244 self::$userMessages[$language] = self::loadUserMessages($language);
245 }
246
247 //let's find language folder and include lang files
248 $mess = self::includeLangFiles($file, $language, $langFile);
249
250 if (!empty($mess))
251 {
252 [$convertEncoding, $targetEncoding, $sourceEncoding] = Translation::getEncodings($language, $langFile);
253
254 foreach ($mess as $key => $val)
255 {
256 if (isset(self::$customMessages[$language][$key]))
257 {
258 self::$messages[$language][$key] = $mess[$key] = self::$customMessages[$language][$key];
259 }
260 else
261 {
262 if ($convertEncoding)
263 {
264 $val = Encoding::convertEncoding($val, $sourceEncoding, $targetEncoding);
265 $mess[$key] = $val;
266 }
267
268 self::$messages[$language][$key] = $val;
269 }
270 }
271 }
272
273 return $mess;
274 }

◆ loadMessages()

static loadMessages (   $file)
static

Loads language messages for specified file in a lazy way

Аргументы
string$file

См. определение в файле loc.php строка 64

65 {
66 if(($realPath = realpath($file)) !== false)
67 {
68 $file = $realPath;
69 }
70 $file = Path::normalize($file);
71
72 self::$lazyLoadFiles[$file] = $file;
73 }

◆ setCurrentLang()

static setCurrentLang (   $language)
static

См. определение в файле loc.php строка 95

96 {
97 self::$currentLang = $language;
98 }

Объявления и описания членов класса находятся в файле: