From 2c3c26f0afb5b09e18467cc6c88c91bcc94bdd47 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 14 Apr 2018 23:01:49 +0100 Subject: [PATCH] Override __ to attempt to pull a translation from both WP & Laravel --- src/helpers.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/helpers.php b/src/helpers.php index 1fc35e8..2999d7a 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -4,6 +4,34 @@ use Koselig\Models\Meta; use Koselig\Models\Post; use Koselig\Support\RecursiveMenuIterator; +if (!function_exists('__')) { + /** + * Tries to get a translation from both Laravel and Wordpress. + * + * @param $key key of the translation + * @param array|string $replace replacements for laravel or domain for wordpress + * @param string $locale locale for laravel, not used for wordpress + * @return string + */ + function __($key, $replace = [], $locale = null) + { + if (is_array($replace)) { + try { + return app('translator')->getFromJson($key, $replace, $locale); + } catch (\Exception $e) { + // failed to get translation from Laravel + if ((!empty($replace) && !is_string($replace)) || !empty($locale)) { + // this doesn't look like something we can pass to WordPress, lets + // rethrow the exception + throw $e; + } + } + } + + return translate($key, empty($replace) ? 'default' : $replace); + } +} + if (!function_exists('query')) { /** * Get the main query or convert a {@link WP_Query} to a {@link \Koselig\Proxy\Query} proxy instance. -- libgit2 1.7.2