We are working on an application with custom content types using fields in core. The site is supposed to be translated to German, and most fields in our content types are translatable, but we have a couple of "checkbox" fields (boolean, "simple on/off checkbox"), which do not seem to translatable, either the label or "help text".

I believe this issue applies to the HEAD version of D7, but we are using v. 7.0 (I can't find any issue which seems to directly relate to what we are seeing with the labels/help text for checkboxes). In fact I just tried to reproduce the issue in HEAD, and found that when I do a default install, turn on the locale module, add German as a site language and turn it on as "default", no German shows up on the site at all. So it seems you need to manually add the translation files in HEAD and I'm not sure what to do there. In the 7.0 version, this seems to be automatic; I think with just the core "Locale" module on and turning on German as the default language (it downloads files, then the site is in German and we can add our own custom translations for our extra fields).

I'll send a link to this issue to my colleague who was actually struggling with this issue today since he can likely add a bit more detail, but apparently this is not an isolated case. In my Google search, I found some discussion related to this issue on a German site:
http://www.drupalcenter.de/node/32639

But as far as I can tell, this issue is new to the queue here.

Comments

franz’s picture

Version: 7.0 » 8.x-dev

This applies to 8.x as well.

IMO, options labels - for radios and checkboxes - should be translated on _options_prepare_options()

nwom’s picture

The option and label is translated only when selecting "Default translated" instead of "Default" under "Manage Display." However the help text does not show as translated no matter what is chosen. Even if the string has been translated, it shows only the source. At least this is the case in the most recent version of Drupal 7.

mikeker’s picture

Title: Field label and help text for boolean fields (simple checkboxes) does not appear to be translatable » "On" and "Off" values for boolean fields are not translatable
Issue summary: View changes
StatusFileSize
new29.2 KB

I just verified this with the latest 8.0.x. The label and help text are available for translation but the "On" and "Off" labels are not.

Adjusted the title to match the issue in 8.x. That'll need to change if/when this gets tagged for backporting to 7.x.

mikeker’s picture

Status: Active » Needs review
Issue tags: +i18n, +internationalization
StatusFileSize
new12.62 KB
new2.84 KB

Turns out the Custom text field is not translatable either.

The attached patch fixes that issue.

However, I wasn't able to get the On/Off values to show on admin/structure/types/manage/article/fields/<node-type>.<field_machine_name>/translate/<language>/edit. I tried adding a @FieldType annotation similar to that for ImageItem, but that only allowed a translation when the content is being edited. I was trying to get it to show like the default value for ImageItem.

Any pointers would be appreciated.

mikeker’s picture

Oh, the above patch also adds a settingsSummary function which is missing for this formatter. Technically out of scope for this issue -- let me know if that needs a new issue instead.

Status: Needs review » Needs work

The last submitted patch, 4: 1061438-4_118n-boolean-fields.patch, failed testing.

mikeker’s picture

Status: Needs work » Needs review

Testbots were having problems and terminating part-way through, according to IRC. Retrying...

mercepedraza’s picture

I'm working on this in the mentored sprint in the Drupalcon Barcelona. I'll try to apply the patch and submit again the test.

sutharsan’s picture

I mentored @mercepedraza during DrupalCon Barcelona. Our discussion concluded that the patch in #4 uses a wrong approach.

+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/BooleanFormatter.php
@@ -102,7 +102,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
-      '#default_value' => $this->getSetting('format_custom_false'),
+      '#default_value' => $this->t($this->getSetting('format_custom_false')),

Wrapping a t() around a method call is D7 style of translating a configuration value. As far as I know, we can allow the default configuration to be translated by adding a schema for this form.

lomo’s picture

Status: Needs review » Needs work

In IRC, Sutharsan agreed that this should have been set back to "Needs work". If the issue has not been fixed or nobody else has assigned it to themselves by next week (when I hope to have some more sprinting time) and if someone has some tips for what the "right approach" would look like, perhaps I can make a patch that conforms to that.

sutharsan’s picture

This is a partial solution that adds modifies the core data type to make the on- and off-labels translatable.
Some faults that needs to be addressed:

  • Saving a translated configuration of the field (e.g. admin/structure/types/manage/article/fields/node.article.field_boolean/translate > edit > save), give the error Notice: Undefined index: field.storage.node.field_boolean in Drupal\config_translation\Form\ConfigTranslationFormBase->submitForm()
  • Manage display of a boolean field > configure field > select 'Custom' > Label fields don't have a default.

Todo:

  • Check if the right label language is used when editing an entity and when displaying the entity.
  • Add tests
badrange’s picture

A colleague tipped me about this issue:
https://www.drupal.org/node/2609874

Are these duplicate of one another?

mikeker’s picture

Status: Needs work » Closed (duplicate)

@badrange, I think you're right. And, as #11 points out there are better ways to do this, which are being used in the issue you linked to. I'll open a followup for the missing setSummary().

mikeker’s picture

proweb.ua’s picture

/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/BooleanFormatter.php

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];
    $setting = $this->getSetting('format');

    if ($setting == 'custom') {
      $summary[] = $this->t('Custom text: @true_label / @false_label', [
        '@true_label' => $this->getSetting('format_custom_true'),
        '@false_label' => $this->getSetting('format_custom_false'),
      ]);
    }
    else {
      $formats = $this->getOutputFormats();
      $summary[] = $this->t('Display: @true_label / @false_label', [
        '@true_label' => $formats[$setting][0],
        '@false_label' => $formats[$setting][1],
      ]);
    }

    return $summary;
  }

change
'@true_label' => t($this->getSetting('format_custom_true')),
'@false_label' => t($this->getSetting('format_custom_false')),

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];
    $setting = $this->getSetting('format');

    if ($setting == 'custom') {
      $summary[] = $this->t('Custom text: @true_label / @false_label', [
        '@true_label' => t($this->getSetting('format_custom_true')),
        '@false_label' => t($this->getSetting('format_custom_false')),
      ]);
    }
    else {
      $formats = $this->getOutputFormats();
      $summary[] = $this->t('Display: @true_label / @false_label', [
        '@true_label' => $formats[$setting][0],
        '@false_label' => $formats[$setting][1],
      ]);
    }

    return $summary;
  }
xdong’s picture

StatusFileSize
new1.99 KB