Skip to content

Instantly share code, notes, and snippets.

@miguelplazasr
Created October 5, 2016 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelplazasr/7eb87038a9bd71cf1ecbcad520224d77 to your computer and use it in GitHub Desktop.
Save miguelplazasr/7eb87038a9bd71cf1ecbcad520224d77 to your computer and use it in GitHub Desktop.
Conviert un archivo i18n de Symfony en JSON
namespace AppBundle\Controller\api;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Routing\ClassResourceInterface;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Yaml\Yaml;
class TranslateController extends FOSRestController implements ClassResourceInterface
{
/**
* Get array by locale
*
* @ApiDoc(
* resource = true,
* section="Translate",
* statusCodes = {
* 200 = "Returned when successful",
* 400 = "Returned when the page is not found"
* }
* )
* @param $part
* @param $locale
* @return JsonResponse
*/
public function getLocaleAction($part, $locale)
{
$response = new JsonResponse();
$response
->setData($this->convertYml($part, $locale))
->setStatusCode(200);
return $response;
}
private function convertYml($part, $locale)
{
$file = $part . '.' . $locale . '.yml';
try {
$object = Yaml::parse(file_get_contents($this->get('kernel')->getRootDir() . "/Resources/translations/" . $file));
return $object;
} catch (\Exception $e) {
return 'The file "' . $file . '" does not exist.';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment