1 <?php namespace BookStack\Translation;
4 class Translator extends \Illuminate\Translation\Translator
8 * Mapping of locales to their base locales
11 protected $baseLocaleMap = [
12 'de_informal' => 'de',
16 * Get the translation for a given key.
19 * @param array $replace
20 * @param string $locale
21 * @return string|array|null
23 public function trans($key, array $replace = [], $locale = null)
25 $translation = $this->get($key, $replace, $locale);
27 if (is_array($translation)) {
28 $translation = $this->mergeBackupTranslations($translation, $key, $locale);
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
42 protected function mergeBackupTranslations(array $translationArray, string $key, $locale = null)
44 $fallback = $this->get($key, [], $this->fallback);
45 $baseLocale = $this->getBaseLocale($locale ?? $this->locale);
46 $baseTranslations = $baseLocale ? $this->get($key, [], $baseLocale) : [];
48 return array_replace_recursive($fallback, $baseTranslations, $translationArray);
52 * Get the array of locales to be checked.
54 * @param string|null $locale
57 protected function localeArray($locale)
59 $primaryLocale = $locale ?: $this->locale;
60 return array_filter([$primaryLocale, $this->getBaseLocale($primaryLocale), $this->fallback]);
64 * Get the locale to extend for the given locale.
66 * @param string $locale
69 protected function getBaseLocale($locale)
71 return $this->baseLocaleMap[$locale] ?? null;