mardi 2 juin 2020

How can update a Laravel language file with a function or a query?

Hello, I created a Helper function for the laravel localization which store the translates in the Database. and also the function get the translate from the Database. this is the function.

public static function translate($word,$locale) {
    $find_word = DB::table('translate')->where('word', $word);
    if($find_word->count() > 0){
        if(count(DB::table('languages')->where('field_name',$locale)->get()) == 0){
            $locale = 'en';
        }
        if($find_word->first()->$locale !== null  && $find_word->first()->$locale !== ''){
            $return = $find_word->first()->$locale;
        }
        else{
            $return = $find_word->first()->en;
        }
    }
    else{
        $data['word'] = $word;
        $data['en'] = str_replace('_', ' ', $word);
        DB::table('translate')->insert($data);
        $return = str_replace('_', ' ', $word);
        $locale = 'en';
    }
    return $return;
}

The function works correctly but for every word I need to run a Database query. So its better to create such function to save the words in a file not to the database. when I call the function and request a word and the word does not exists add the word to the file. I prefer this function from laravel default localization. because in the Laravel localization we must create a file manually then we must add words and its translate to the file manually.

So, How can I create such function to save the new requested word in a file or get it from the file?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire