core icon indicating copy to clipboard operation
core copied to clipboard

Add multi-language OpenAPI specification

Open alebedev80 opened this issue 2 years ago • 1 comments

I need multi-language API documentation. For now possible to generate single language only (which use in resource files). How better to implement it? Build instance of ApiPlatform\OpenApi\OpenApi with already translated field or do translation during normalization in ApiPlatform\OpenApi\Serializer\ApiGatewayNormalizer?

I think better to translate OpenApi instance and propose to implement OpenApiFactoryInterface decorator in follow way:

// OpenApiTranslator.php

<?php

namespace App\Docs\OpenAPI;

use ApiPlatform\OpenApi\OpenApi;

interface OpenApiTranslator
{
    public function translate(OpenApi $openApi, string $lang = 'en'): OpenApi;
}


// OpenApiTranslatorFactory.php
<?php

declare(strict_types=1);

namespace App\Docs\OpenAPI;

use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
use ApiPlatform\OpenApi\OpenApi;

readonly class OpenApiTranslatorFactory implements OpenApiFactoryInterface
{
    private const DEFAULT_LANG = 'en';
    public function __construct(
        private OpenApiFactoryInterface $decorated,
        private OpenApiTranslator $translator
    )
    {
    }

    /**
     * @inheritDoc
     */
    public function __invoke(array $context = []): OpenApi
    {
        $lang = $context['lang'] ?? self::DEFAULT_LANG;
        $openApi =($this->decorated)($context);
        return $this->translator->translate($openApi, $lang);
    }
}

and fix commad api:openapi:export command to check which translation available (or add some argument) and generate translated OpenAPI specifications with some suffix in the filename, e.g. 'openapi_en.json', 'openapi_de.json' and etc.

alebedev80 avatar Aug 30 '23 09:08 alebedev80

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.

stale[bot] avatar Oct 29 '23 11:10 stale[bot]