I have a site on Drupal 8.0.2 with a custom theme. For the sake of this description, I will call the .theme file "custom.theme".

In custom.theme, I added a hook_form_FORM_ID_alter() implementation to add some custom header strings to a form. I used the t() function to make these strings translatable.

Example:

  if (!empty($form['field_some_field'])) {
    $form['field_some_field']['#prefix'] = '<h3>' . t('A Custom Header') . '</h3>';
  }

I loaded the form in the site's default language (en) to verify that the form alter worked properly.

Then I went to admin/config/regional/translate so I could translate "A Custom Header" into other languages. I could not find the string.

Ultimately, I could only do so after I loaded the form in a language other than the default language. Once I did so, I could find and translate the string in admin/config/regional/translate.

Comments

paulmckibben created an issue. See original summary.

swentel’s picture

Hmm, this has the behavior since ages afaik, not sure how to get around that.

paulmckibben’s picture

@swentel, thanks!

If the consensus is "works as designed," I'm happy to help update documentation to make this less confusing.

swentel’s picture

Well, let's leave it open for a while to see if I'm not wrong :)

Or if anyone has some advice to get around the behavior.

I use https://www.drupal.org/project/potx usually though to extract the custom strings from my code and then bulk import them.

Berdir’s picture

Yes, this has always worked like that.

The reason is that it's part of the locale/interface translation code. It looks for a source/translation for every string, when it's actually running and implicitly creates/updates entries if they are missing.

That code never runs with english. It's likely that actually works as well when you enable the checkbox to also attempt to translate when using EN.

swentel’s picture

Status: Active » Closed (works as designed)

Ok, closing then.

Maybe we should mention this behavior somewhere on https://www.drupal.org/node/2415945 - but not sure.