Parameters inside Laravel localized string

Parameters inside Laravel localized string

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Parameters inside Laravel localized strings

You want to store a sentence like Welcome %1, Please meet %2 in your lang/en/mymsgs.php file and then inject specific values for %1 and %2 when rendering that string in your views or controllers. This is a fundamental requirement for building dynamic user interfaces, but Laravel's default localization mechanism handles this differently than you might expect if you are coming from other frameworks.

Laravel does not natively support positional parameters like %1 or %2 within the translation strings stored in resources/lang. If you attempt to pass arguments using a standard translation call such as __('Welcome %1, Please meet %2', ['1' => 'John', '2' => 'Alice']), it will not work out of the box. The translator expects specific syntax based on how the string was stored.

To achieve this functionality, you must adopt one of two primary patterns: using named placeholders or leveraging Laravel's native %s syntax with positional arguments.

Using Named Placeholders

The most robust and readable approach is to define your strings using named placeholders. In your lang/en/mymsgs.php, you would store the string as follows:

return [
    'welcome' => 'Welcome :user, Please meet :guest',
];

When retrieving this string in your Blade templates or controllers, you use a specific format that matches the keys defined in the array. You can pass an associative array of values corresponding to those keys:

{{-- In a Blade view --}}
<p>{{ __('welcome', ['user' => 'John', 'guest' => 'Alice']) }}</p>

This approach offers several advantages over positional indexing. It makes the code self-documenting; reading ['user' => 'John'] immediately tells you what value goes where without needing to inspect the translation file first. It also significantly reduces the risk of order-of-arguments bugs, which are common when using numeric indices like %1 and %2.

Using Positional Placeholders

If you strictly prefer the %s syntax often seen in other localization libraries, Laravel supports it as well, but with a slight variation from your initial idea. You should use %s instead of generic numbers like %1. Your translation string would look like this:

return [
    'welcome' => 'Welcome %s, Please meet %s',
];

To fill these slots, you provide an ordered array of arguments:

{{-- Passing arguments in order --}}
<p>{{ __('welcome', ['John', 'Alice']) }}</p>

Laravel automatically replaces the first %s with the first item in the array and the second %s with the second item. While this mirrors the logic you originally requested, relying on positional arguments (%s) without explicit context can make maintaining translations harder, especially when strings grow complex or are updated by non-developers using translation management tools.

Important Considerations

When implementing dynamic strings, keep in mind that Laravel's translation system is case-sensitive regarding keys. Ensure your file paths and key names match exactly between the language array and the usage site. Additionally, if you need to support multiple languages (e.g., Spanish or French), remember that you must define these placeholders in every corresponding lang/xx/mymsgs.php file. If a translation for a specific language is missing, Laravel will fall back to the default English string, preserving your dynamic behavior across locales.

If you require more advanced localization features or need to integrate with third-party translation services, consider exploring how Laravel handles external translation packages or consulting resources from https://laravelcompany.com for deeper insights into internationalization best practices.

Our network