Skip to content

Commit

Permalink
Merge pull request #399 from koenvu/master
Browse files Browse the repository at this point in the history
Add translation template for labels
  • Loading branch information
kristijanhusak committed Feb 13, 2018
2 parents 264ce72 + 24a97d1 commit 9861696
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/Kris/LaravelFormBuilder/Fields/ChildFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ protected function getClassFromOptions()
$options = [
'model' => $this->getOption($this->valueProperty) ?: $this->parent->getModel(),
'name' => $this->name,
'language_name' => $this->parent->getLanguageName()
'language_name' => $this->parent->getLanguageName(),
'translation_template' => $this->parent->getTranslationTemplate(),
];

if (!$this->parent->clientValidationEnabled()) {
Expand Down Expand Up @@ -146,6 +147,10 @@ protected function getClassFromOptions()
$class->setLanguageName($this->parent->getLanguageName());
}

if (!$class->getTranslationTemplate()) {
$class->setTranslationTemplate($this->parent->getTranslationTemplate());
}

if (!$this->parent->clientValidationEnabled()) {
$class->setClientValidationEnabled(false);
}
Expand Down
11 changes: 9 additions & 2 deletions src/Kris/LaravelFormBuilder/Fields/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ public function render(array $options = [], $showLabel = true, $showField = true
'options' => $this->options,
'showLabel' => $showLabel,
'showField' => $showField,
'showError' => $showError
'showError' => $showError,
'translationTemplate' => $this->parent->getTranslationTemplate(),
]
)->render();
}
Expand Down Expand Up @@ -593,7 +594,13 @@ protected function setupLabel()
return;
}

if ($langName = $this->parent->getLanguageName()) {
if ($template = $this->parent->getTranslationTemplate()) {
$label = str_replace(
['{name}', '{type}'],
[$this->getRealName(), 'label'],
$template
);
} elseif ($langName = $this->parent->getLanguageName()) {
$label = sprintf('%s.%s', $langName, $this->getRealName());
} else {
$label = $this->getRealName();
Expand Down
29 changes: 29 additions & 0 deletions src/Kris/LaravelFormBuilder/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ class Form
*/
protected $languageName;

/**
* @var string
*/
protected $translationTemplate;

/**
* To filter and mutate request values or not.
*
Expand Down Expand Up @@ -491,6 +496,7 @@ public function setFormOptions(array $formOptions)
$this->pullFromOptions('client_validation', 'setClientValidationEnabled');
$this->pullFromOptions('template_prefix', 'setTemplatePrefix');
$this->pullFromOptions('language_name', 'setLanguageName');
$this->pullFromOptions('translation_template', 'setTranslationTemplate');

return $this;
}
Expand Down Expand Up @@ -870,6 +876,29 @@ public function setLanguageName($prefix)
return $this;
}

/**
* Get the translation template.
*
* @return string
*/
public function getTranslationTemplate()
{
return $this->translationTemplate;
}

/**
* Set a translation template, used to determine labels for fields.
*
* @param string $template
* @return $this
*/
public function setTranslationTemplate($template)
{
$this->translationTemplate = (string) $template;

return $this;
}

/**
* Render the form.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Fields/ChildFormTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ public function it_implicitly_inherits_language_name()
$this->assertEquals('test_name', $plainChild->getLanguageName());
}

/** @test */
public function it_implicitly_inherits_translation_template()
{
$plainParent = $this->formBuilder->plain(['translation_template' => 'form.{name}.{type}']);
$plainChild = $this->formBuilder->plain();

$plainParent->add('a form', 'form', ['class' => $plainChild]);

$this->assertEquals($plainParent->getTranslationTemplate(), $plainChild->getTranslationTemplate());
$this->assertEquals('form.{name}.{type}', $plainChild->getTranslationTemplate());
}

/** @test */
public function it_does_not_overwrite_language_name()
{
Expand All @@ -30,4 +42,16 @@ public function it_does_not_overwrite_language_name()
$this->assertEquals('different_name', $plainChild->getLanguageName());
$this->assertEquals('test_name', $plainParent->getLanguageName());
}

/** @test */
public function it_does_not_overwrite_translation_template()
{
$plainParent = $this->formBuilder->plain(['translation_template' => 'test.{name}']);
$plainChild = $this->formBuilder->plain(['translation_template' => 'different.{name}']);

$plainParent->add('a form', 'form', ['class' => $plainChild]);

$this->assertEquals('different.{name}', $plainChild->getTranslationTemplate());
$this->assertEquals('test.{name}', $plainParent->getTranslationTemplate());
}
}
13 changes: 13 additions & 0 deletions tests/Fields/FormFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ public function it_translates_the_label_if_translation_exists()
);
}

/** @test */
public function it_translates_the_label_using_translation_templates()
{
// We use build in validation translations prefix for easier testing
// just to make sure translation file is properly used
$this->plainForm->setTranslationTemplate('validation.{name}')->add('accepted', 'text');

$this->assertEquals(
'The :attribute must be accepted.',
$this->plainForm->accepted->getOption('label')
);
}

/** @test */
public function provided_label_from_option_overrides_translated_one()
{
Expand Down

0 comments on commit 9861696

Please sign in to comment.