Waavi / translation

Better translation management for Laravel
MIT License
359 stars 167 forks source link

Please support string translations #143

Open divdax opened 5 years ago

divdax commented 5 years ago

Would be nice to manage string translations in database with this package instead of using json files. https://laravel.com/docs/5.7/localization#using-translation-strings-as-keys

Translations with group and item column feels very weird.

GainsNetwork commented 5 years ago

Hey I don't know if it can help you but I managed to translate directly the strings by creating the following helper :

<?php

use \Waavi\Translation\Models\Translation;

function translate($string, $params = []){

    try{

        $translation = Translation::where('item', $string)->where('locale', App::getLocale())->first()->text;

        foreach($params as $key => $value){
            $translation = str_replace(':'.$key, $value, $translation);
        }

    }catch(\Exception $e){

        return '"'.$string.'"'." has no translation";

    }
    return $translation;
}

function reverseTranslate($string){

    return Translation::where('text', $string)->first()->item;

}

?>

Then you only need to create a service provider like this :

...
public function register(){
         require_once app_path('Helpers/TranslationHelper.php');
}

And you can use it in every view / controller!