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

Creating (non-fallback) translations doesn't work if $useTranslationFallback is set #52

Closed
lan0 opened this issue Sep 24, 2014 · 1 comment
Assignees
Labels
Milestone

Comments

@lan0
Copy link
Contributor

lan0 commented Sep 24, 2014

When setting the $useTranslationFallback attribute on a model, creating non-default translations doesn't work out of the box.

An example:

App::make('config')->set('app.fallback_locale', 'en');
$country = Country::create(['iso' => 'gr']);
$country->useTranslationFallback = true;
$country->translate('en')->name = 'Greece';
$country->translate('de')->name = 'Griechenland';
dd($country->translate('en')->name);

This should dump Greece, but the entry for the fallback locale got overwritten by Griechenland.

I looked through your code and didn't find a simple solution to this problem, as this behaviour is intended when fetching translations but not when setting them. To distinguish between this two cases I added a third parameter to getTranslation() that is true when a translation should get created (when called from fill()). Then I added && ! $creating to the elseif here. Of course this doesn't work for setting attributes via translate(), which is why I didn't submit a pull request.

@dimsav
Copy link
Owner

dimsav commented Oct 9, 2014

Hi @lan0,

thanks for mentioning the issue. I don't think a third parameter is needed. The problem here is that getTranslation() will take $this->useTranslationFallback if set first, and secondly $withFallback.

We could solve this issue by making $withFallback default to null, and give it higher priority if set. Something like that:

/**
 * Alias for getTranslation()
 */
public function translate($locale = null, $withFallback = null)
{
    return $this->getTranslation($locale, $withFallback);
}

public function getTranslation($locale = null, $withFallback = null)
{
    $locale = $locale ?: App::getLocale();

    if ( ! isset($withFallback))
    {
        $withFallback = isset($this->useTranslationFallback) ? $this->useTranslationFallback : false;
    }

    // ...
}

After this change, the following code will return the appropriate locale:

App::make('config')->set('app.fallback_locale', 'en');
$country = Country::create(['iso' => 'gr']);
$country->useTranslationFallback = true;
$country->translate('en', false)->name = 'Greece';
$country->translate('de', false)->name = 'Griechenland';
dd($country->translate('en')->name); // Will echo Greece

Is this convenient for your case? Anyhow I think I will do this change in v5.

I apologize for taking so long to answer. Unfortunately, I can't find easily the time to contribute to the package.

@dimsav dimsav added this to the version 5 milestone Oct 9, 2014
@dimsav dimsav added the feature label Oct 9, 2014
@dimsav dimsav self-assigned this Oct 9, 2014
lan0 added a commit to lan0/laravel-translatable that referenced this issue Nov 3, 2014
dimsav added a commit that referenced this issue Nov 5, 2014
Enabled creating fallback translations #52
@dimsav dimsav closed this as completed Nov 6, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants