]> BookStack Code Mirror - bookstack/blob - app/Translation/Translator.php
Added locale and text direction to html templates
[bookstack] / app / Translation / Translator.php
1 <?php namespace BookStack\Translation;
2
3 class Translator extends \Illuminate\Translation\Translator
4 {
5
6     /**
7      * Mapping of locales to their base locales
8      * @var array
9      */
10     protected $baseLocaleMap = [
11         'de_informal' => 'de',
12     ];
13
14     /**
15      * Get the translation for a given key.
16      *
17      * @param  string  $key
18      * @param  array   $replace
19      * @param  string  $locale
20      * @return string|array|null
21      */
22     public function trans($key, array $replace = [], $locale = null)
23     {
24         $translation = $this->get($key, $replace, $locale);
25
26         if (is_array($translation)) {
27             $translation = $this->mergeBackupTranslations($translation, $key, $locale);
28         }
29
30         return $translation;
31     }
32
33     /**
34      * Merge the fallback translations, and base translations if existing,
35      * into the provided core key => value array of translations content.
36      * @param array $translationArray
37      * @param string $key
38      * @param null $locale
39      * @return array
40      */
41     protected function mergeBackupTranslations(array $translationArray, string $key, $locale = null)
42     {
43         $fallback = $this->get($key, [], $this->fallback);
44         $baseLocale = $this->getBaseLocale($locale ?? $this->locale);
45         $baseTranslations = $baseLocale ? $this->get($key, [], $baseLocale) : [];
46
47         return array_replace_recursive($fallback, $baseTranslations, $translationArray);
48     }
49
50     /**
51      * Get the array of locales to be checked.
52      *
53      * @param  string|null  $locale
54      * @return array
55      */
56     protected function localeArray($locale)
57     {
58         $primaryLocale = $locale ?: $this->locale;
59         return array_filter([$primaryLocale, $this->getBaseLocale($primaryLocale), $this->fallback]);
60     }
61
62     /**
63      * Get the locale to extend for the given locale.
64      *
65      * @param string $locale
66      * @return string|null
67      */
68     protected function getBaseLocale($locale)
69     {
70         return $this->baseLocaleMap[$locale] ?? null;
71     }
72 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.