translatable-dataobject icon indicating copy to clipboard operation
translatable-dataobject copied to clipboard

Fallback to other language than default language possible?

Open derralf opened this issue 8 years ago • 1 comments

Hi, is it possible to fallback to another language than the default/master language (with $strict = false)?

Lets say i have 3 languages: german, english, russian. Default language is german (most content ist german, client is german).

Now when some contents are translated to english (but not to russian), it would be nice to display english (more international) contents to russian visitors (instead of german though it's the master language).

derralf avatar Sep 02 '16 15:09 derralf

Currently, that's not possible. Ideally, your project-setup has the master-language as preferred fallback. Eg. English as master-language.

You could create a separate extension with that functionality though…

class FallbackTranslationExtension extends DataExtension 
{
    public function FT($fieldName)
    {
        $value = $this->owner->getLocalizedValue($fieldName, true);
        if ($value) {
            return $value;
        }

        // Using `en_US` as fallback locale here
        $fallback = TranslatableDataObject::localized_field($fieldName, 'en_US');
        return $this->owner->dbObject($fallback);
    }
}

Then use $FT('MyField') in templates…

I'm not sure how common your use-case is… maybe it would be worthwhile to add the functionality to the module. Ideally, this would be configurable via config, so that you can specify a fallback order for every language? The problem I see with that, is that it's only applied to DataObjects and doesn't affect the translation of pages, which could be confusing.

bummzack avatar Sep 03 '16 07:09 bummzack