question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Save and back with translations

See original GitHub issue

Bug report

Using https://backpackforlaravel.com/docs/4.1/crud-operation-update#translatable-models-and-multi-language-cruds I’ve found two issues editing a field:

  1. when you add a new record, language selector does’nt appear, you have to save a value then edit again
  2. when a field jus have one value for a language, button “Save and back” does’nt works well, save but not go back

Is it a bug in the latest version of Backpack? YES

After I run composer update backpack/crud the bug… is it still there? YES

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

PHP VERSION:

PHP 7.4.25 (cli) (built: Oct 23 2021 18:05:05) ( NTS ) Copyright © The PHP Group Zend Engine v3.4.0, Copyright © Zend Technologies with Zend OPcache v7.4.25, Copyright ©, by Zend Technologies with Xdebug v3.1.1, Copyright © 2002-2021, by Derick Rethans

LARAVEL VERSION:

v8.73.1@36dfae9d9ef7f88e8f9489c484a0a0609592bc21

BACKPACK VERSION:

4.1.60@bec5cfa11f0cd0712c79256748acf5850080c0ee

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
Mello21centurycommented, Nov 22, 2021

I’ve done it myself on my project. I’ll try to explain it as much as possible

This code goes into app\Models\Traits\HasTranslations.php

<?php

namespace App\Models\Traits;

use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\HasTranslations as BackpackHasTranslations;
use Illuminate\Support\Arr;

trait HasTranslations
{
	use BackpackHasTranslations {
		BackpackHasTranslations::create as parentCreate;
		BackpackHasTranslations::update as parentUpdate;
	}


	/**
	 * Create translated items as json.
	 *
	 * @param  array  $attributes
	 * @return static
	 */
	public static function create(array $attributes = [])
	{
		$locale = $attributes['locale'] ?? \App::getLocale();
		$attributes = Arr::except($attributes, ['locale']);
		$non_translatable = [];

		$model = new static();
		$translatableFields = array_keys($model->getTranslationsAttribute());

		// do the actual saving
		foreach ($attributes as $attribute => $value) {
			if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable
				$model->setTranslation($attribute, $locale, $value);
			} else { // the attribute is NOT translatable
				$non_translatable[$attribute] = $value;
			}
		}
		foreach ($translatableFields as $translatableField) {
			foreach (request()->get($translatableField) as $locale => $value) {
				$model->setTranslation($translatableField, $locale, $value);
			}
		}

		$model->fill($non_translatable)->save();

		return $model;

	}

	/**
	 * Update translated items as json.
	 *
	 * @param  array  $attributes
	 * @param  array  $options
	 * @return bool
	 */
	public function update(array $attributes = [], array $options = [])
	{
		if (!$this->exists) {
			return false;
		}

		$locale = $attributes['locale'] ?? \App::getLocale();
		$attributes = Arr::except($attributes, ['locale']);
		$non_translatable = [];

		// do the actual saving
		foreach ($attributes as $attribute => $value) {
			if ($this->isTranslatableAttribute($attribute)) { // the attribute is translatable
				$this->setTranslation($attribute, $locale, $value);
			} else { // the attribute is NOT translatable
				$non_translatable[$attribute] = $value;
			}
		}
		$translatableFields = array_keys($this->getTranslationsAttribute());
		foreach ($translatableFields as $translatableField) {
			foreach (request()->get($translatableField) as $locale => $value) {
				$this->setTranslation($translatableField, $locale, $value);
			}
		}

		return $this->fill($non_translatable)->save($options);

	}
}

Add these two files into resources/views/vendor/backpack/crud/fields/

FILE: text_locale.blade.php

<!-- field_type_name -->
@php($values = $crud->entry ? $crud->entry->getTranslations($field['name']) : [])
@foreach(config('backpack.crud.locales') as $langKey => $langValue)

	<?php
	$newField = $field;
	$newField['name'] = $field['name'] . '[' . $langKey . ']';
	$newField['label'] = $field['label'] . ' ' . $langValue;
	$newField['type'] = 'text';
	$newField['value'] = @$values[$langKey];
	?>
    @include('crud::fields.text', ['field' => $newField])
@endforeach

FILE: textarea_locale.blade.php

<!-- field_type_name -->
@php($values = $crud->entry ? $crud->entry->getTranslations($field['name']) : [])
@foreach(config('backpack.crud.locales') as $langKey => $langValue)

	<?php
	$newField = $field;
	$newField['name'] = $field['name'] . '[' . $langKey . ']';
	$newField['label'] = $field['label'] . ' ' . $langValue;
	$newField['type'] = 'text';
	$newField['value'] = @$values[$langKey];
	?>
    @include('crud::fields.textarea', ['field' => $newField])
@endforeach

Now in your model that uses the HasTranslations trait change the trait to

use App\Models\Traits\HasTranslations;

Now in any CRUD controller you may use those new fields

CRUD::field('name')->label(__('Name'))->type('text_locale');
CRUD::field('description')->label(__('Description'))->type('textarea_locale');

Hope this helps

0reactions
pxpmcommented, Nov 26, 2021

@blondie63 Thanks for the feedback, it’s nice to know it’s working. Very appreciated the feedback!

Cheers man 🍺

Read more comments on GitHub >

github_iconTop Results From Across the Web

Didn't we already fix this? Why you keep seeing the same ...
It is the database that remembers your project has been translated in the past, pairing your text with past results depending on how...
Read more >
Google Translate
No information is available for this page.
Read more >
Save and Submit Translations - Smartling Help Center
Save in bulk: Click Save Page to save multiple translated strings at once. The bulk save functionality will save the strings for the...
Read more >
Translations not saved · Issue #23033 · PrestaShop ... - GitHub
Go to 'International -> Translations'; Select any installed module translation and click edit; Input a translation, and click save; Return to ...
Read more >
Error Save Translation? - Avaya: CM/Aura (Definity) - Tek-Tips
Check the software load against each other. Now you will run the command 'save trans'. This backs up what is in active memory...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found