<?php


function t($key, $replace = [], $locale = null)
{
    $locale = $locale ?? app()->getLocale();

    $translations = cache()->rememberForever(
        "translations:$locale",
        function () use ($locale) {
            return \App\Models\Translation::where('locale', $locale)
                ->pluck('value', 'key')
                ->toArray();
        }
    );

    $text = $translations[$key] ?? $key;

    foreach ($replace as $k => $v) {
        $text = str_replace(":$k", $v, $text);
    }

    return $text;
}

//if (!function_exists('t')) {
//    function t($key, $locale = null)
//    {
//        $locale = $locale ?? app()->getLocale();
//
//        $translations = cache()->rememberForever(
//            "translations:$locale",
//            function () use ($locale) {
//                return \App\Models\Translation::where('locale', $locale)
//                    ->pluck('value', 'key')
//                    ->toArray();
//            }
//        );
//
//        return $translations[$key] ?? $key;
//    }
//}
