Resolves #45809: Refactor code and introduce namespacing
[TYPO3CMS/Extensions/static_info_tables.git] / Classes / Utility / LocalizationUtility.php
1 <?php
2 namespace SJBR\StaticInfoTables\Utility;
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 2009 Sebastian Kurf├╝rst <sebastian@typo3.org>
7 * (c) 2013 Stanislas Rolland <typo3@sjbr.ca>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Localization helper which should be used to fetch localized labels for static info entities.
28 *
29 */
30 class LocalizationUtility {
31
32 /**
33 * Key of the language to use
34 *
35 * @var string
36 */
37 protected static $languageKey = 'default';
38
39 /**
40 * Pointer to alternative fall-back language to use
41 *
42 * @var array
43 */
44 protected static $alternativeLanguageKeys = array();
45
46 /**
47 * Returns the localized label for a static info entity
48 *
49 * @param array $identifiers An array with key 1- 'uid' containing a uid and/or 2- 'iso' containing one or two iso codes (i.e. country zone code and country code, or language code and country code)
50 * @param string $tableName The name of the table
51 * @param boolean local name only - if set local labels are returned
52 * @return string The value from the label field of the table
53 */
54 public static function translate ($identifiers, $tableName, $local = FALSE) {
55
56 $value = '';
57 self::setLanguageKeys();
58 \SJBR\StaticInfoTables\Utility\TcaUtility::loadTca($tableName);
59
60 $isoLanguage = self::getIsoLanguageKey(self::$languageKey);
61 $value = self::getLabelFieldValue($identifiers, $tableName, $isoLanguage, $local);
62 if ($value) {
63 $value = self::convertCharset($value, 'utf-8');
64 } else if (count(self::$alternativeLanguageKeys)) {
65 // To do: modify configuration to deal with alternative language keys
66 $languages = array_reverse(self::$alternativeLanguageKeys);
67 foreach ($languages as $language) {
68 $isoLanguage = self::getIsoLanguageKey($language);
69 $value = self::getLabelFieldValue($identifiers, $tableName, $isoLanguage);
70 if ($value) {
71 $value = self::convertCharset($value, 'utf-8');
72 break;
73 }
74 }
75 }
76 return $value;
77 }
78
79 /**
80 * Get the localized value for the label field
81 *
82 * @param array $identifiers An array with key 1- 'uid' containing a uid and/or 2- 'iso' containing one or two iso codes (i.e. country zone code and country code, or language code and country code)
83 * @param string $tableName The name of the table
84 * @param string language ISO code
85 * @param boolean local name only - if set local labels are returned
86 * @return string the value for the label field
87 */
88 protected static function getLabelFieldValue ($identifiers, $tableName, $language, $local = FALSE) {
89 $value = '';
90 $labelFields = self::getLabelFields($tableName, $language, $local);
91 if (count($labelFields)) {
92 // Build the list of fields
93 $prefixedLabelFields = array();
94 foreach ($labelFields as $labelField) {
95 $prefixedLabelFields[] = $tableName . '.' . $labelField;
96 }
97 $fields = $tableName . '.uid,' . implode(',', $prefixedLabelFields);
98 //Build the where clause
99 $whereClause = '';
100 if ($identifiers['uid']) {
101 $whereClause .= $tableName . '.uid = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($identifiers['uid'], $tableName);
102 } else if (!empty($identifiers['iso'])) {
103 $isoCode = is_array($identifiers['iso']) ? $identifiers['iso'] : array($identifiers['iso']);
104 foreach ($isoCode as $index => $code) {
105 if ($code) {
106 $field = self::getIsoCodeField($tableName, $code, $index);
107 if ($field) {
108 $whereClause .= ($whereClause ? ' AND ' : '') . $tableName . '.' . $field . ' = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($code, $tableName);
109 }
110 }
111 }
112 }
113 // Get the entity
114 if ($whereClause) {
115 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
116 $fields,
117 $tableName,
118 $whereClause . \SJBR\StaticInfoTables\Utility\TcaUtility::getEnableFields($tableName)
119 );
120 if (is_array($rows) && count($rows)) {
121 foreach ($labelFields as $labelField) {
122 if ($rows[0][$labelField]) {
123 $value = $rows[0][$labelField];
124 break;
125 }
126 }
127 }
128 }
129 }
130 return $value;
131 }
132
133 /**
134 * Returns the label fields for a given language
135 *
136 * @param string table name
137 * @param string ISO language code to be used
138 * @param boolean If set, we are looking for the "local" title field
139 * @return array field names
140 */
141 public static function getLabelFields ($tableName, $lang, $local = FALSE) {
142 $labelFields = array();
143 if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['static_info_tables']['tables'][$tableName]['label_fields'])) {
144 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['static_info_tables']['tables'][$tableName]['label_fields'] as $field) {
145 if ($local) {
146 $labelField = str_replace ('##', 'local', $field);
147 } else {
148 $labelField = str_replace ('##', strtolower($lang), $field);
149 }
150 if (is_array($GLOBALS['TCA'][$tableName]['columns'][$labelField])) {
151 $labelFields[] = $labelField;
152 }
153 }
154 }
155 return $labelFields;
156 }
157
158 /**
159 * Returns a iso code field for the passed table name, iso code and index
160 *
161 * @param string table name
162 * @param string iso code
163 * @param integer index in the table's isocode_field configuration array
164 * @return string field name
165 */
166 protected static function getIsoCodeField ($table, $isoCode, $index = 0) {
167 $isoCodeField = '';
168 $isoCodeFieldTemplate = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['static_info_tables']['tables'][$table]['isocode_field'][$index];
169 if ($isoCode && $table && $isoCodeFieldTemplate) {
170 $field = str_replace ('##', self::isoCodeType($isoCode), $isoCodeFieldTemplate);
171 if (is_array($GLOBALS['TCA'][$table]['columns'][$field])) {
172 $isoCodeField = $field;
173 }
174 }
175 return $isoCodeField;
176 }
177
178 /**
179 * Returns the type of an iso code: nr, 2, 3
180 *
181 * @param string iso code
182 * @return string iso code type
183 */
184 protected static function isoCodeType ($isoCode) {
185 $type = '';
186 $isoCodeAsInteger = \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($isoCode);
187 if ($isoCodeAsInteger) {
188 $type = 'nr';
189 } else if (strlen($isoCode) == 2) {
190 $type = '2';
191 } else if (strlen($isoCode) == 3) {
192 $type = '3';
193 }
194 return $type;
195 }
196
197 /**
198 * Get the ISO language key corresponding to a TYPO3 language key
199 *
200 * @param string $key The TYPO3 language key
201 * @return string the ISO language key
202 */
203 public static function getIsoLanguageKey($key) {
204 if ($key === 'default') {
205 $lang = 'EN';
206 } else {
207 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
208 'lg_iso_2,lg_country_iso_2',
209 'static_languages',
210 'lg_typo3 = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($key, 'static_languages')
211 );
212 if (is_array($rows) && count($rows)) {
213 $lang = $rows[0]['lg_iso_2'] . ($rows[0]['lg_country_iso_2'] ? '_' . $rows[0]['lg_country_iso_2'] : '');
214 }
215
216 $lang = $lang ? $lang : strtoupper($key);
217 }
218 return $lang;
219 }
220
221 /**
222 * Get the current TYPO3 language
223 *
224 * @return string the TYP3 language key
225 */
226 public static function getCurrentLanguage() {
227 if (self::$languageKey === 'default') {
228 self::setLanguageKeys();
229 }
230 return self::$languageKey;
231 }
232
233 /**
234 * Sets the currently active language/language_alt keys.
235 * Default values are "default" for language key and "" for language_alt key.
236 *
237 * @return void
238 * @author Christopher Hlubek <hlubek@networkteam.com>
239 * @author Bastian Waidelich <bastian@typo3.org>
240 */
241 protected static function setLanguageKeys() {
242 self::$languageKey = 'default';
243 self::$alternativeLanguageKeys = array();
244 if (TYPO3_MODE === 'FE') {
245 if (isset($GLOBALS['TSFE']->config['config']['language'])) {
246 self::$languageKey = $GLOBALS['TSFE']->config['config']['language'];
247 if (isset($GLOBALS['TSFE']->config['config']['language_alt'])) {
248 self::$alternativeLanguageKeys[] = $GLOBALS['TSFE']->config['config']['language_alt'];
249 } else {
250 /** @var $locales \TYPO3\CMS\Core\Localization\Locales */
251 $locales = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\Locales');
252 if (in_array(self::$languageKey, $locales->getLocales())) {
253 foreach ($locales->getLocaleDependencies(self::$languageKey) as $language) {
254 self::$alternativeLanguageKeys[] = $language;
255 }
256 }
257 }
258 }
259 } else {
260 self::$languageKey = $GLOBALS['LANG']->lang;
261 if (strlen($GLOBALS['BE_USER']->uc['lang']) > 0) {
262 self::$languageKey = $GLOBALS['BE_USER']->uc['lang'];
263 }
264 }
265 }
266
267 /**
268 * Converts a string from the specified character set to the current.
269 * The current charset is defined by the TYPO3 mode.
270 *
271 * @param string $value string to be converted
272 * @return string converted string
273 */
274 protected static function convertCharset($value) {
275 if (TYPO3_MODE === 'FE') {
276 return $GLOBALS['TSFE']->csConv($value, 'utf-8');
277 } else {
278 return $value;
279 }
280 }
281 }
282 ?>