John Davidson

php - Multilingualism website display Post by language or default

0 comments
Message:


I am programming a Multilingualism website with symfony
my article table:






























































id original_id rubrique_id status lang updated_at title
1 1 1 1 en 2021-02-16 00:01:00 pede libero En
2 1 1 1 fr 2021-02-16 00:02:00 pede libero Fr
3 1 1 1 * 2021-02-16 00:03:00 pede libero All
4 4 1 1 fr 2021-02-16 00:04:00 rhoncus aliquet Fr
5 4 1 1 * 2021-02-16 00:05:00 rhoncus aliquet All


My Answer:
To display posts on a multilingual website based on the user's language preference or a default language, you can use Symfony's translation component along with the Symfony Doctrine ORM to fetch and display the appropriate content.

Here's a basic example of how you can achieve this:

1. Create a Symfony controller action to handle the display of posts:

php
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Article;

class ArticleController extends AbstractController
{
public function displayArticle(Request $request)
{
$language = $request->getLocale(); // Get the user's preferred language from the request

$defaultLanguage = 'en'; // Set a default language

$articles = $this->getDoctrine()->getRepository(Article::class)->findByLanguage($language, $defaultLanguage);

return $this->render('article/index.html.twig', [
'articles' => $articles,
]);
}
}


2. Create a custom repository method in your Article entity to fetch articles based on language:

php
namespace App\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Article;

class ArticleRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Article::class);
}

public function findByLanguage($language, $defaultLanguage)
{
return $this->createQueryBuilder('a')
->where('a.lang = :language OR a.lang = :defaultLanguage')
->setParameter('language', $language)
->setParameter('defaultLanguage', $defaultLanguage)
->getQuery()
->getResult();
}
}


3. Update your Article entity to include a language field:

php
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
*/
class Article
{
// Other entity properties

/**
* @ORM\Column(type="string", length=2)
*/
private $lang;

// Getters and setters for lang field
}


4. Update your Twig template to display the articles:

twig
{% for article in articles %}

{{ article.title }}


{{ article.content }}


{% endfor %}


This is a basic example to get you started. You can expand on this by adding more languages, implementing language switching functionality, and handling translations for each article.

Rate this post

3 of 5 based on 5753 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us