|
| 1 | +<?php namespace Hpolthof\Translation;; |
| 2 | + |
| 3 | +use Illuminate\Translation\LoaderInterface; |
| 4 | +use Symfony\Component\Translation\TranslatorInterface; |
| 5 | + |
| 6 | +class Translator extends \Illuminate\Translation\Translator implements TranslatorInterface { |
| 7 | + |
| 8 | + public function __construct(LoaderInterface $database, LoaderInterface $loader, $locale) |
| 9 | + { |
| 10 | + $this->database = $database; |
| 11 | + parent::__construct($loader, $locale); |
| 12 | + } |
| 13 | + |
| 14 | + protected static function isNamespaced($namespace) |
| 15 | + { |
| 16 | + return !(is_null($namespace) || $namespace == '*'); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Get the translation for the given key. |
| 21 | + * |
| 22 | + * @param string $key |
| 23 | + * @param array $replace |
| 24 | + * @param string $locale |
| 25 | + * @return string |
| 26 | + */ |
| 27 | + public function get($key, array $replace = array(), $locale = null) |
| 28 | + { |
| 29 | + list($namespace, $group, $item) = $this->parseKey($key); |
| 30 | + |
| 31 | + // Here we will get the locale that should be used for the language line. If one |
| 32 | + // was not passed, we will use the default locales which was given to us when |
| 33 | + // the translator was instantiated. Then, we can load the lines and return. |
| 34 | + foreach ($this->parseLocale($locale) as $locale) |
| 35 | + { |
| 36 | + if(!self::isNamespaced($namespace)) { |
| 37 | + // Database stuff |
| 38 | + $this->database->addTranslation($locale, $group, $key); |
| 39 | + } |
| 40 | + |
| 41 | + $this->load($namespace, $group, $locale); |
| 42 | + |
| 43 | + $line = $this->getLine( |
| 44 | + $namespace, $group, $locale, $item, $replace |
| 45 | + ); |
| 46 | + |
| 47 | + if ( ! is_null($line)) break; |
| 48 | + } |
| 49 | + |
| 50 | + // If the line doesn't exist, we will return back the key which was requested as |
| 51 | + // that will be quick to spot in the UI if language keys are wrong or missing |
| 52 | + // from the application's language files. Otherwise we can return the line. |
| 53 | + if ( ! isset($line)) return $key; |
| 54 | + |
| 55 | + return $line; |
| 56 | + } |
| 57 | + |
| 58 | + public function load($namespace, $group, $locale) |
| 59 | + { |
| 60 | + if ($this->isLoaded($namespace, $group, $locale)) return; |
| 61 | + |
| 62 | + // If a Namespace is give the Filesystem will be used |
| 63 | + // otherwise we'll use our database. |
| 64 | + // This will allow legacy support. |
| 65 | + if(!self::isNamespaced($namespace)) { |
| 66 | + // If debug is off then cache the result forever to ensure high performance. |
| 67 | + if(!\Config::get('app.debug')) { |
| 68 | + $that = $this; |
| 69 | + $lines = \Cache::rememberForever('__translations.'.$locale.'.'.$group, function() use ($that, $locale, $group, $namespace) { |
| 70 | + return $this->database->load($locale, $group, $namespace); |
| 71 | + }); |
| 72 | + } else { |
| 73 | + $lines = $this->database->load($locale, $group, $namespace); |
| 74 | + } |
| 75 | + } else { |
| 76 | + $lines = $this->loader->load($locale, $group, $namespace); |
| 77 | + } |
| 78 | + $this->loaded[$namespace][$group][$locale] = $lines; |
| 79 | + } |
| 80 | +} |
0 commit comments