0

I have a site which contains thousand of unused (and not translated) strings.

I would like to remove them to light up my database.

For the records,those strings where created because the original language of the source files has changed and because of some ugly code like t("$variable" bla bla")

My idea is to remove all the untranslated strings (which will contain used string) because the used strings will be auto-repopulated when needed. Here is the code I plan to use:

  $locale_storage = \Drupal::service('locale.storage');
  $strings = $locale_storage->getStrings(['translated' => FALSE);
  foreach ($strings as $string){
    $string_to_del = $locale_storage->findString(['source' => $string->getString()]);
    $locale_storage->delete($string_to_del);
  }

Is it safe?

2
  • Does this answer your question? How to bulk delete translation strings Commented Jul 18, 2023 at 5:50
  • 1
    No it doesn't because I don't want to remove the translated strings which are good. I would like to remove only the untranslated strings.
    – Baud
    Commented Jul 18, 2023 at 7:52

1 Answer 1

1

Instead of a one-time deletion, I would periodically export your important strings to code. This is easy to script and means you will always have a backup if deletion goes wrong or a translator makes a mistake. Drush makes it easy.

Export strings as PO files with Drush

You can remove all untranslated strings with a DB command but I would back up your translated strings first, specifically your customized strings:

Use drush locale:export to export strings to a PO file for each language:

drush locale:export ja --types=customized > translations/en.po

You could try keeping the not-customized strings as well, but since those are imported from drupal.org, you probably don't need to back them up.

You can then commit these .po files to your git repo to have a backup of the strings in the database.

Wipe all strings

See how to bulk delete translation strings.

Re-import the customized strings

Use drush locale:import:

drush locale:import en translations/en.po --type=customized --override=customized

Additional resource

If the drush locale commands aren't sufficient, the Drush language commands module adds some more useful stuff.

3
  • Your answer is nice and very clear but it doesn't answer my questions which are: 1-Could I safely delete all the NOT TRANSLATED strings? If yes, 2-Could I use the provided code in the OP?
    – Baud
    Commented Jul 18, 2023 at 19:37
  • @Baud As for safety, as I said, I think it's a good idea to back up the strings by exporting first. Can you delete untranslated strings? Yes. Will your code work? I don't know, I didn't test it. I assume you have a local dev site set up and will run the code on that first. Commented Jul 18, 2023 at 23:05
  • 1
    I have exported the strings using drush but I had to remove --types=customized to get all the strings of my source files. I will credit you for the answer. Many thanks
    – Baud
    Commented Jul 19, 2023 at 10:18

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.