Step 1: Organize Language Files
- Locate Language Directory: In Laravel, language files are stored in the
resources/langdirectory. - 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
'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
'مرحبًا بكم في موقعنا!',
'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:
@lang('messages.welcome')
Alternatively, you can use the __() helper function:
{{ __('messages.welcome') }}
Now your app will show different text depending on the selected language!