1 <?php namespace BookStack\Translation;
3 class Translator extends \Illuminate\Translation\Translator
7 * Mapping of locales to their base locales
10 protected $baseLocaleMap = [
11 'de_informal' => 'de',
15 * Get the translation for a given key.
18 * @param array $replace
19 * @param string $locale
20 * @return string|array|null
22 public function trans($key, array $replace = [], $locale = null)
24 $translation = $this->get($key, $replace, $locale);
26 if (is_array($translation)) {
27 $translation = $this->mergeBackupTranslations($translation, $key, $locale);
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
41 protected function mergeBackupTranslations(array $translationArray, string $key, $locale = null)
43 $fallback = $this->get($key, [], $this->fallback);
44 $baseLocale = $this->getBaseLocale($locale ?? $this->locale);
45 $baseTranslations = $baseLocale ? $this->get($key, [], $baseLocale) : [];
47 return array_replace_recursive($fallback, $baseTranslations, $translationArray);
51 * Get the array of locales to be checked.
53 * @param string|null $locale
56 protected function localeArray($locale)
58 $primaryLocale = $locale ?: $this->locale;
59 return array_filter([$primaryLocale, $this->getBaseLocale($primaryLocale), $this->fallback]);
63 * Get the locale to extend for the given locale.
65 * @param string $locale
68 protected function getBaseLocale($locale)
70 return $this->baseLocaleMap[$locale] ?? null;