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

Is it possible to have a localizable / translatable Custom Entity #144

Open
kanduvisla opened this issue Jan 24, 2018 · 6 comments
Open

Comments

@kanduvisla
Copy link

The Pictogram example only shows how you can have a label that is translatable / localizable. But is it also possible to have the values of your Custom Entity localizable?

For example: if I look at fabric, would it also be possible to have the fields name and alternative name localizable?

@kanduvisla
Copy link
Author

kanduvisla commented Jan 25, 2018

Small update: if I look at the updateTranslations()-method in Pim\Bundle\CustomeEntityBundle\Updater I see the following method:

protected function updateTranslations(AbstractTranslatableCustomEntity $referenceData, $propertyPath, $values): void
{
    foreach ($values as $locale => $value) {
        if (!in_array($locale, $this->localeRepository->getActivatedLocaleCodes())) {
            throw new \InvalidArgumentException(
                sprintf('Locale "%s" is not activated', $locale)
            );
        }
        $translation = $referenceData->getTranslation($locale);
        $translation->setLabel($value);
    }
}

This method seems so imply that only the label of an entity is translatable. However, there is a $propertyPath-argument, indicating that also another property should be able to translated. However, this argument never seems to be used in this method. Is this a bug?

edit:

It looks like that if I replace $translation->setLabel($value) with call_user_func([$translation,'set' . ucfirst($propertyPath)], $value); it works in my case. It might not be bulletproof, but I'm thinking something funny is going on here. Or I am misunderstanding how the internals of Akeneo work. Perhaps someone with more insight in Akeneo could spread some light on this?

@kanduvisla
Copy link
Author

Can anyone tell me if this is intended behavior? Because I currently have a working setup with localizable custom entities and I'm willing enough to make a pull request for this feature, but I'm not sure if what I'm doing is correct. I'm very new to Akeneo (I've only worked 2 or 3 weeks with it now), but I do have a lot of experience with PHP and Magento 2. Here's what I've got:

When analyzing the code of the custom entity bundle, it appeared to me that there was no built-in option for localizable custom entities. The example provided only translated a label and this works, but the core code suggests that the label is in fact the only property that is localizable. For example: if I have a custom entity called FAQ, with a property question and answer, it's not possible for me to have these 2 properties localizable. Question 1: is this assumption correct?

So what I did was the following:

  • I extended the Updater-class so it dynamically sets the properts (as in my previous comment)
  • I added frontend JavaScript / templates for localizable textfields, textarea and a language switcher.
  • I extended the ReferenceDataProcessor for the normalizer and the denormalizer on order for the import/export-job to function with the translation-array.

I can create a pull request with these adjustments / edits, but once again: I'm very new to Akeneo so it might be that I'm completely on the wrong track anyhow. There is not much documentation out there and the community seems kind of small.

@mmetayer
Copy link
Collaborator

Hi @kanduvisla ,

Your assumption is correct, currently you can only update a label property without overriding the Updater. Do not hesitate to open a PR with your edits, we will review it as soon as possible. A few tips before submitting your code:

Regards,
Mathias

@kanduvisla
Copy link
Author

Thanks! I already thought that there had to be a better way than call_user_func() 😜

@kanduvisla
Copy link
Author

@mmetayer : I've create a PR of what I've got so far. I still want to expand it with tests and example code, but it would be nice to know if I'm on the right track.

@mmetayer
Copy link
Collaborator

Thank you very much for the PR @kanduvisla 🙂, I'm quite busy at the moment, but I'll give it a look as soon as I can.

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

No branches or pull requests

4 participants