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

Fallback to default if translation is missing #23

Closed
mbouclas opened this issue Jun 10, 2014 · 12 comments
Closed

Fallback to default if translation is missing #23

mbouclas opened this issue Jun 10, 2014 · 12 comments
Labels

Comments

@mbouclas
Copy link

Is there a way to do that? It is very common for translations to be missing in some languages, in which case we should fall back to the default one.

@dimsav
Copy link
Owner

dimsav commented Jun 10, 2014

Yes, please read the readme file for the basic instructions. The default locale is set like this.

@dimsav dimsav closed this as completed Jun 10, 2014
@mbouclas
Copy link
Author

ok, i don't think i made my self clear. Say you have EN as default and FR as a secondary. Field title in EN is there but title in FR is not in the DB cause the user has not translated it yet. Naturaly, a translate('fr')->title returns NULL.
So my question is, is there a way for the translate('fr')->title to fallback to translate('en')->title in case FR is for this row is NULL?

@dimsav dimsav reopened this Jun 10, 2014
@dimsav
Copy link
Owner

dimsav commented Jun 10, 2014

Oh, sorry. Now I see what you mean.

Well, this feature is not available at the moment. I am leaving the issue open until a solution is found.

@mbouclas
Copy link
Author

If i might suggest 2 approaches.

  1. a method called something translateOrFallback($attribute,$locale=null)
    which would be nice to use in templates. What we do is check if the
    translate($locale)->{$attribute} is_null and return
    translate($defaultLocale)->{$attribute}. It won't be too expensive on a
    reasonable amount of calls

  2. a slightly different approach. When we have the data from the DB we
    build an array like 'gr'=>array('title'=>'sdsds'). Then, in a method we do
    a if array_key_exists otherwise return default. Very cheap when it comes to
    multiple calls but we need to create that associative array.

What do you think?

On Tue, Jun 10, 2014 at 10:19 AM, Dimitrios Savvopoulos <
notifications@github.com> wrote:

Oh, sorry. Now I see what you mean.

Well, this feature is not available at the moment. I am leaving the issue
open until a solution is found.


Reply to this email directly or view it on GitHub
#23 (comment)
.

@dimsav
Copy link
Owner

dimsav commented Jun 10, 2014

// Default locale is 'en'

echo $country->getTranslation('cn', true)->name // 'Greece'

// Alias of the above
echo $country->translateOrDefault('cn')->name   // 'Greece'

Is something like that ok for you?

@mbouclas
Copy link
Author

that looks great

@sdebacker
Copy link
Collaborator

I deleted my previous comment as I didn't reed dimsav's comment correctly. It seems to be a good enhancement.

@dimsav
Copy link
Owner

dimsav commented Jun 10, 2014

Feature is now implemented in version 4.1

@dimsav dimsav closed this as completed Jun 10, 2014
@ghost
Copy link

ghost commented Jun 15, 2014

I'm having trouble trying to understand the new getTranslation implementation. I'll try to elaborate the (i think) desired behaviur in pseudo code:

if there is a translation for current locale
    we are lucky
elseif we accept a fallback and there is actually a translation for the fallback
    lucky again
else
    unlucky

Something like this is working for me

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

    if ($this->getTranslationByLocaleKey($locale))
    {
        $translation = $this->getTranslationByLocaleKey($locale);
    }
    elseif ($withFallback && $this->getTranslationByLocaleKey(\Config::get('app.fallback_locale')))
    {
        $translation = $this->getTranslationByLocaleKey(\Config::get('app.fallback_locale'));
    }
    else
    {
        $translation = $this->getNewTranslationInstance($locale);
        $this->translations->add($translation);
    }

    return $translation;
}

Please tell me if I'm missing something :) Thank you!

@dimsav
Copy link
Owner

dimsav commented Jun 15, 2014

Hi @gpasci, I understand your confusion. However, could you please explain where you would use this behaviour?

@dimsav dimsav reopened this Jun 15, 2014
@ghost
Copy link

ghost commented Jun 17, 2014

Hi, sorry for the delay. I'm setting app.locale on app boot according to the current URL segment:

  • app.com/content -> english,
  • app.com/es/content -> spanish,
  • app.com/ru/content -> russian and so on.

Then, thanks to your work, fetching the localized content is flawless, until a fallback value is needed.

Clearly according to tests your class seems to work as expected (I must say I've not run them) so the problem is in the outer environment. My guess, also noticing that you aren't using app.fallback_locale (either because it's been introduced fairly recently, or I don't know.. :)), is that to follow your workflow I have to carry on the user locale in another variable and use app.locale as the default; but watching demos, you suggest something like App::setLocale($locale) and then $model->property; here $locale have the same meaning I'm giving to it (call it user locale, view locale, as opposed to default: i.e. when url doesn't tell nothing about locale). So everything is just a guess :), but the only way to make all play well is with that little change.

I can send you the code if you want to take a look at how I'm implementing it. Sorry if my english is not helping.. Thank you again!

@dimsav
Copy link
Owner

dimsav commented Jun 18, 2014

Hi @gpasci, I wasn't aware of app.fallback_locale, probably because it was added recently. Thanks for the info!

I agree with you that this should be used as fallback_locale and not the App::getLocale().

I created issue #24 to fix this in a future version.

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

3 participants