feat(wip): resource translation#4342
Conversation
7fb3bb6 to
6793e3d
Compare
|
Hey Alan, thanks a lot for your work, looks really good. I cannot wait to test it in Sylius. One question tho. Is it required to have |
|
Hello @lchrusciel, |
1e9e437 to
baa310f
Compare
baa310f to
51ca0ea
Compare
686badb to
740be25
Compare
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
4072542 to
eb4bf57
Compare
52b0588 to
170f3f1
Compare
170f3f1 to
37e39a9
Compare
|
Hey @alanpoulain, it seems really nice. I wanted to mention another approach to implementing translations, that can be seen in https://github.com/Locastic/ApiPlatformTranslationBundle. The translations there are stored in the In the projects we use, the translations are not returned by default and can be requested via the For example if you make a get request in french and also retrieve the translations, you'll get the following: What do you think about this approach? |
|
Hello @SpartakusMd. {
"@context": {
"@vocab": "http://example.com/docs.jsonld#",
"hydra": "http://www.w3.org/ns/hydra/core#",
"@language": "fr"
},
"@id": "/books/1",
"@type": "Book",
"name": "Nom en français"
}The format is only changed if you request all the translations. |
|
Exactly. The format is only changed if you request all the translations and it becomes something like this: The field |
|
OK I understand your point but it will not be changed since the specification defines it like this: https://www.w3.org/TR/json-ld/#example-71-language-map-expressing-a-property-in-three-languages |
|
There's no update about this PR? |
|
Hi, are you still interested by this PR ? Do you have time to finish it and resolve the conflict ? Also, now Symfony has his own Translatable interface, does it still make sens to have one in ApiPlatform ? |
|
No I think this PR should be closed or taken over. |
Introduces two interfaces:
TranslatableInterfaceandTranslationInterface.It's the user responsibility to implement these interfaces how they want, either by themselves or by using extensions like https://github.com/KnpLabs/DoctrineBehaviors/blob/master/docs/translatable.md.
During the serialization, if the resource uses the
TranslatableInterface, the attribute value will be taken from the resource translation (represented byTranslationInterface), if available for the current locale.During the deserialization (
POST,PUT,PATCH), if the resource uses theTranslatableInterface, a resource translation will either be created or updated (if it already exists for the current locale).In order to to it, the translatable resource needs to have the corresponding translation attribute (not mapped to Doctrine).
For instance, if we have a
BookTranslationresource:Click to expand
Then the
Bookresource needs to have anameattribute:Click to expand
You can also see that there is a new
translationattribute for the resource.It contains:
class: the related translation classallTranslationsEnabled(orall_translations_enabled): to enable the retrieval of all translationsallTranslationsClientEnabled(orall_translations_client_enabled): to allow the client to retrieve all translationsallTranslationsClientParameterName(orall_translations_client_parameter_name): the parameter used to retrieve all translationsAll the translations for a resource can be retrieved either by enabling
allTranslationsEnabled(in this case they will always be sent in the response) orallTranslationsClientEnabled.For instance with this configuration:
Click to expand
If the following request is sent:
GET /books?allT=1The response will be:
Click to expand
{ "@context": { "@vocab": "http://example.com/docs.jsonld#", "hydra": "http://www.w3.org/ns/hydra/core#", "name": { "@container": "@language", "@id": "Book/name" } }, "@id": "/books", "@type": "hydra:Collection", "hydra:member": [ { "@id": "/books/1", "@type": "Book", "name": { "en": "Name in English", "fr": "Nom en français" } } ], "hydra:totalItems": 1, "hydra:view": { "@id": "/books?allT=1", "@type": "hydra:PartialCollectionView" } }The JSON-LD context is also changed accordingly (see https://json-ld.org/spec/latest/json-ld/#string-internationalization).
TODO:
To change the locale, you can enable and use the
Accept-Languageheader: https://symfony.com/blog/new-in-symfony-5-4-language-negotiationSee also: