PHP code example of casa-parks / extract-translations

1. Go to this page and download the library: Download casa-parks/extract-translations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

casa-parks / extract-translations example snippets


'providers' => [
    // Other service providers...

    CasaParks\ExtractTranslations\ExtractTranslationsServiceProvider::class,
],



namespace App\Composers;

use CasaParks\ExtractTranslations\Builder as TranslationsExtractorBuilder;
use Illuminate\Contracts\View\View;

class TranslationsComposer
{
    /**
     * The translations extractor builder.
     *
     * @var \CasaParks\ExtractTranslations\Builder
     */
    protected $builder;

    /**
     * Whether the data is cached or not.
     *
     * @var bool
     */
    protected $cached;

    /**
     * The view data.
     *
     * @var array
     */
    protected $data;

    /**
     * Creates a new translations composer.
     *
     * @param \CasaParks\ExtractTranslations\Builder $builder
     */
    public function __construct(TranslationsExtractorBuilder $builder)
    {
        $this->builder = $builder;
    }

    /**
     * Compose the view.
     *
     * @param \Illuminate\Contracts\View\View $view
     *
     * @return void
     */
    public function compose(View $view)
    {
        if (! $this->cached) {
            $this->cache();
        }

        $view->with($this->data);
    }

    /**
     * Cache the data.
     *
     * @return void
     */
    protected function cache()
    {
        $this->cached = true;

        $translations = $this->builder
            ->locales('en', 'de')
            ->groups('pagination', 'validation')
            ->service();

        $this->data = compact('translations');
    }
}

/**
 * Register any composers for your application.
 *
 * @return void
 */
public function boot()
{
    // ...

    // assuming `layout` is your common layout template.
    $this->app['view']->composer('layout', 'App\Composers\TranslationsComposer');

    // ...
}

namespace App\Http\Controllers\Api;

use CasaParks\ExtractTranslations\Builder;

class TranslationController extends Controller
{
    /**
     * Get the available translations.
     *
     * @param \CasaParks\ExtractTranslations\Builder $builder
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Builder $builder)
    {
        return $builder->locales('en', 'de')
            ->groups('pagination', 'validation')
            ->service();
    }
}

$router->get('/api/translations', [
    'as' => 'get::api.translations',
    'uses' => 'Api\TranslationController@list',
]);

namespace App\Http\Controllers\Api;

use CasaParks\ExtractTranslations\Builder;

class TranslationController extends Controller
{
    /**
     * Get the available translations.
     *
     * @param string                                 $translation
     * @param \CasaParks\ExtractTranslations\Builder $builder
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function get($translation, Builder $builder)
    {
        $translations = $builder->locales($translation)
            ->groups('pagination', 'validation')
            ->service()
            ->toArray();

        return array_get($translations, $translation, []);
    }
}

$router->get('/api/translations/{translation}', [
    'as' => 'get::api.translation',
    'uses' => 'Api\TranslationController@get',
]);
blade
<!-- ... -->
<head>
    <!-- ... -->

    <script>window.translations = {!! $translations->toJson() !!}</script>

    <!-- ... -->
</head>
<!-- ... -->
js
function translate(translation) {
    window.axios.get(`/api/translations/${translation}`)
        .then(translations => window.translations = translations);
}