Current Location: Home> Latest Articles> Use vsprintf to implement multilingual string templates

Use vsprintf to implement multilingual string templates

gitbox 2025-06-04

In multilingual (i18n) development, programmers often need to insert variables, such as user name, time, quantity, etc. into template strings. If string splicing is used directly, not only does the code readability be poor, but it is also difficult to maintain. The vsprintf() function provided by PHP is a powerful tool to solve this problem. It combines formatting strings and array parameters to achieve flexible and powerful dynamic replacement functions, especially suitable for multilingual scenarios.

1. What is vsprintf() ?

vsprintf() is a string formatting function in PHP. Its working principle is similar to sprintf() , the difference is that parameters are passed in as arrays. This allows us to easily pass arrays (such as multilingual variables obtained from database) into a string template.

The syntax is as follows:

 string vsprintf(string $format, array $values)
  • $format : A string with format placeholders.

  • $values : an array where each value in the array will replace the placeholders in the string in turn.

2. Application of vsprintf() in multilingual templates

Consider an example of a multilingual system. We need to display the following information:

English : Hello, John! You have 5 unread messages.
Chinese : Hello, John! You have 5 unread messages.

In order to flexibly replace variables (username and message number), we can use these two sentences as templates and mark them with the placeholder %s .

Sample code:

 <?php

// Assume a language template loaded from a language file or database
$translations = [
    'en' => 'Hello, %s! You have %d unread messages.',
    'zh' => 'Hello,%s!You have %d Unread message。'
];

// User Information
$user = 'John';
$messageCount = 5;

// Current language
$lang = 'zh'; // or 'en'

// use vsprintf Make a replacement
$output = vsprintf($translations[$lang], [$user, $messageCount]);

echo $output;

?>

Output:

 Hello,John!You have 5 Unread message。

As you can see, we replace the two placeholders %s (string) and %d (integer) defined in the template with user name and message number respectively through vsprintf() .

3. Support the format of positional parameters

Placeholder positions may change when more complex language structures are needed. For example:

 $translations = [
    'en' => 'User %1$s has %2$d items in cart.',
    'fr' => 'Il y a %2$d articles dans le panier de l’utilisateur %1$s.'
];

By specifying the parameter position ( %1$s , %2$d ), we can avoid errors caused by different syntax order.

Example:

 <?php

$translations = [
    'en' => 'User %1$s has %2$d items in cart.',
    'fr' => 'Il y a %2$d articles dans le panier de l’utilisateur %1$s.'
];

$user = 'Alice';
$cartItems = 3;

$lang = 'fr';

echo vsprintf($translations[$lang], [$user, $cartItems]);

?>

Output:

 Il y a 3 articles dans le panier de l’utilisateur Alice.

4. Combining URL examples

Suppose we have a prompt: "Click here to visit your personal homepage: <a href='%s'>link</a>"

In actual use, you can write this:

 <?php

$translations = [
    'zh' => "Click here to visit your personal homepage:<a href='%s'>Link</a>"
];

$url = 'https://gitbox.net/user/profile';

echo vsprintf($translations['zh'], [$url]);

?>

Output:

 Click here to visit your personal homepage:<a href='https://gitbox.net/user/profile'>Link</a>

5. Summary

Using vsprintf() to implement dynamic replacement of multilingual string templates has the following advantages:

  • The template has clear structure and strong maintenance;

  • Support parameter position control and adapt to different syntax structure languages;

  • Conveniently integrate with array data and language packs;

  • Still flexible and efficient when outputting HTML and URLs.

For projects that use frameworks such as Laravel, Symfony, although they have their own encapsulated translation methods, the underlying implementation can still use vsprintf() as a replacement for template placeholders.

In short, mastering vsprintf() is an important part of writing elegant, multilingual friendly PHP code.