Laravel translation custom fallback

I am building a multilingual site. First, I save the content to the database, and in the browser, I show a UI showing that a normal admin user can edit that data. Whenever the admin adds any line to the database, he / she can restore all these language files in app / lang / under the corresponding file from the admin panel, just by clicking one button.

My table structure

id | location | key     |hindi   | english |
---------------------------------------
 1 | global   | welcome |namaste | Welcome | 

      

and use in the view file like echo trans('global.welcome');

Now I want to automatically collect data from a view file. Suppose I added a echo trans('global.hello');

file in my view and it is not present in app / lang / en / global.php so the fallback language will call. I want to track this situation so that I can add one row to the database table with hello as the key. I want to add to the database at design time, not production.

  1. Is this a good thing to do, or is there a better option?
  2. How do I run my own function during a reservation?
+3


source to share


1 answer


How about having your own translation function (for development only or even for production). There you can first check if the translation exists and after that call the default translation method. Now the approach if you only want to use it for development or production (it might be useful for some additional functionality)

Development only: Edit the function trans

directly in the file helpers.php

or override it
(you need to comment out the function in helpers.php

out)

Development and production: Create your own function with your name



However, the code remains the same

function trans($id, $parameters = array(), $domain = 'messages', $locale = null){
    $translator = app('translator');
    if(!$translator->has($id, Config::get('app.locale')){
        // insert db row
    }
    return $translator->trans($id, $parameters, $domain, $locale);
}

      

0


source







All Articles