Internationalization (i18n) in Drupal

Module developers should use Drupal’s i18n features to ensure that all user-facing text is translatable, thereby supporting multilingual functionality and making the site accessible to a global audience.

This course covers the use of the t() function in PHP, the Drupal.t() function in JavaScript, and the t filter in Twig templates. You will learn how to use placeholders for dynamic content, understand the importance of context in translations, and ensure that all strings are properly sanitized and escaped. By the end of this course, you will be able to make your Drupal site's interface fully translatable.

Key topics

  • Using the t() function in PHP code to make strings translatable
  • Implementing Drupal.t() in JavaScript code for translatable strings
  • Using placeholders for dynamic content in translatable strings
  • Adding context to translations to avoid ambiguity
  • Sanitizing and escaping dynamic content in translatable strings
  • Ensuring all user-facing strings in Twig templates are translatable using the t filter and {% trans %} tag
Tutorials in this course
Drupal 8, 9, 10, and 11
More information

Any text that will be displayed to user as part of your application's user interface should be passed through the t() function, or an equivalent, so that it can be translated into other languages as needed. This tutorial will look at how to use the t() function.

This tutorial contains information that applies to anyone writing modules or themes. And many of the tutorials you read on this site and on the web in general will expect that you understand how basic string translation works.

In this tutorial we'll look at:

  • Passing strings through the t() function or equivalent so they are available for translation
  • Using placeholders for dynamic content in translatable strings
  • Tips for making your code's interface strings easier to translate
More information

Sometimes your JavaScript needs to insert new strings into the user interface. In order to ensure that those user-facing strings can be translated into other languages, just like the rest of Drupal's user interface, you should make sure and use the Drupal.t function anytime you output a string of text.

Categories
Drupal 8, 9, 10, and 11
More information

In order to ensure that all user interface strings in your application can be translated using Drupal's localization system, any text you add to templates needs to use either the t filter or the {% trans %} tag. Anyone creating themes or editing template files associated with a theme or a module should know how to use these two utilities.

In this tutorial we'll look at:

  • How to use the t filter and {% trans %} tag in a Twig template
  • The differences between the two, and how to determine which one to use
  • How to translate strings assigned to variables in preprocess functions using the PHP t() function
This course appears in the following guides:
Module Development
Learn to output themeable and translatable content in modules and themes.

Output and Format Data in Code