diff --git a/addons/default/visiosoft/advs-module/resources/config/settings/sections.php b/addons/default/visiosoft/advs-module/resources/config/settings/sections.php index 75de1d0be..b4eebd380 100644 --- a/addons/default/visiosoft/advs-module/resources/config/settings/sections.php +++ b/addons/default/visiosoft/advs-module/resources/config/settings/sections.php @@ -65,7 +65,7 @@ return [ 'user' => [ 'title' => 'visiosoft.module.advs::section.user', 'fields' => [ - 'register_email_field', + 'register_email_field' ], ], 'filter' => [ @@ -74,6 +74,12 @@ return [ 'hide_price_filter', 'hide_date_filter', 'hide_photo_filter', 'hide_map_filter', 'user_filter_limit' ], ], + 'translations' => [ + 'title' => 'visiosoft.module.advs::section.translations', + 'fields' => [ + 'override_text', + ], + ], ], ], ]; diff --git a/addons/default/visiosoft/advs-module/resources/config/settings/settings.php b/addons/default/visiosoft/advs-module/resources/config/settings/settings.php index 7ef868ffe..fa6f0f2dd 100644 --- a/addons/default/visiosoft/advs-module/resources/config/settings/settings.php +++ b/addons/default/visiosoft/advs-module/resources/config/settings/settings.php @@ -325,4 +325,9 @@ return [ 'mode' => 'checkbox' ], ], + 'override_text' => [ + 'type' => 'anomaly.field_type.tags', + 'bind' => 'override_text', + 'env' => 'OVERRIDE_TEXT', + ], ]; diff --git a/addons/default/visiosoft/advs-module/resources/lang/en/section.php b/addons/default/visiosoft/advs-module/resources/lang/en/section.php index 1992005b1..6e1310527 100644 --- a/addons/default/visiosoft/advs-module/resources/lang/en/section.php +++ b/addons/default/visiosoft/advs-module/resources/lang/en/section.php @@ -55,4 +55,5 @@ return [ 'option_configuration' => [ 'title' => 'Configuration', ], + 'translations' => 'Translations', ]; diff --git a/addons/default/visiosoft/advs-module/resources/lang/en/setting.php b/addons/default/visiosoft/advs-module/resources/lang/en/setting.php index 56e9cf779..3a5ae72be 100644 --- a/addons/default/visiosoft/advs-module/resources/lang/en/setting.php +++ b/addons/default/visiosoft/advs-module/resources/lang/en/setting.php @@ -197,4 +197,8 @@ return [ 'show_breadcrumb_when_creating_ad' => [ 'name' => 'Show Breadcrumb When Creating an Ad', ], + 'override_text' => [ + 'name' => 'Override Text', + 'instructions' => 'Old Value:New Value' + ], ]; diff --git a/app/Lang/Loader.php b/app/Lang/Loader.php new file mode 100644 index 000000000..2272b315a --- /dev/null +++ b/app/Lang/Loader.php @@ -0,0 +1,266 @@ + + * @author Ryan Thompson + */ +class Loader extends FileLoader +{ + + /** + * The runtime cache. + * + * @var array + */ + protected static $disabled = []; + + /** + * The streams path. + * + * @var string + */ + protected $streams; + + /** + * The addon collection instance. + * + * @var AddonCollection + */ + protected $addons; + + /** + * The application instance. + * + * @var Application + */ + protected $application; + + /** + * Create a new Loader instance. + * + * @param Filesystem $files + * @param string $path + */ + public function __construct(Filesystem $files, $path) + { + $this->streams = base_path('vendor/visiosoft/streams-platform/resources/lang'); + + $this->application = app(Application::class); + $this->addons = app(AddonCollection::class); + + parent::__construct($files, $path); + } + + /** + * Load a locale from a given path. + * + * Keep streams overrides in place + * that are NOT namespaced cause + * we're overriding Laravel's + * base language files too. + * + * @param string $path + * @param string $locale + * @param string $group + * @return array + */ + protected function loadPath($path, $locale, $group) + { + $lines = parent::loadPath($path, $locale, $group); + + if ($path == $this->streams && $lines) { + $lines = $this->loadAddonOverrides($lines, $locale, $group); + $lines = $this->loadSystemOverrides($lines, $locale, $group); + $lines = $this->loadApplicationOverrides($lines, $locale, $group); + } + + return $lines; + } + + /** + * Load namespaced overrides from + * system AND application paths. + * + * @param array $lines + * @param string $locale + * @param string $group + * @param string $namespace + * @return array + */ + protected function loadNamespaceOverrides(array $lines, $locale, $group, $namespace) + { + /** + * @deprecated since 1.6; Use manual loading or publishing. + */ + if (env('AUTOMATIC_ADDON_OVERRIDES', true)) { + $lines = $this->loadAddonOverrides($lines, $locale, $group, $namespace); + } + + $lines = $this->loadSystemOverrides($lines, $locale, $group, $namespace); + $lines = $this->loadApplicationOverrides($lines, $locale, $group, $namespace); + + return parent::loadNamespaceOverrides($lines, $locale, $group, $namespace); + } + + /** + * Load system overrides. + * + * @param array $lines + * @param $locale + * @param $group + * @param $namespace + * @return array + */ + protected function loadSystemOverrides(array $lines, $locale, $group, $namespace = null) + { + if (!$namespace || $namespace == 'streams') { + + $file = base_path("resources/streams/lang/{$locale}/{$group}.php"); + + if (is_dir(base_path("resources/streams/lang")) && $this->files->exists($file)) { + $lines = array_replace_recursive($lines, $this->files->getRequire($file)); + } + } + + if (str_is('*.*.*', $namespace)) { + + list($vendor, $type, $slug) = explode('.', $namespace); + + $file = base_path("resources/addons/{$vendor}/{$slug}-{$type}/lang/{$locale}/{$group}.php"); + + if (is_dir(base_path("resources/addons/{$vendor}/{$slug}-{$type}/lang")) && $this->files->exists($file)) { + $lines = array_replace_recursive($lines, $this->files->getRequire($file)); + } + } + + return $lines; + } + + /** + * Load system overrides. + * + * @param array $lines + * @param $locale + * @param $group + * @param $namespace + * @return array + */ + protected function loadApplicationOverrides(array $lines, $locale, $group, $namespace = null) + { + if (!$namespace || $namespace == 'streams') { + + $file = $this->application->getResourcesPath("streams/lang/{$locale}/{$group}.php"); + + if (is_dir($this->application->getResourcesPath("streams/lang")) && $this->files->exists($file)) { + $lines = array_replace_recursive($lines, $this->files->getRequire($file)); + } + } + + if (str_is('*.*.*', $namespace)) { + + list($vendor, $type, $slug) = explode('.', $namespace); + + $file = $this->application->getResourcesPath( + "addons/{$vendor}/{$slug}-{$type}/lang/{$locale}/{$group}.php" + ); + + if ( + is_dir($this->application->getResourcesPath("addons/{$vendor}/{$slug}-{$type}/lang")) + && $this->files->exists($file) + ) { + $lines = array_replace_recursive($lines, $this->files->getRequire($file)); + } + } + + foreach (config()->get('override_text') as $override) { + $override = explode(':', $override); + + $lines = $this->findArrayValue($override[0],$override[1],$lines); + } + + return $lines; + } + + function replaceNewValue($find_value, $new_value, $arr) + { + if (is_array($arr)) { + foreach ($arr as $key => $item) { + $arr[$key] = $this->replaceNewValue($find_value, $new_value, $item); + } + return $arr; + } + if (strtolower($arr) == strtolower($find_value)) { + return $new_value; + } + return $arr; + } + + function findArrayValue($find_value, $new_value, $arr) + { + foreach ($arr as $key => $item) { + $arr[$key] = $this->replaceNewValue($find_value, $new_value, $item); + } + return $arr; + } + + + + /** + * @param array $lines + * @param $locale + * @param $group + * @param null $namespace + * @return array + */ + protected function loadAddonOverrides(array $lines, $locale, $group, $namespace = null) + { + /** @var Addon $addon */ + foreach ($this->addons->enabled() as $addon) { + + $disabled = array_get(self::$disabled, $key = $addon->getNamespace('streams'), false); + + if (!$disabled && !$this->files->isDirectory($addon->getPath('resources/streams'))) { + self::$disabled[$key] = $disabled = true; + } + + if (!$disabled && (!$namespace || $namespace == 'streams')) { + + $file = $addon->getPath("resources/streams/lang/{$locale}/{$group}.php"); + + if ($this->files->exists($file)) { + $lines = array_replace_recursive($lines, $this->files->getRequire($file)); + } + } + + $disabled = array_get(self::$disabled, $key = $addon->getNamespace('addons'), false); + + if (!$disabled && !$this->files->isDirectory($addon->getPath('resources/addons'))) { + self::$disabled[$key] = $disabled = true; + } + + if (!$disabled && str_is('*.*.*', $namespace)) { + + list($vendor, $type, $slug) = explode('.', $namespace); + + $file = $addon->getPath( + "resources/addons/{$vendor}/{$slug}-{$type}/lang/{$locale}/{$group}.php" + ); + + if ($this->files->exists($file)) { + $lines = array_replace_recursive($lines, $this->files->getRequire($file)); + } + } + } + + return $lines; + } +} diff --git a/app/Listeners/Translations.php b/app/Listeners/Translations.php new file mode 100755 index 000000000..a891f3401 --- /dev/null +++ b/app/Listeners/Translations.php @@ -0,0 +1,45 @@ +singleton( + 'translation.loader', + function ($application) { + return new Loader($application['files'], $application['path.lang']); + } + ); + + app()->singleton( + 'translator', + function ($application) { + $loader = $application->make('translation.loader'); + + // When registering the translator component, we'll need to set the default + // locale as well as the fallback locale. So, we'll grab the application + // configuration so we can easily get both of these values from there. + $locale = $application['config']['app.locale']; + + $trans = new Translator($loader, $locale); + + $trans->setFallback($application['config']['app.fallback_locale']); + + return $trans; + } + ); + + if (defined('LOCALE')) { + app()->setLocale(LOCALE); + config()->set('app.locale', LOCALE); + } + // Set our locale namespace. + app()->make('translator')->addNamespace('streams', realpath(__DIR__ . '/../../vendor/visiosoft/streams-platform/resources/lang')); + + } +} diff --git a/config/streams.php b/config/streams.php index a05e9b5d3..bc4d41f38 100644 --- a/config/streams.php +++ b/config/streams.php @@ -13,7 +13,11 @@ return [ | */ - 'listeners' => [], + 'listeners' => [ + \Anomaly\Streams\Platform\Event\Booted::class => [ + \App\Listeners\Translations::class + ], + ], /* |--------------------------------------------------------------------------