PHP code example of survos / libre-translate-bundle

1. Go to this page and download the library: Download survos/libre-translate-bundle 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/ */

    

survos / libre-translate-bundle example snippets


    #[Route('/{target}', name: 'app_homepage')]
    public function home(
        LibreTranslate $libreTranslate,
        CacheInterface $cache,
        string         $target = 'es'): Response
    {
        $url = 'https://saurav.tech/NewsAPI/top-headlines/category/health/in.json';
        $data = $cache->get(md5($url), fn(CacheItem $item) => json_decode(file_get_contents($url)));
        $translations = [];
        foreach ($data->articles as $idx => $article) {
            $translations[] = $cache->get(md5($article->title).$target,
                fn(CacheItem $cacheItem) => $libreTranslate->Translate($article->title, target: $target)
            );
        }

        return $this->render('app/index.html.twig', [
            'headlines' => $data,
            'translations' => $translations,
            'languages' => $libreTranslate->Languages()
        ]);
    }