I can not find out how to translate the new entered labels (Manage display -> Rewrite label -> New Label).

Comments

drupalfan2 created an issue. See original summary.

nikita_tt’s picture

Hi. I use this module for my own sites and recently I faced the same problem. Below you can find how I resolved it.

In my case, I have an entity type (custom but it doesn't matter) and a field (BaseField but it doesn't matter as well) that has rewritten label in one of the Form Modes.

See settings for the applicant_comment field!

The config file location: config/sync/core.entity_form_display.o_app.contractor.contractor_sc__referred.yml
Config file content:

uuid: 617715ee-abb1-49ca-b322-96ec2ac57b4e
langcode: en
status: true
dependencies:
  config:
    - core.entity_form_mode.o_answer.answer_application
    - core.entity_form_mode.o_app.contractor_sc__referred
    - field.field.o_app.contractor.field_company_details
    - field.field.o_app.contractor.field_decision_maker
    - field.field.o_app.contractor.field_employees_count
    - field.field.o_app.contractor.field_financial_status
    - field.field.o_app.contractor.field_need_people
    - field.field.o_app.contractor.field_need_work
    - field.field.o_app.contractor.field_personal_picture
    - field.field.o_app.contractor.field_projects_pictures
    - image.style.medium
    - dev-okto-pro--platform_app.o_app_type.contractor
  module:
    - allowed_formats
    - country
    - datetime
    - entity_form_field_label
    - field_group
    - myproject_app
    - myproject_questions
    - svg_image
    - telephone
    - text
third_party_settings:
  field_group:
    group_tabs:
      children:
        - group_personal_details
        - group_contact_details
        - group_files
        - group_questions
      parent_name: ''
      weight: 2
      format_type: tabs
      region: content
      format_settings:
        id: ''
        classes: ''
        direction: vertical
      label: Tabs
    group_questions:
      children:
        - field_need_work
        - group_organisation_questions_wra
        - answers
        - group_main_spec_wrapper
        - specialization_2
        - q__language_level
        - q__computer_skills
        - applicant_comment
        - next_action_date
      parent_name: group_tabs
      weight: 3
      format_type: tab
      region: content
      format_settings:
        id: ''
        classes: group-questions
        description: ''
        formatter: closed
        required_fields: true
      label: Questions
    group_personal_details:
      children:
        - group_wrapper_names
        - group_wrapper_birthday
        - group_wrapper_address
      parent_name: group_tabs
      weight: 0
      format_type: tab
      region: content
      format_settings:
        id: ''
        classes: group-personal-details
        description: ''
        formatter: closed
        required_fields: true
      label: 'Personal Details'
    group_contact_details:
      children:
        - contact_email
        - contact_phone
      parent_name: group_tabs
      weight: 1
      format_type: tab
      region: content
      format_settings:
        id: ''
        classes: group-contact-details
        description: ''
        formatter: closed
        required_fields: true
      label: 'Contact Details'
    group_wrapper_names:
      children:
        - first_name
        - last_name
      parent_name: group_personal_details
      weight: 20
      format_type: html_element
      region: content
      format_settings:
        id: ''
        classes: wrapper-names
        element: div
        show_label: false
        label_element: h3
        label_element_classes: ''
        required_fields: true
        attributes: ''
        effect: none
        speed: fast
      label: 'Wrapper Names'
    group_wrapper_address:
      children:
        - residence_country
        - residence_city
      parent_name: group_personal_details
      weight: 23
      format_type: html_element
      region: content
      format_settings:
        id: ''
        classes: wrapper-address
        element: div
        show_label: false
        label_element: h3
        label_element_classes: ''
        required_fields: true
        attributes: ''
        effect: none
        speed: fast
      label: 'Wrapper Address'
    group_wrapper_birthday:
      children:
        - birth_date
      parent_name: group_personal_details
      weight: 21
      format_type: html_element
      region: content
      format_settings:
        id: ''
        classes: wrapper-birthday
        element: div
        show_label: false
        label_element: h3
        label_element_classes: ''
        required_fields: true
        attributes: ''
        effect: none
        speed: fast
      label: 'Wrapper Birthday'
    group_main_spec_wrapper:
      children:
        - specialization_1
        - equipment_1
      parent_name: group_questions
      weight: 3
      format_type: html_element
      region: content
      format_settings:
        id: ''
        classes: spec-main-wrapper
        element: div
        show_label: false
        label_element: h3
        label_element_classes: ''
        required_fields: true
        attributes: ''
        effect: none
        speed: fast
      label: 'Main Specialization Wrapper'
    group_organisation_questions_wra:
      children:
        - q__have_organisation
        - q__create_organisation
      parent_name: group_questions
      weight: 1
      format_type: html_element
      region: content
      format_settings:
        show_empty_fields: false
        id: ''
        classes: org-q-wrapper
        element: div
        show_label: false
        label_element: h3
        label_element_classes: ''
        required_fields: true
        attributes: ''
        effect: none
        speed: fast
      label: 'Organisation Questions Wrapper'
    group_files:
      children:
        - field_personal_picture
        - field_projects_pictures
      parent_name: group_tabs
      weight: 2
      format_type: tab
      region: content
      format_settings:
        show_empty_fields: false
        id: ''
        classes: group-files
        description: ''
        formatter: closed
        required_fields: true
      label: Files
id: o_app.contractor.contractor_sc__referred
targetEntityType: o_app
bundle: contractor
mode: contractor_sc__referred
content:
  answers:
    type: o_answers_inline_entity_form_complex
    weight: 2
    region: content
    settings:
      form_mode: answer_application
      revision: '1'
      label_singular: ''
      label_plural: ''
      match_operator: CONTAINS
      override_labels: 0
      collapsible: 0
      collapsed: 0
      allow_new: 0
      allow_existing: 0
      allow_duplicate: 0
    third_party_settings:
      entity_form_field_label:
        rewrite_label: '1'
        new_label: ''
  applicant_comment:
    type: text_textarea
    weight: 7
    region: content
    settings:
      rows: 3
      placeholder: ''
    third_party_settings:
      allowed_formats:
        hide_help: '1'
        hide_guidelines: '1'
      entity_form_field_label:
        rewrite_label: '1'
        new_label: 'Describe extensively what is your experience in your main specialisation'
  birth_date:
    type: datetime_default
    weight: 25
    region: content
    settings: {  }
    third_party_settings: {  }
  contact_email:
    type: email_default
    weight: 1
    region: content
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  contact_phone:
    type: telephone_default
    weight: 2
    region: content
    settings:
      placeholder: ''
    third_party_settings: {  }
  equipment_1:
    type: myproject_equipment_selection
    weight: 22
    region: content
    settings:
      specialization_field: specialization_1
    third_party_settings:
      entity_form_field_label:
        new_label: ''
        rewrite_label: 0
  field_need_work:
    type: boolean_checkbox
    weight: 0
    region: content
    settings:
      display_label: true
    third_party_settings: {  }
  field_personal_picture:
    type: image_image
    weight: 0
    region: content
    settings:
      preview_image_style: large
      progress_indicator: throbber
    third_party_settings:
      entity_form_field_label:
        new_label: ''
        rewrite_label: 0
  field_projects_pictures:
    type: image_image
    weight: 1
    region: content
    settings:
      preview_image_style: medium
      progress_indicator: throbber
    third_party_settings:
      entity_form_field_label:
        new_label: ''
        rewrite_label: 0
  first_name:
    type: string_textfield
    weight: 23
    region: content
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  last_name:
    type: string_textfield
    weight: 24
    region: content
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  next_action_date:
    type: datetime_default
    weight: 8
    region: content
    settings: {  }
    third_party_settings: {  }
  q__computer_skills:
    type: options_buttons
    weight: 6
    region: content
    settings: {  }
    third_party_settings: {  }
  q__create_organisation:
    type: options_buttons
    weight: 2
    region: content
    settings: {  }
    third_party_settings: {  }
  q__have_organisation:
    type: options_buttons
    weight: 1
    region: content
    settings: {  }
    third_party_settings: {  }
  q__language_level:
    type: options_buttons
    weight: 5
    region: content
    settings: {  }
    third_party_settings: {  }
  residence_city:
    type: string_textfield
    weight: 6
    region: content
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  residence_country:
    type: country_autocomplete
    weight: 5
    region: content
    settings:
      size: '60'
      autocomplete_route_name: country.autocomplete
      placeholder: 'Start typing a country name ...'
    third_party_settings: {  }
  specialization_1:
    type: options_select
    weight: 21
    region: content
    settings: {  }
    third_party_settings: {  }
  specialization_2:
    type: options_buttons
    weight: 4
    region: content
    settings: {  }
    third_party_settings: {  }
hidden:
  access_date_to: true
  ad: true
  app_status: true
  cold_calls: true
  demo_user: true
  equipment_2: true
  field_company_details: true
  field_decision_maker: true
  field_employees_count: true
  field_financial_status: true
  field_need_people: true
  innovation_meetings: true
  langcode: true
  lead_quality: true
  lead_quality__general: true
  name: true
  myproject_comment: true
  myproject_responsible: true
  records: true
  revision_log: true
  revision_user: true
  source: true
  status: true
  translation: true
  user_id: true

And here is the French translation of this Form Mode.
Location: config/sync/language/fr/core.entity_form_display.o_app.contractor.contractor_sc__referred.yml
Content:

third_party_settings:
  field_group:
    group_questions:
      label: Questions
    group_personal_details:
      label: 'Détails personnels'
    group_contact_details:
      label: 'Détails du contact'
    group_files:
      label: 'Des dossiers'
content:
  applicant_comment:
    third_party_settings:
      entity_form_field_label:
        new_label: 'Décrivez en détail quelle est votre expérience dans votre spécialisation principale'
drupalfan2’s picture

Thank you, but my solution is very easy:

function my_preprocess_field(&$variables, $hook) {
  if ($variables['field_name'] == 'my_field_name') {
    if (stristr($_SERVER['SERVER_NAME'], "com")) {  // some condition if to translate or not
      $variables['label'] = "Translated label Text";
    }
  } 
}

But still this module needs a solution for its own.

nikita_tt’s picture

I think this is a more general problem related to the translation of third-party settings. And I suggest using config files for translating field labels and their rewrites instead of doing it via code.

drupalfan2’s picture

Yes we need a solution appropriate to this module.

promo-il’s picture

entity_form_field_label.module

function entity_form_field_label_field_widget_multivalue_form_alter(array &$elements, FormStateInterface $form_state, array $context) {
  $label_settings = $context['widget']->getThirdPartySettings('entity_form_field_label');

  if ($label_settings && !empty($label_settings['rewrite_label'])) {
    /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage_definition */
    $field_storage_definition = $context['items']->getFieldDefinition()
      ->getFieldStorageDefinition();
    $main_property = $field_storage_definition->getMainPropertyName();
    $is_multiple = $field_storage_definition->isMultiple();
/*WT-patch-own:translations*/ $label_settings['new_label'] = t($label_settings['new_label']);
    switch ($field_storage_definition->getType()) {
function entity_form_field_label_preprocess_field(&$variables) {
  $entity_display = EntityViewDisplay::collectRenderDisplay($variables['element']['#object'], $variables['element']['#view_mode']);
  $plugin = $entity_display->getRenderer($variables['element']['#field_name']);
  if ($plugin instanceof ThirdPartySettingsInterface) {
    $label_settings = $plugin->getThirdPartySettings('entity_form_field_label');
    if ($label_settings && !empty($label_settings['rewrite_label'])) {
/*WT-patch-own:translations*/
      //$variables['label'] = $label_settings['new_label'];
      $variables['label'] = t($label_settings['new_label']);
/*<<<*/
    }
  }
}

then reload form or display for not-default language
then add translation\s on /admin/config/regional/translate

dinazaur’s picture

Status: Active » Needs review

Actually, configuration just requires a schema to be translatable, in this patch was already added schema for widget. Also, dev version of the module already has a schema for formater. That's all you need to be able to translate the labels.

nikita_tt’s picture

Status: Needs review » Fixed
Related issues: +#3280148: Schema errors generated when using this module with phpunit

Fixed here: https://www.drupal.org/project/entity_form_field_label/issues/3280148
The 8.x-1.6 release contains these changes.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.