Advertising sustains the DA. Ads are hidden for members. Join today

Defining Strings

Last updated on
15 May 2025

This documentation is deprecated.

The documentation has moved

New Location https://project.pages.drupalcode.org/string/

The simplest way to define routes is to create a my_module_name.string.yml file. Each string is defined as a machine name in the form of namespace.key (for example dashboard.title) with the following properties:

  • default (fallbacks to machine name if left empty): The text that is displayed to the user when translation is not available. This is also the string for the singular case. Make sure it is clear this is singular, to ease translation (e.g. use "1 new comment" instead of "1 new"). Do not use @count in the singular string.
  • default_plural (fallbacks to machine name if left empty): The text that is displayed to the user when translation is not available. The string for the plural case. Make sure it is clear this is plural, to ease translation. Use @count in place of the item count, as in "@count new comments".
  • msgid: Use to reference text from other modules or core which used traditional methods like t, formatPlural, etc.
  • msgctxt: The context the source string belongs to. See the Internationalization topic for more information about string contexts.
  • comments: Text entered as comments would be used to provide additional information for translators. E.g. it would appear as notes for translators in the translation source (POT) file. 

Here's a more complex annotated example:

string_demo.string.yml

# Demo: String with a placeholder.
dashboard.welcome_message.short:
  default: "Hello @name!"
# Demo: Another string with placeholder.
dashboard.welcome_message.long:
  default: "Hello @name! Welcome back, good to see you again."
# Demo: Plural strings.
search.result.items_count:
  default: "@count item found"
  default_plural: "@count items found"
# Demo: Expose strings from contrib module.
# Following is a string which is actually used in a contrib module "Group".
drupal.contrib.groups.label:
  default: "Groups"
  msgid: "Groups"
# Demo: Expose strings from core.
# Following is a string from core.
drupal.core.module.extend.label:
  default: "Extend"
  msgid: "Extend"
# Demo: String Override.
# @TODO:
# Following is a way you could override an existing string.
# Much like string overrides. This allows us to not add
# another English language to replace existing string.
drupal.core.node.content.label:
  default: "Pages"
  msgid: "Content"
# A string with context.
# However it really doesn't make any sense.
dashboard.greetings.short:
  default: 'Sup @name!'
  msgid: 'Hi @name!'
  msgctxt: 'short_greeting'
# A string with context.
# However it really doesn't make any sense.
drupal.core.node.search_result_summary:
  default: 'One item found.'
  default_plural: '@count items found.'
  msgid: '@count item found'
  msgid_plural: '@count items found'
  # You may also add some comments for each string.
  comments:
    - 'Comment line 1'
    - 'Comment line 2'

Help improve this page

Page status: Deprecated

You can: