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