How can I find the current language in a Laravel view?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Discovering the Current Language in a Laravel View: Practical Solutions Introduction: As a Laravel developer, you may find yourself in the situation where you need to display the current language used by your web application within its views. The default Lang class in Laravel provides localization and translation capabilities along with an array of configured languages. However, it does not have a direct method for obtaining the currently active language from the view. This blog post will explore several ways to achieve that goal using various approaches. Solution 1: Parsing the URI One reliable way to find out the current language in your Laravel application is by parsing the URL within your view. The default route prefix for each language usually follows a consistent pattern (e.g., /en/ or /fr/). You can use the built-in PHP function `parse_url()` to extract the path from the given URI and then find the language code within it: ```php // In your view, add this code: $uri = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'); if (strpos($uri, '/') === 0) { $uri = ltrim($uri); } $language_code = substr($uri, 1, strpos($uri, '/', 1)); ``` Now you have the language code in `$language_code`, which can be used for various purposes. Solution 2: Accessing Route Model Bindings When using route model bindings, the corresponding language may already be accessible from within your view. You just need to determine if a language has been specified as part of the route: ```php $language_code = null; if (isset($route->language)) { $language_code = $route->language; } ``` Solution 3: Leveraging Laravel Blade macros Laravel provides a powerful feature called "Blade Macros" that allows you to create custom functions within your views. To find the current language, you can create a macro like this: ```php // In your view: @macro('getCurrentLanguage') $uri = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'); if (strpos($uri, '/') === 0) { $uri = ltrim($uri); } $language_code = substr($uri, 1, strpos($uri, '/', 1)); return $language_code; @endmacro ``` Call the macro in your view: ```php {{ getCurrentLanguage() }} ``` Solution 4: Using Laravel's Route Model Bindings with Blade Macros If you have a consistent language prefix in your routes, such as '/lang/en', you can use the Laravel's built-in `route()` helper and the newly created macro to obtain the current language code without parsing the URI: ```php // In your view: @macro('getCurrentLanguage') return Route::getCurrentRoute()->getPrefix(); @endmacro {{ getCurrentLanguage() }} ``` Conclusion: While Laravel's built-in Lang class does not have a direct method to find the current language, there are multiple ways to accomplish this task. You can parse the URI, access route model bindings, or leverage Blade macros with either route information or your application's naming conventions. Choose the solution that best fits your needs and enhances the maintainability of your codebase. Always remember to keep your Laravel web application up-to-date for optimal performance and new features, such as those offered by https://laravelcompany.com/.