Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

saving translation records #43

Closed
nexana opened this issue Sep 11, 2014 · 5 comments
Closed

saving translation records #43

nexana opened this issue Sep 11, 2014 · 5 comments

Comments

@nexana
Copy link

nexana commented Sep 11, 2014

Does the package automatically create records in the translation table when an entity is first saved?
Or should I handle this manually?

A user on stackoverflow asked a similar question:
http://stackoverflow.com/questions/25409328/laravel4-manage-translation-in-db

Thank you!

@nexana
Copy link
Author

nexana commented Sep 12, 2014

I am asking this because I have an entity "Page", for which I want translations in 3 different locales.
When I create a new Page in the database, I would expect that for each locale set in the config, a PageTranslation record is also created.

Current setup:

In my app config I have set the locales:
'locales' => array('nl','en','fr'),
'locale' => 'nl',

class Page extends Eloquent
{
use Dimsav\Translatable\Translatable;

public $translatedAttributes = array('title','content');
protected  $table = 'pages';
protected $fillable = array('name','order','showinmenu');
}


class PageTranslation extends Eloquent implements SluggableInterface
{
protected  $table = 'page_translations';
protected $fillable = array('title', 'description','order','locale');
}

And In my pagecontroller I create a new page:
class PageController extends \BaseController {
public function store()
{
$page = new Page();
$page->name = Input::get('name');
$page->showinmenu = Input::has('showinmenu');
$page->save();
}
}

@jonasva
Copy link

jonasva commented Sep 12, 2014

It only adds a record in your model's translations table for the current locale. Why would you want it to add empty records for the other locales?

@nexana
Copy link
Author

nexana commented Sep 12, 2014

Thank you very much for your answer. Now I know this is default behaviour.

To answer yours:
The application I am making let's the admin user add pages to the front-end.
When he adds a new page in the backend, he is then taken to a form where he can enter the translated content for each locale available (ie a textfield for english, french, dutch,...content). So this is independent of his default locale.

It would be handy if the empty records for each locale were already available right after the user created the page.

@jonasva
Copy link

jonasva commented Sep 13, 2014

You can still do that by using the translate() method mentioned in the documentation. Like this:

$country->translate('en')->name = 'abc';
$country->save();

In the form the user is taken to after creating a page you could then make specific fields and save them per locale.

For example:
in your view you could put fields like this:

 {{ Form::label('content_en', 'English Content') }} 
 {{ Form::text('content_en',null,array('class' => 'form-control', 'required' => 'required')) }}

Then in your controller you translate the correct field:

 $country->translate('en')->content = Input::get('content_en');
 $country->save();

@dimsav
Copy link
Owner

dimsav commented Oct 9, 2014

Hey @nexana,

you don't need empty db records to show empty input forms. Laravel-translatable only adds db records for translations that are defined.

Cheers

@dimsav dimsav closed this as completed Oct 9, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants