I'm using User interface translation in English to override system strings like Drupal 7's String Overrides module did. Works great. However, in my custom module, as I edit strings that run through the t() function, the Source string table in User interface translation is filling up with unused strings.

I'm writing a simple function to delete unwanted source strings one-by-one. The post at http://www.drupal8.ovh/en/tutoriels/89/delete-a-source-string-and-target-translations-drupal-8 provides code for deleting a translation once you know the LID (Locale Id?), which it suggests you get by inspecting the HTML of the displayed User interface translation page.

Question: Is there a way to look up the LID for a source string using the text of the source string?

I've read many posts, but don't see any that suggest how to do this.

1 Answer 1


OK: Figured it out through trial and error. Here's the code:

$db = \Drupal::database();

$query = $db->select('locales_source', 'ls')->
    condition('source', $source_string)->
    fields('ls', ['lid'])->
    range(0, 1);

$lid = $query->execute()->fetchField();

UPDATE: Found a cleaner way. Here's the code:

$storage = \Drupal::service('locale.storage');
$string = $storage->findString(['source' => $source_string]);

if (!is_null($string)) {
  $lid = $string->lid;

I'm using this now in a custom module. Seems to be working well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.