Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sira's avatar
Level 2

Multilingual Blog in Laravel

Hi, in my project I have a CRUD in place for a news articles page, managed by Filament admin panel.

All of the website was already translated "manually" via __() functions and language .json files.

What is the best approach to translate also the news articles? Ideally when the user saves the article it would already have its translation available. Is there any package that uses a 3rd party API to automatically translate blog posts or articles created by CRUD operations?

Thank you in advance.

0 likes
2 replies
LaryAI's avatar
Level 58

One approach to translating news articles in Laravel is to use the Laravel Localization feature along with a translation package. Here's a step-by-step solution:

  1. Set up Laravel Localization:

    • Open the config/app.php file and uncomment the Illuminate\Translation\TranslationServiceProvider service provider.
    • In the same file, set the locale option to the default language you want to use.
    • Create language files for each supported language in the resources/lang directory. For example, create en for English and es for Spanish.
    • In each language file, define translation strings using the __('key') function. For example, in resources/lang/en/messages.php, you can define 'welcome' => 'Welcome to our blog!'.
  2. Implement translation for news articles:

    • Add a locale column to the news_articles table to store the language of each article.
    • When saving a new article, store the selected language in the locale column.
    • Create a translation table, such as news_article_translations, with columns like news_article_id, locale, title, content, etc.
    • When saving an article translation, store the translation in the news_article_translations table, linking it to the corresponding news_article_id and locale.
  3. Use a translation package:

    • One popular package for managing translations in Laravel is "spatie/laravel-translatable". Install it using Composer:
      composer require spatie/laravel-translatable
      
    • Follow the package's documentation to set up the required migrations and models.
    • Modify your NewsArticle model to use the Translatable trait provided by the package.
    • Define translatable attributes in your model, such as title and content.
    • Use the translatable scope provided by the package to retrieve translated articles.
  4. Automatic translation using a 3rd party API:

    • There are several translation APIs available, such as Google Translate or Microsoft Translator.
    • Choose an API and sign up for an account to obtain an API key.
    • Install a package that integrates with your chosen API, such as "stichoza/google-translate-php" for Google Translate.
    • In your controller or service, use the API package to automatically translate the article's title and content when saving.
    • For example, you can listen to the saving event of the NewsArticle model and call the translation API to translate the attributes before saving.

Remember to handle any errors that may occur during translation and provide fallbacks for untranslated content.

Note: This solution is a general guideline and may require customization based on your specific project requirements.

vincent15000's avatar

You can for example use a translation API.

https://translated.com/welcome

The Spatie package suggested by the AI doesn' translate, but helps to manage the translations to store them in the database once you have all needed translations.

Please or to participate in this conversation.