Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translated attachements have no fallback option #746

Open
mariavilaro opened this issue Jul 31, 2024 · 2 comments
Open

Translated attachements have no fallback option #746

mariavilaro opened this issue Jul 31, 2024 · 2 comments

Comments

@mariavilaro
Copy link
Contributor

I think that it would be good (and coherent with how this plugin works) to have a fallback value for translated attachements, so the default language file is shown if no translations are uploaded.

@daftspunk
Copy link
Member

daftspunk commented Jul 31, 2024

I agree with this. Technically it is very difficult to implement: the relation uses a key that includes the locale in it. This means the SQL engine is responsible for the fallback logic and this is where the difficulty arises.

edit:

After giving this some thought, we could introduce a second _fallback relation

public $translatable = [
    ['photo', 'fallback' => true]
];

public $attachOne = [
    'photo' => \System\Models\File::class,
];

The above would generate two relations called photo and photo_fallback, this should make it possible to check for a translated value explicitly and then use the fallback, if required.

For example:

$photo = $user->photo ?: $user->photo_fallback;

Would this work?

@mariavilaro
Copy link
Contributor Author

For now I solved my problem with this code:

public $translatable = ['title', 'image_ml_relation'];

public $attachOne = [
    'image_ml_relation' => 'System\Models\File',
];

public function getImageMlAttribute()
{
    $image = $this->image_ml_relation;
    if (!$image) {
        $currentLocale = \RainLab\Translate\Classes\Translator::instance()->getLocale();
        $defaultLocale = \RainLab\Translate\Classes\Translator::instance()->getDefaultLocale();
        if ($currentLocale != $defaultLocale) {
            $image = $this->lang($defaultLocale)->image_ml_relation;
        }
    }
    return $image;
}

And I get the image from the front with {{ model.image_ml.path }}

Maybe this could be integrated in the plugin somehow (change the name of the relation automatically to name+_relation, and then add a new accessor for the original attribute name, that gets the fallback if needed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants