Score:0

Is it safe to remove all untranslated strings?

sa flag

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?

cn flag
Does this answer your question? [How to bulk delete translation strings](https://drupal.stackexchange.com/questions/277149/how-to-bulk-delete-translation-strings)
Baud avatar
sa flag
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.
Score:1
cn flag

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.

Baud avatar
sa flag
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?
cn flag
@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.
Baud avatar
sa flag
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
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.