PluralVariants.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Drupal\config_translation\FormElement;
  3. use Drupal\Component\Utility\SafeMarkup;
  4. use Drupal\Core\Config\Config;
  5. use Drupal\Core\Language\LanguageInterface;
  6. use Drupal\language\Config\LanguageConfigOverride;
  7. /**
  8. * Defines form elements for plurals in configuration translation.
  9. */
  10. class PluralVariants extends FormElementBase {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. protected function getSourceElement(LanguageInterface $source_language, $source_config) {
  15. $plurals = $this->getNumberOfPlurals($source_language->getId());
  16. $values = explode(LOCALE_PLURAL_DELIMITER, $source_config);
  17. $element = [
  18. '#type' => 'fieldset',
  19. '#title' => SafeMarkup::format('@label <span class="visually-hidden">(@source_language)</span>', [
  20. // Labels originate from configuration schema and are translatable.
  21. '@label' => $this->t($this->definition->getLabel()),
  22. '@source_language' => $source_language->getName(),
  23. ]),
  24. '#tree' => TRUE,
  25. ];
  26. for ($i = 0; $i < $plurals; $i++) {
  27. $element[$i] = [
  28. '#type' => 'item',
  29. // @todo Should use better labels https://www.drupal.org/node/2499639
  30. '#title' => $i == 0 ? $this->t('Singular form') : $this->formatPlural($i, 'First plural form', '@count. plural form'),
  31. '#markup' => SafeMarkup::format('<span lang="@langcode">@value</span>', [
  32. '@langcode' => $source_language->getId(),
  33. '@value' => isset($values[$i]) ? $values[$i] : $this->t('(Empty)'),
  34. ]),
  35. ];
  36. }
  37. return $element;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function getTranslationElement(LanguageInterface $translation_language, $source_config, $translation_config) {
  43. $plurals = $this->getNumberOfPlurals($translation_language->getId());
  44. $values = explode(LOCALE_PLURAL_DELIMITER, $translation_config);
  45. $element = [
  46. '#type' => 'fieldset',
  47. '#title' => SafeMarkup::format('@label <span class="visually-hidden">(@translation_language)</span>', [
  48. // Labels originate from configuration schema and are translatable.
  49. '@label' => $this->t($this->definition->getLabel()),
  50. '@translation_language' => $translation_language->getName(),
  51. ]),
  52. '#tree' => TRUE,
  53. ];
  54. for ($i = 0; $i < $plurals; $i++) {
  55. $element[$i] = [
  56. '#type' => 'textfield',
  57. // @todo Should use better labels https://www.drupal.org/node/2499639
  58. '#title' => $i == 0 ? $this->t('Singular form') : $this->formatPlural($i, 'First plural form', '@count. plural form'),
  59. '#default_value' => isset($values[$i]) ? $values[$i] : '',
  60. '#attributes' => ['lang' => $translation_language->getId()],
  61. ];
  62. }
  63. return $element;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function setConfig(Config $base_config, LanguageConfigOverride $config_translation, $config_values, $base_key = NULL) {
  69. $config_values = implode(LOCALE_PLURAL_DELIMITER, $config_values);
  70. parent::setConfig($base_config, $config_translation, $config_values, $base_key);
  71. }
  72. }