Quick Start Guide¶
This guide will help you get started with the String module quickly. We'll cover the essential concepts and show you how to use the module in your Drupal project.
Basic Concepts¶
The String module introduces a new way to handle translatable strings in Drupal:
- String Identifiers: Instead of using the actual text, you use unique identifiers
- Namespaces: Strings are organized in namespaces (typically your module name)
- Groups: Strings can be grouped using dot notation
- Context: Strings can have different translations based on context
Your First String¶
- Create a new file in your custom module called
your_module.string.yml
:
# Basic string
your_module.welcome:
default: "Welcome to our site!"
# String with placeholder
your_module.greeting:
default: "Hello, @name!"
# Plural string
your_module.items:
default: "@count item"
default_plural: "@count items"
- Use the string in your code:
<?php
// Basic string
$output = t('your_module.welcome');
// String with placeholder
$output = t('your_module.greeting', ['@name' => $user->getDisplayName()]);
// Plural string
$output = format_plural($count, 'your_module.items', 'your_module.items');
String Management¶
Note
Managing String Translation can be done via Drupal Core's interface translation.
Viewing Strings¶
- Go to Configuration → Regional and language → String management
- Or visit
/admin/config/regional/string/manage
Exporting Strings¶
- Go to Configuration → Regional and language → String export
- Or visit
/admin/config/regional/string/export
- Select the strings you want to export
- Choose the export format
- Click "Export"
Importing Translations¶
- Go to Configuration → Regional and language → Import
- Or visit
/admin/config/regional/translate/import
- Upload your translation file
- Click "Import"
Best Practices¶
-
Use Meaningful Identifiers:
# Good user.profile.welcome: default: "Welcome to your profile" # Bad str1: default: "Welcome to your profile"
-
Group Related Strings:
# Good user.profile.welcome: default: "Welcome" user.profile.edit: default: "Edit Profile" user.profile.delete: default: "Delete Profile" # Bad welcome: default: "Welcome" edit_profile: default: "Edit Profile" delete_profile: default: "Delete Profile"
-
Use Context When Needed:
Tip
In most cases, when you use "group" as described in previous step, context might not be needed. avoiding un-necessary complications during usage.
For example, it is easier to use t('forms.button.submit')
over t('button.submit', [], ['context' => 'form_button']);
# Same string, different contexts
button.submit:
default: "Submit"
msgctxt: "form_button"
link.submit:
default: "Submit"
msgctxt: "navigation_link"
Common Tasks¶
Adding a New String¶
- Add the string to your
.string.yml
file - Clear the cache:
drush cr
Updating a String¶
- Update the string in your
.string.yml
file - Clear the cache:
drush cr
Translating a String¶
- Export the strings
- Translate them in your preferred editor
- Import the translations
Next Steps¶
- Learn more about String Management
- Explore Translation Features
- Check out Best Practices
- Read the API Reference