fallbackLocale = $fallbackLocale; $this->locale = $this->fallbackLocale; $this->translationsPath = $translationsPath; if(!file_exists($translationsPath) || !is_dir($translationsPath)) { throw new \RuntimeException("Could not initialize translator: invalid translations path."); } } public function provides(): string { return 'translator'; } /** * Get the default locale string. * @return string */ public function getFallbackLocale(): string { return $this->fallbackLocale; } /** * Get the current locale string. * @return string */ public function getLocale(): string { return $this->locale; } /** * Set the current locale string. If the form is invalid, an exception will be thrown. * @param string $locale * @return $this */ public function setLocale(string $locale): static { if(!static::checkLocale($locale)) { throw new \RuntimeException("Could not set locale: invalid locale format."); } $this->locale = $locale; $this->loadTranslations(); return $this; } /** * Get a translation from the translation array. * @param string $key * @param $default * @return mixed|null|string */ public function getTranslation(string $key, $default = null) { if(empty($this->translations)) { return $default; } $keys = explode ('.', $key); $value = $this->translations; foreach($keys as $index) { if(!key_exists($index, $value)) return $default; $value = $value[$index]; } return $value; } /** * Check if a locale string has the correct format. * @param string $locale * @return bool */ public static function checkLocale(string $locale): bool { return (!empty($locale) && preg_match_all(static::LOCALE_REGEX, $locale)); } /** * Load translations for the currently selected locale. * @return void */ private function loadTranslations(): void { $dir = $this->translationsPath; $locale = $this->locale; $localeDir = $dir . DIRECTORY_SEPARATOR . $locale; if(!file_exists($localeDir)) { //TODO: is the error really needed? throw new \RuntimeException("Unable to load translations; translation directory not found."); } $files = glob($localeDir . DIRECTORY_SEPARATOR . '*.php'); if($files === false || empty($files)) { return; } $this->translations = []; foreach($files as $file) { $translations = require $file; $key = basename($file, '.php'); $this->translations[$key] = $translations; } } }