4 months ago

Is it possible to get the key of a translated string?

Posted 4 months ago by Sociopath

Hi, I have on resources/lang/fr_FR.json, resources/lang/en_US.json etc there I have all keys on spanish and his value on the desired language.

I'm trying to translate the URL incoming, for example if I click on "contacto" if I'm on spanish language and I siwtch to english it works fine because I have the key contacto registered, but if I'm in english I have "contact" and I don't have this key to translate in.

Thath's the function that changes the url locale

    public function setLocale($locale, $desiredLocale)
        $previousUrl = explode("/", url()->previous());
        $newUrl = array();

        foreach ($previousUrl as $segment) {
            $translatedSegment =  __($segment, [], $desiredLocale);
            array_push($newUrl, $translatedSegment);
        $uri = implode("/", $newUrl);
        $uri = str_ireplace($locale, $desiredLocale, $uri);

        return redirect($uri, 301);

Please sign in or create an account to participate in this conversation.