DeveloperBreeze

Creating Language Files in Laravel

Step 1: Organize Language Files

  1. Locate Language Directory: In Laravel, language files are stored in the resources/lang directory.
  2. Create Language Folders: For each language you want to support, create a folder inside resources/lang:
  • For English, create resources/lang/en
  • For Arabic, create resources/lang/ar

Step 2: Create Language Files

Inside each language folder, create files to store your translations. Let’s start by creating a messages.php file for both languages.

  • English File: resources/lang/en/messages.php
   <?php
   return [
       'welcome' => 'Welcome to our website!',
       'verify_accreditation' => 'I verify that I am an accredited user and I am using this at my own responsibility.',
   ];
  • Arabic File: resources/lang/ar/messages.php
   <?php
   return [
       'welcome' => 'مرحبًا بكم في موقعنا!',
       'verify_accreditation' => 'أؤكد أنني مستخدم معتمد وأتحمل مسؤولية استخدامي لهذا.',
   ];

Each key-value pair represents a text string in your app. Use the same keys in both language files, so Laravel can retrieve the appropriate translation based on the current language setting.

Step 3: Use Translation Strings in Views

In your Blade templates, replace hardcoded text with the @lang or __() functions to display translated strings. For example:

<!-- This will output the translated 'welcome' message based on the current language setting -->
<p>@lang('messages.welcome')</p>

Alternatively, you can use the __() helper function:

<p>{{ __('messages.welcome') }}</p>

Now your app will show different text depending on the selected language!

Related Posts

More content you might like

Tutorial
php

Using Laravel Config and Localization Based on Site Settings

Open App\Providers\AppServiceProvider.php and update the boot method:

   namespace App\Providers;

   use Illuminate\Support\ServiceProvider;
   use Illuminate\Support\Facades\Schema;
   use Illuminate\Support\Facades\Config;
   use Illuminate\Support\Facades\App;
   use App\Models\SiteSetting;

   class AppServiceProvider extends ServiceProvider
   {
       public function boot()
       {
           if (Schema::hasTable('site_settings')) {
               $settings = SiteSetting::pluck('value', 'key');

               // Set application name
               Config::set('app.name', $settings['app_name'] ?? Config::get('app.name'));

               // Set application timezone
               Config::set('app.timezone', $settings['app_timezone'] ?? Config::get('app.timezone'));

               // Set application language
               App::setLocale($settings['app_language'] ?? Config::get('app.locale'));
           }
       }
   }

Nov 16, 2024
Read More
Tutorial
javascript css +1

Understanding Laravel Layouts and Their Usage

Extend the master layout:

@extends('layouts.master')

@section('title', 'Home')

@section('content')
    <h1>Welcome to My Laravel App</h1>
    <p>This is the home page.</p>
@endsection

Aug 22, 2024
Read More
Tutorial
javascript

Integrating Vite with React in a Laravel Project: A Comprehensive Guide

   npm install @vitejs/plugin-vue @vitejs/plugin-pwa

Update your vite.config.js to include these plugins:

Aug 14, 2024
Read More
Tutorial

Integrating Vite with Laravel for Modern Web Development

This setup allows you to build Vue components within your Laravel application.

Vite supports environment variables that you can use to manage different settings for development and production. Create .env.development and .env.production files in your project root:

Aug 14, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!