🏡 index : ~doyle/koselig.git

author Jordan Doyle <jordan@doyle.wf> 2018-04-14 23:01:49.0 +01:00:00
committer GitHub <noreply@github.com> 2018-04-14 23:01:49.0 +01:00:00
commit
2c3c26f0afb5b09e18467cc6c88c91bcc94bdd47 [patch]
tree
3e007c46a6ad5fef844d52fb72826ebc76d11881
parent
0f43c0fe51489dd97bb3f38e1616337af827fa53
download
2c3c26f0afb5b09e18467cc6c88c91bcc94bdd47.tar.gz

Override __ to attempt to pull a translation from both WP & Laravel



Diff

 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
+++ a/src/helpers.php
@@ -1,9 +1,37 @@
<?php

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.