Create simple helper function to add a string translation programatically

Created on 5 October 2014, over 10 years ago
Updated 14 April 2025, 6 days ago

Problem/Motivation

Custom modules builder require a way to add interface translations strings to a drupal site. Programatically adding a string translation is currently fairly complicated.
Below is an example this is how to add a translation of an existing singular string.

    // Find existing source string.
    $string = $this->localeStorage->findString(array('source' => 'Log in'));
    // Create translation for new string and save it as non-customized.
    $translation = $this->localeStorage->createTranslation(array(
      'lid' => $string->lid,
      'language' => 'nl',
      'translation' => 'Inloggen',
    ))->save();

Inspired by this D7 helper function by fago: http://dropbucket.org/node/323

Proposed resolution

Create an helper function to add translations of singular and plural string translations of existing and new source strings. The TranslationManager is a candidate for placing this method.

Remaining tasks

none

User interface changes

none

API changes

Adds a method to add a translation.

✨ Feature request
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

locale.module

Created by

πŸ‡³πŸ‡±Netherlands sutharsan

Live updates comments and jobs are added and updated live.
  • D8MI

    (Drupal 8 Multilingual Initiative) is the tag used by the multilingual initiative to mark core issues (and some contributed module issues). For versions other than Drupal 8, use the i18n (Internationalization) tag on issues which involve or affect multilingual / multinational support. That is preferred over Translation.

  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thank you for sharing your idea for improving Drupal.

    We are working to decide if this proposal meets the Criteria for evaluating proposed changes. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or there is no community support. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024