Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
lang.php
1<?php
2
3namespace Bitrix\Rest;
4
7
8class Lang
9{
10 private const DEFAULT_LANG = 'en';
11 private static $languageList;
12 private const KEY_LANG_ALL = 'LANG_ALL';
13
19 public static function listLanguage(): array
20 {
21 if (static::$languageList === null)
22 {
23 static::$languageList = [
24 LANGUAGE_ID,
25 Loc::getDefaultLang(LANGUAGE_ID),
26 ];
27 $licenseLang = null;
28 if (Main\Loader::includeModule('bitrix24'))
29 {
30 $licenseLang = \CBitrix24::getLicensePrefix();
31 }
32 else
33 {
34 $dbSites = \CSite::getList(
35 'sort',
36 'asc',
37 [
38 'DEFAULT' => 'Y',
39 'ACTIVE' => 'Y'
40 ]
41 );
42 if ($site = $dbSites->fetch() && !empty($site['LANGUAGE_ID']))
43 {
44 $licenseLang = $site['LANGUAGE_ID'];
45 }
46 }
47
48 if($licenseLang === null)
49 {
50 $licenseLang = static::DEFAULT_LANG;
51 }
52
53 static::$languageList[] = $licenseLang;
54 }
55
56 return static::$languageList;
57 }
58
59 public static function mergeFromLangAll($data)
60 {
61 $result = [];
62 if (!empty($data[static::KEY_LANG_ALL]))
63 {
64 $useLang = false;
65 $langList = static::listLanguage();
66 foreach ($langList as $lang)
67 {
68 if (is_array($data[static::KEY_LANG_ALL][$lang]))
69 {
70 $useLang = $lang;
71 break;
72 }
73 }
74
75 if ($useLang !== false)
76 {
77 $result = $data[static::KEY_LANG_ALL][$useLang];
78 }
79 else
80 {
81 $item = reset($data[static::KEY_LANG_ALL]);
82 if (is_array($item))
83 {
84 $result = $item;
85 }
86 }
87 }
88
89 return array_merge($data, $result);
90 }
91
100 public static function fillCompatibility(array $param, array $fieldList, array $defaultValues = []) : array
101 {
102 $result = [];
103
104 $langList = static::listLanguage();
105 $langDefault = reset($langList);
106
107 if (empty($param[static::KEY_LANG_ALL]))
108 {
109 foreach ($fieldList as $key)
110 {
111 $value = trim($param[$key] ?? '');
112 if ($value !== '')
113 {
114 $result[static::KEY_LANG_ALL][$langDefault][$key] = $value;
115 $result[$key] = trim($param[$key]);
116 }
117 elseif ($defaultValues[$key])
118 {
119 $result[static::KEY_LANG_ALL][$langDefault][$key] = $defaultValues[$key];
120 $result[$key] = $defaultValues[$key];
121 }
122 }
123 }
124 else
125 {
126 foreach ($param[static::KEY_LANG_ALL] as $langCode => $langItem)
127 {
128 foreach ($fieldList as $field)
129 {
130 $value = trim($langItem[$field] ?? '');
131 if ($value !== '')
132 {
133 $result[static::KEY_LANG_ALL][$langCode][$field] = $value;
134 }
135 elseif ($defaultValues[$field])
136 {
137 $result[static::KEY_LANG_ALL][$langDefault][$field] = $defaultValues[$field];
138 }
139 }
140 }
141 $result = static::mergeFromLangAll($result);
142 }
143
144 if (empty($result[static::KEY_LANG_ALL]) && !empty($defaultValues))
145 {
146 $result[static::KEY_LANG_ALL][$langDefault] = $defaultValues;
147 }
148
149 return $result;
150 }
151}
static getDefaultLang($lang)
Definition loc.php:460
static listLanguage()
Definition lang.php:19
static mergeFromLangAll($data)
Definition lang.php:59
static fillCompatibility(array $param, array $fieldList, array $defaultValues=[])
Definition lang.php:100