Locale.php 18.2 KB
Newer Older
Henning Leutz's avatar
Henning Leutz committed
1 2 3
<?php

/**
Henning Leutz's avatar
Henning Leutz committed
4
 * This file contains \QUI\Locale
Henning Leutz's avatar
Henning Leutz committed
5 6
 */

Henning Leutz's avatar
Henning Leutz committed
7 8
namespace QUI;

Henning Leutz's avatar
Henning Leutz committed
9
use QUI;
10
use QUI\Utils\StringHelper;
Henning Leutz's avatar
Henning Leutz committed
11

Henning Leutz's avatar
Henning Leutz committed
12 13 14 15
/**
 * The locale object
 * translate the ui and all messages
 *
16 17
 * @author  www.pcsg.de (Henning Leutz)
 * @licence For copyright and license information, please view the /README.md
Henning Leutz's avatar
Henning Leutz committed
18 19
 * @package com.pcsg.qui
 *
20
 * @use     gettext - if enabled
21
 * @todo    integrate http://php.net/intl
Henning Leutz's avatar
Henning Leutz committed
22
 */
Henning Leutz's avatar
Henning Leutz committed
23
class Locale
Henning Leutz's avatar
Henning Leutz committed
24
{
25 26
    /**
     * The current lang
27
     *
Henning Leutz's avatar
Henning Leutz committed
28
     * @var array|bool
29
     */
Henning Leutz's avatar
Henning Leutz committed
30
    protected $dateFormats = false;
31

Henning Leutz's avatar
Henning Leutz committed
32 33
    /**
     * The current lang
34
     *
Henning Leutz's avatar
Henning Leutz committed
35
     * @var string
Henning Leutz's avatar
Henning Leutz committed
36
     */
Henning Leutz's avatar
Henning Leutz committed
37
    protected $current = 'en';
Henning Leutz's avatar
Henning Leutz committed
38 39 40

    /**
     * the exist langs
41
     *
Henning Leutz's avatar
Henning Leutz committed
42 43
     * @var array
     */
44
    protected $langs = [];
Henning Leutz's avatar
Henning Leutz committed
45

46 47
    /**
     * gettext object
48
     *
Henning Leutz's avatar
Henning Leutz committed
49
     * @var array
50
     */
51
    protected $gettext = [];
Henning Leutz's avatar
Henning Leutz committed
52 53
    /**
     * no translation flag
54
     *
Henning Leutz's avatar
Henning Leutz committed
55
     * @var boolean
Henning Leutz's avatar
Henning Leutz committed
56
     */
57 58
    public $no_translation = false;

Henning Leutz's avatar
Henning Leutz committed
59 60
    /**
     * ini file objects, if no gettext exist
61
     *
Henning Leutz's avatar
Henning Leutz committed
62 63
     * @var array
     */
64
    protected $inis = [];
Henning Leutz's avatar
Henning Leutz committed
65

66 67
    /**
     * List of internal locale list for setlocale()
68
     *
69 70
     * @var array
     */
71
    protected $localeList = [];
72

73 74 75 76 77 78 79
    /**
     * Saves the current language of this Locale if setTemporaryCurrent is used.
     *
     * @var bool
     */
    protected $tempCurrent = false;

80

81
    /**
Henning Leutz's avatar
Henning Leutz committed
82
     * Locale tostring
83
     *
Henning Leutz's avatar
Henning Leutz committed
84
     * @return string
85 86 87 88 89 90
     */
    public function __toString()
    {
        return 'Locale()';
    }

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    /**
     * Sets the current language of this locale to $lang.
     *
     * WARNING: It it STRONGLY advised to use resetCurrent() immediately after
     * your use case. Changing the global current language longer than that may otherwise have
     * unforeseeable consequences!
     *
     * @param string $lang
     * @return void
     */
    public function setTemporaryCurrent(string $lang)
    {
        if (empty($this->tempCurrent)) {
            $this->tempCurrent = $this->getCurrent();
        }

        $this->setCurrent($lang);
    }

    /**
     * Resets the current language to the initial state. Useful only after setTemporaryCurrent()
     * was used!
     *
     * @return void
     */
    public function resetCurrent()
    {
        if (!empty($this->tempCurrent)) {
            $this->setCurrent($this->tempCurrent);
            $this->tempCurrent = false;
        }
    }

124 125 126
    /**
     * Set the current language
     *
Henning Leutz's avatar
Henning Leutz committed
127
     * @param string $lang - en, en_EN, de, de_DE, de_AT
128 129 130
     */
    public function setCurrent($lang)
    {
131
        $lang = \preg_replace('/[^a-zA-Z_]/', '', $lang);
132

Henning Leutz's avatar
Henning Leutz committed
133
        $this->current = $lang;
134 135 136 137 138
    }

    /**
     * Return the current language
     *
Henning Leutz's avatar
Henning Leutz committed
139
     * @return string
140 141 142
     */
    public function getCurrent()
    {
Henning Leutz's avatar
Henning Leutz committed
143
        return $this->current;
144
    }
Henning Leutz's avatar
Henning Leutz committed
145

146 147 148
    /**
     * @return string
     */
149
    public function getDecimalSeparator()
150 151 152 153 154 155 156
    {
        return $this->get('quiqqer/quiqqer', 'numberFormat.decimal_separator');
    }

    /**
     * @return string
     */
157
    public function getGroupingSeparator()
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    {
        return $this->get('quiqqer/quiqqer', 'numberFormat.grouping_separator');
    }

    /**
     * @return array|string
     */
    public function getDecimalPattern()
    {
        return $this->get('quiqqer/quiqqer', 'numberFormat.decimal_pattern');
    }

    /**
     * @return array|string
     */
    public function getPercentPattern()
    {
        return $this->get('quiqqer/quiqqer', 'numberFormat.percent_pattern');
    }

    /**
     * @return array|string
     */
    public function getCurrencyPattern()
    {
        return $this->get('quiqqer/quiqqer', 'numberFormat.currency_pattern');
    }

    /**
     * @return array|string
     */
    public function getAccountingCurrencyPattern()
    {
        return $this->get('quiqqer/quiqqer', 'numberFormat.accounting_currency_pattern');
    }

Henning Leutz's avatar
Henning Leutz committed
194 195
    /**
     * Refresh the locale
Henning Leutz's avatar
Henning Leutz committed
196
     * Clears the locale
Henning Leutz's avatar
Henning Leutz committed
197
     */
Henning Leutz's avatar
Henning Leutz committed
198
    public function refresh()
Henning Leutz's avatar
Henning Leutz committed
199
    {
200 201
        $this->gettext = [];
        $this->langs   = [];
Henning Leutz's avatar
Henning Leutz committed
202 203
    }

Henning Leutz's avatar
Henning Leutz committed
204
    /**
205 206 207
     * Format a number
     *
     * @param float|integer $number
208
     * @param integer $format
209
     * @return string
Henning Leutz's avatar
Henning Leutz committed
210
     */
211
    public function formatNumber($number, $format = \NumberFormatter::DECIMAL)
Henning Leutz's avatar
Henning Leutz committed
212
    {
213 214 215
        $localeCode = QUI::getLocale()->getLocalesByLang(
            QUI::getLocale()->getCurrent()
        );
Henning Leutz's avatar
Henning Leutz committed
216

217 218
        $Formatter = new \NumberFormatter($localeCode[0], $format);

219 220
        if (\is_string($number)) {
            $number = \floatval($number);
221
        }
Henning Leutz's avatar
Henning Leutz committed
222

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
        $decimalSeparator  = self::get('quiqqer/quiqqer', 'numberFormat.decimal_separator');
        $groupingSeparator = self::get('quiqqer/quiqqer', 'numberFormat.grouping_separator');
        $decimalPattern    = self::get('quiqqer/quiqqer', 'numberFormat.decimal_pattern');

        if (!empty($decimalSeparator)) {
            $Formatter->setSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL, $groupingSeparator);
        }

        if (!empty($groupingSeparator)) {
            $Formatter->setSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL, $groupingSeparator);
        }

        if (!empty($decimalPattern)) {
            $Formatter->setPattern($decimalPattern);
        }
238

239
        //  numberFormat.numbering_system
240 241 242 243
        //  numberFormat.percent_pattern
        //  numberFormat.currency_pattern
        //  numberFormat.accounting_currency_pattern

244
        //  "numbering_system": "latn",
245 246
        //  "decimal_pattern": "#,##0.###",
        //  "percent_pattern": "#,##0%",
Henning Leutz's avatar
Henning Leutz committed
247

248
        return $Formatter->format($number);
Henning Leutz's avatar
Henning Leutz committed
249 250
    }

251 252 253
    /**
     * Format a date timestamp
     *
254
     * @param             $timestamp
255
     * @param bool|string $format - (optional) ;if not given, it uses the quiqqer system format
256
     *
Henning Leutz's avatar
Henning Leutz committed
257
     * @return string
258
     */
259
    public function formatDate($timestamp, $format = false)
260
    {
261 262
        if (!\is_numeric($timestamp)) {
            $timestamp = \strtotime($timestamp);
263 264 265 266
        }

        $current = $this->getCurrent();

267
        if ($format) {
268 269 270 271
            $oldlocale = \setlocale(LC_TIME, "0");
            \setlocale(LC_TIME, $this->getLocalesByLang($current));
            $result = \strftime($format, $timestamp);
            \setlocale(LC_TIME, $oldlocale);
272 273 274 275

            return $result;
        }

Henning Leutz's avatar
Henning Leutz committed
276
        $formats = $this->getDateFormats();
277

278
        if (!empty($formats[$current])) {
279 280
            $oldlocale = \setlocale(LC_TIME, "0");
            \setlocale(LC_TIME, $this->getLocalesByLang($current));
281
            $result = strftime($formats[$current], $timestamp);
282
            \setlocale(LC_TIME, $oldlocale);
283 284 285 286

            return $result;
        }

287
        return \utf8_encode(\strftime('%D', $timestamp));
288 289
    }

290 291 292 293 294 295 296
    /**
     * Return a date formatter for the current language
     *
     * @param int $dateType
     * @param int $timeType
     * @return \IntlDateFormatter
     */
297 298 299 300
    public function getDateFormatter(
        $dateType = \IntlDateFormatter::SHORT,
        $timeType = \IntlDateFormatter::NONE
    ) {
301 302 303 304 305 306
        $localeCode = $this->getLocalesByLang($this->getCurrent());
        $Formatter  = new \IntlDateFormatter($localeCode[0], $dateType, $timeType);

        return $Formatter;
    }

307 308
    /**
     * Return all available dateformats
309
     *
Henning Leutz's avatar
Henning Leutz committed
310
     * @return array
311
     */
Henning Leutz's avatar
Henning Leutz committed
312
    protected function getDateFormats()
313
    {
Henning Leutz's avatar
Henning Leutz committed
314 315
        if ($this->dateFormats) {
            return $this->dateFormats;
316 317
        }

Henning Leutz's avatar
Henning Leutz committed
318
        $this->dateFormats = QUI::conf('date_formats');
319

Henning Leutz's avatar
Henning Leutz committed
320
        if (!$this->dateFormats) {
321
            $this->dateFormats = [];
322 323
        }

Henning Leutz's avatar
Henning Leutz committed
324
        return $this->dateFormats;
325 326 327 328 329
    }

    /**
     * Return the locale list for a language
     *
330
     * @param string $lang - Language code (de, en, fr ...)
331
     *
Henning Leutz's avatar
Henning Leutz committed
332
     * @return array
333
     */
Henning Leutz's avatar
Henning Leutz committed
334
    public function getLocalesByLang($lang)
335
    {
Henning Leutz's avatar
Henning Leutz committed
336 337
        if (isset($this->localeList[$lang])) {
            return $this->localeList[$lang];
338 339 340
        }

        // no shell
341
        if (!QUI\Utils\System::isShellFunctionEnabled('locale')) {
342
            // if we cannot read locale list, so we must guess
343
            $langCode = \strtolower($lang).'_'.\strtoupper($lang);
344

345
            $this->localeList[$lang] = [
346
                $langCode,
347 348 349
                $langCode.'.utf8',
                $langCode.'.UTF-8',
                $langCode.'@euro'
350
            ];
351

Henning Leutz's avatar
Henning Leutz committed
352
            return $this->localeList[$lang];
353 354 355 356
        }


        // via shell
357 358
        $locales = \shell_exec('locale -a');
        $locales = \explode("\n", $locales);
359

360
        $langList = [];
361

362
        foreach ($locales as $locale) {
363
            if (\strpos($locale, $lang) !== 0) {
364 365 366 367 368 369
                continue;
            }

            $langList[] = $locale;
        }

370
        $langCode = \strtolower($lang).'_'.\strtoupper($lang);
371 372

        // not the best solution
373
        if ($lang == 'en') {
374 375 376 377
            $langCode = 'en_GB';
        }

        // sort, main locale to the top
378
        \usort($langList, function ($a, $b) use ($langCode) {
379
            if ($a == $b) {
380 381 382
                return 0;
            }

383
            if (\strpos($a, $langCode) === 0) {
384 385 386
                return -1;
            }

387
            if (\strpos($b, $langCode) === 0) {
388 389 390 391 392 393
                return 1;
            }

            return $a > $b;
        });

Henning Leutz's avatar
Henning Leutz committed
394
        $this->localeList[$lang] = $langList;
395

Henning Leutz's avatar
Henning Leutz committed
396
        return $this->localeList[$lang];
397 398
    }

Henning Leutz's avatar
Henning Leutz committed
399 400
    /**
     * Set translation
401
     *
Henning Leutz's avatar
Henning Leutz committed
402 403 404 405
     * @param string $lang - Language
     * @param string $group - Language group
     * @param string|array $key
     * @param string|boolean $value
Henning Leutz's avatar
Henning Leutz committed
406
     */
407
    public function set($lang, $group, $key, $value = false)
408
    {
Henning Leutz's avatar
Henning Leutz committed
409
        if (!isset($this->langs[$lang])) {
410
            $this->langs[$lang] = [];
Henning Leutz's avatar
Henning Leutz committed
411 412
        }

Henning Leutz's avatar
Henning Leutz committed
413
        if (!isset($this->langs[$lang][$group])) {
414
            $this->langs[$lang][$group] = [];
415
        }
Henning Leutz's avatar
Henning Leutz committed
416

417
        if (!\is_array($key)) {
Henning Leutz's avatar
Henning Leutz committed
418
            $this->langs[$lang][$group][$key] = $value;
419

Henning Leutz's avatar
Henning Leutz committed
420
            return;
421
        }
Henning Leutz's avatar
Henning Leutz committed
422

423
        $this->langs[$lang][$group] = \array_merge(
Henning Leutz's avatar
Henning Leutz committed
424
            $this->langs[$lang][$group],
425 426 427
            $key
        );
    }
Henning Leutz's avatar
Henning Leutz committed
428

429 430 431
    /**
     * Exist the variable in the translation?
     *
Henning Leutz's avatar
Henning Leutz committed
432 433
     * @param string $group - language group
     * @param string|boolean $value - language group variable, optional
434
     *
Henning Leutz's avatar
Henning Leutz committed
435
     * @return boolean
436
     */
437
    public function exists($group, $value = false)
438
    {
Henning Leutz's avatar
Henning Leutz committed
439
        $str = $this->getHelper($group, $value);
Henning Leutz's avatar
Henning Leutz committed
440

441 442
        if ($value === false) {
            if (empty($str)) {
443 444
                return false;
            }
Henning Leutz's avatar
Henning Leutz committed
445

446 447
            return true;
        }
Henning Leutz's avatar
Henning Leutz committed
448

449
        $_str = '['.$group.'] '.$value;
Henning Leutz's avatar
Henning Leutz committed
450

451
        if ($_str === $str) {
452 453
            return false;
        }
Henning Leutz's avatar
Henning Leutz committed
454

455 456
        return true;
    }
Henning Leutz's avatar
Henning Leutz committed
457

Henning Leutz's avatar
Henning Leutz committed
458 459 460 461 462 463 464 465 466 467 468
    /**
     * Exists the language in the locale?
     *
     * @param {string} $language - language eq: de, en
     * @return bool
     */
    public function existsLang($language)
    {
        return isset($this->langs[$language]);
    }

Henning Leutz's avatar
Henning Leutz committed
469 470 471
    /**
     * Get the translation
     *
Henning Leutz's avatar
Henning Leutz committed
472 473 474
     * @param string $group - Gruppe
     * @param string|boolean $value - (optional) Variable, optional
     * @param array|boolean $replace - (optional)
Henning Leutz's avatar
Henning Leutz committed
475
     *
Henning Leutz's avatar
Henning Leutz committed
476
     * @return string|array
Henning Leutz's avatar
Henning Leutz committed
477
     */
478
    public function get($group, $value = false, $replace = false)
479
    {
480
        if ($replace === false || empty($replace)) {
481
            $str = $this->getHelper($group, $value);
482
            $str = \str_replace('{\n}', PHP_EOL, $str);
483 484

            return $str;
485 486
        }

Henning Leutz's avatar
Henning Leutz committed
487
        $str = $this->getHelper($group, $value);
488

489
        foreach ($replace as $key => $value) {
490
            if (\is_array($value) || \is_object($value)) {
491 492 493
                continue;
            }

494
            $str = \str_replace('['.$key.']', $value, $str);
495 496
        };

497
        $str = \str_replace('{\n}', PHP_EOL, $str);
498

499 500 501
        return $str;
    }

502 503 504 505 506 507 508 509 510 511 512 513 514
    /**
     * Get the translation from a specific language
     *
     * @param string $lang
     * @param string $group
     * @param string|boolean $value
     * @param array|boolean $replace
     *
     * @return string|array
     */
    public function getByLang($lang, $group, $value = false, $replace = false)
    {
        if ($replace === false || empty($replace)) {
515
            $str = $this->getHelper($group, $value, $lang);
516
            $str = \str_replace('{\n}', PHP_EOL, $str);
517 518

            return $str;
519 520 521 522 523
        }

        $str = $this->getHelper($group, $value, $lang);

        foreach ($replace as $key => $value) {
524
            if (\is_array($value) || \is_object($value)) {
525 526 527
                continue;
            }

528
            $str = \str_replace('['.$key.']', $value, $str);
529 530
        };

531
        $str = \str_replace('{\n}', PHP_EOL, $str);
532

533 534 535
        return $str;
    }

536 537 538
    /**
     * Translation helper method
     *
Henning Leutz's avatar
Henning Leutz committed
539 540
     * @param string $group
     * @param string|boolean $value - (optional)
541
     * @param string|boolean $current - (optional) wanted language
542
     *
Henning Leutz's avatar
Henning Leutz committed
543
     * @return string|array
544 545 546
     * @see ->get()
     * @ignore
     */
547
    protected function getHelper($group, $value = false, $current = false)
548
    {
549
        if ($this->no_translation) {
550
            return '['.$group.'] '.$value;
551
        }
Henning Leutz's avatar
Henning Leutz committed
552

553 554 555
        if (!$current) {
            $current = $this->current;
        }
Henning Leutz's avatar
Henning Leutz committed
556

557 558 559 560
        if (isset($this->langs[$current][$group][$value])) {
            return $this->langs[$current][$group][$value];
        }

561
        // auf gettext wenn vorhanden
Henning Leutz's avatar
Henning Leutz committed
562
        $GetText = $this->initGetText($group, $current);
Henning Leutz's avatar
Henning Leutz committed
563

564 565
        if ($GetText !== false) {
            $str = $GetText->get($value);
Henning Leutz's avatar
Henning Leutz committed
566

567
            if ($value != $str) {
Henning Leutz's avatar
Henning Leutz committed
568 569
                return $str;
            }
570
        }
Henning Leutz's avatar
Henning Leutz committed
571

572
        if (!isset($this->langs[$current]) || !isset($this->langs[$current][$group])) {
573
            // Kein gettext vorhanden, dann Config einlesen
574
            $this->langs[$current][$group] = [];
575 576 577 578 579 580

            try {
                $this->initConfig($group, $current);
            } catch (QUI\Exception $Exception) {
                QUI\System\Log::writeDebugException($Exception);
            }
581
        }
Henning Leutz's avatar
Henning Leutz committed
582

583
        if (!$value) {
Henning Leutz's avatar
Henning Leutz committed
584
            return $this->langs[$current][$group];
585
        }
Henning Leutz's avatar
Henning Leutz committed
586

Henning Leutz's avatar
Henning Leutz committed
587
        if (isset($this->langs[$current][$group][$value])) {
Henning Leutz's avatar
Henning Leutz committed
588
            return $this->langs[$current][$group][$value];
589
        }
Henning Leutz's avatar
Henning Leutz committed
590

591
        return '['.$group.'] '.$value;
592
    }
Henning Leutz's avatar
Henning Leutz committed
593 594 595 596

    /**
     * the GetText init
     *
Henning Leutz's avatar
Henning Leutz committed
597 598
     * @param string $group - language group
     * @param string|null $lang - optional, language
599
     *
Henning Leutz's avatar
Henning Leutz committed
600
     * @return boolean|\QUI\Utils\Translation\GetText
Henning Leutz's avatar
Henning Leutz committed
601
     */
Henning Leutz's avatar
Henning Leutz committed
602
    public function initGetText($group, $lang = null)
603
    {
Henning Leutz's avatar
Henning Leutz committed
604
        $current = $this->current;
Henning Leutz's avatar
Henning Leutz committed
605

606
        if (\is_string($lang)) {
Henning Leutz's avatar
Henning Leutz committed
607 608 609
            $current = $lang;
        }

Henning Leutz's avatar
Henning Leutz committed
610 611
        if (isset($this->gettext[$current])
            && isset($this->gettext[$current][$group])
612
        ) {
Henning Leutz's avatar
Henning Leutz committed
613
            return $this->gettext[$current][$group];
614
        }
Henning Leutz's avatar
Henning Leutz committed
615

616
        if (!\function_exists('gettext')) {
Henning Leutz's avatar
Henning Leutz committed
617
            $this->gettext[$current][$group] = false;
618

Henning Leutz's avatar
Henning Leutz committed
619 620 621 622
            return false;
        }


Henning Leutz's avatar
Henning Leutz committed
623
        $GetText = new QUI\Utils\Translation\GetText(
Henning Leutz's avatar
Henning Leutz committed
624 625 626 627
            $current,
            $group,
            $this->dir()
        );
Henning Leutz's avatar
Henning Leutz committed
628

Henning Leutz's avatar
Henning Leutz committed
629
        $this->gettext[$current][$group] = $GetText;
Henning Leutz's avatar
Henning Leutz committed
630

Henning Leutz's avatar
Henning Leutz committed
631 632
        if ($GetText->fileExist()) {
            return $GetText;
Henning Leutz's avatar
Henning Leutz committed
633
        }
Henning Leutz's avatar
Henning Leutz committed
634

Henning Leutz's avatar
Henning Leutz committed
635
        $file = $GetText->getFile();
636

637
        System\Log::addWarning(
638
            QUI::getLocale()->get('quiqqer/quiqqer', 'message.translation.file.not.found', [
Henning Leutz's avatar
Henning Leutz committed
639
                'file' => $file
640 641 642 643
            ]),
            [
                'file' => $file
            ]
Henning Leutz's avatar
Henning Leutz committed
644 645
        );

Henning Leutz's avatar
Henning Leutz committed
646
        $this->gettext[$current][$group] = false;
647

Henning Leutz's avatar
Henning Leutz committed
648
        return false;
649
    }
Henning Leutz's avatar
Henning Leutz committed
650 651 652

    /**
     * read a config
653
     *
Henning Leutz's avatar
Henning Leutz committed
654
     * @param string $group - translation group
Henning Leutz's avatar
Henning Leutz committed
655
     * @param string|bool $lang - translation language
656 657
     *
     * @throws QUI\Exception
Henning Leutz's avatar
Henning Leutz committed
658
     */
Henning Leutz's avatar
Henning Leutz committed
659
    public function initConfig($group, $lang = false)
660
    {
Henning Leutz's avatar
Henning Leutz committed
661 662 663 664 665
        if ($lang === false) {
            $lang = $this->current;
        }

        $file = $this->getTranslationFile($lang, $group);
Henning Leutz's avatar
Henning Leutz committed
666

667
        if (!\file_exists($file)) {
668 669
            return;
        }
Henning Leutz's avatar
Henning Leutz committed
670

Henning Leutz's avatar
Henning Leutz committed
671 672
        if (isset($this->inis[$file])) {
            $Config = $this->inis[$file];
673 674
        } else {
            $Config = new QUI\Config($file);
Henning Leutz's avatar
Henning Leutz committed
675 676
        }

677
        $this->set($lang, $group, $Config->toArray());
678
    }
Henning Leutz's avatar
Henning Leutz committed
679 680 681 682

    /**
     * Get the translation file in dependence to the lang and group
     *
Henning Leutz's avatar
Henning Leutz committed
683 684
     * @param string $lang
     * @param string $group
Henning Leutz's avatar
Henning Leutz committed
685
     *
Henning Leutz's avatar
Henning Leutz committed
686
     * @return string
Henning Leutz's avatar
Henning Leutz committed
687 688 689
     */
    public function getTranslationFile($lang, $group)
    {
690
        $lang   = \preg_replace('/[^a-zA-Z]/', '', $lang);
691
        $locale = StringHelper::toLower($lang).'_'.StringHelper::toUpper($lang);
692
        $group  = \str_replace('/', '_', $group);
Henning Leutz's avatar
Henning Leutz committed
693

694
        return $this->dir().'/'.$locale.'/LC_MESSAGES/'.$group.'.ini.php';
Henning Leutz's avatar
Henning Leutz committed
695 696
    }

697
    /**
Henning Leutz's avatar
Henning Leutz committed
698
     * Folder located the translations
699
     *
Henning Leutz's avatar
Henning Leutz committed
700
     * @return string
Henning Leutz's avatar
Henning Leutz committed
701 702 703
     */
    public function dir()
    {
704
        return VAR_DIR.'locale/';
Henning Leutz's avatar
Henning Leutz committed
705
    }
706 707 708 709 710 711 712 713 714 715

    /**
     * Verified the string if the string is a locale string
     * a locale strings looks like: [group] var.var.var
     *
     * @param string $str
     * @return bool
     */
    public function isLocaleString($str)
    {
716 717 718
        if (\strpos($str, ' ') === false
            || \strpos($str, '[') === false
            || \strpos($str, ']') === false
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
        ) {
            return false;
        }

        return true;
    }

    /**
     * Return the parts of a locale string
     * a locale strings looks like: [group] var.var.var
     *
     * @param string $str
     * @return array -  [0=>group, 1=>var]
     */
    public function getPartsOfLocaleString($str)
    {
735
        $str = \explode(' ', $str);
736

737 738 739 740
        if (!isset($str[1])) {
            return $str;
        }

741 742
        $group = \str_replace(['[', ']'], '', $str[0]);
        $var   = \trim($str[1]);
743

744
        return [$group, $var];
745
    }
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763

    /**
     * Parse a locale string and translate it
     * a locale strings looks like: [group] var.var.var
     *
     * @param $title
     * @return string
     */
    public function parseLocaleString($title)
    {
        if (!$this->isLocaleString($title)) {
            return $title;
        }

        $parts = $this->getPartsOfLocaleString($title);

        return $this->get($parts[0], $parts[1]);
    }
764 765 766 767 768 769 770 771 772

    /**
     * Parse a locale array and translate it
     *
     * @param array $locale - with group, translation var and replacement vars (optional)
     * @return string
     */
    public function parseLocaleArray($locale)
    {
Henning Leutz's avatar
Henning Leutz committed
773
        if (!isset($locale[0]) || !isset($locale[1])) {
774 775 776 777 778 779 780 781 782
            return '';
        }

        if (!isset($locale[2])) {
            return $this->get($locale[0], $locale[1]);
        }

        return $this->get($locale[0], $locale[1], $locale[2]);
    }
Henning Leutz's avatar
Henning Leutz committed
783
}